Skip to content

Commit f781d8b

Browse files
committed
Amazon iOS interview question - Container With Most Water
1 parent 0ed5135 commit f781d8b

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

Help.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
5EAC92C726BD35E700A6198C /* SaveAndLoadDataOnDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EAC92C626BD35E700A6198C /* SaveAndLoadDataOnDevice.swift */; };
5757
5EAF0B81268C5F4300D92322 /* LaunchScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EAF0B80268C5F4300D92322 /* LaunchScreen.swift */; };
5858
5ED4FE63270920F700E3B850 /* License in Resources */ = {isa = PBXBuildFile; fileRef = 5ED4FE62270920F700E3B850 /* License */; };
59+
5EDBDABA279B5C5B006DF495 /* ContainerWithMostWater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EDBDAB9279B5C5B006DF495 /* ContainerWithMostWater.swift */; };
5960
5EDE65CE26E29E0000DB71C0 /* SwipeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EDE65CD26E29E0000DB71C0 /* SwipeScreen.swift */; };
6061
5EF5E01526F72FE40068A717 /* EnumDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5EF5E01426F72FE40068A717 /* EnumDataModel.swift */; };
6162
/* End PBXBuildFile section */
@@ -112,6 +113,7 @@
112113
5EAC92C626BD35E700A6198C /* SaveAndLoadDataOnDevice.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SaveAndLoadDataOnDevice.swift; sourceTree = "<group>"; };
113114
5EAF0B80268C5F4300D92322 /* LaunchScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchScreen.swift; sourceTree = "<group>"; };
114115
5ED4FE62270920F700E3B850 /* License */ = {isa = PBXFileReference; lastKnownFileType = text; path = License; sourceTree = "<group>"; };
116+
5EDBDAB9279B5C5B006DF495 /* ContainerWithMostWater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContainerWithMostWater.swift; sourceTree = "<group>"; };
115117
5EDE65CD26E29E0000DB71C0 /* SwipeScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeScreen.swift; sourceTree = "<group>"; };
116118
5EF5E01426F72FE40068A717 /* EnumDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumDataModel.swift; sourceTree = "<group>"; };
117119
/* End PBXFileReference section */
@@ -140,6 +142,7 @@
140142
children = (
141143
5E1F30D5278C9C5E009446ED /* Pascal'sTriangle.swift */,
142144
5E4D3FCA278FA2F800A0861A /* SpiralMatrix.swift */,
145+
5EDBDAB9279B5C5B006DF495 /* ContainerWithMostWater.swift */,
143146
);
144147
path = LeetCode;
145148
sourceTree = "<group>";
@@ -397,6 +400,7 @@
397400
5EAC92BA26BA8D0D00A6198C /* MoveRow.swift in Sources */,
398401
5E83856B2667652700ACA770 /* CleanClass.swift in Sources */,
399402
5E83857E2667B2A400ACA770 /* DeadCode.swift in Sources */,
403+
5EDBDABA279B5C5B006DF495 /* ContainerWithMostWater.swift in Sources */,
400404
5E8385802667B63900ACA770 /* MARK.swift in Sources */,
401405
5EAC17FA26808458006EC7CD /* PassDataClosure.swift in Sources */,
402406
5EAC92BE26BC0BCD00A6198C /* Source.swift in Sources */,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// ContainerWithMostWater.swift
3+
// Help
4+
//
5+
// Created by Sergey Lukaschuk on 21.01.2022.
6+
//
7+
8+
import Foundation
9+
10+
// MARK: Container With Most Water
11+
12+
/*
13+
You are given an integer array `height` of length `n`. There are n vertical lines drawn such that the two endpoints of the `ith` line are `(i, 0)` and `(i, height[i])`.
14+
15+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
16+
17+
Return the maximum amount of water a container can store.
18+
19+
**Notice** that you may not slant the container.
20+
21+
**Example 1:**<br>
22+
*Input:* `height = [1,8,6,2,5,4,8,3,7]` <br>
23+
*Output:* `49` <br>
24+
*Explanation:* The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
25+
26+
**Example 2:**<br>
27+
*Input:* `height = [1,1]`<br>
28+
*Output:* `1`<br>
29+
30+
*/
31+
32+
func maxArea(_ height: [Int]) -> Int {
33+
guard !height.isEmpty else { return -1 }
34+
35+
var maxArea = 0
36+
var left = 0
37+
var right = height.count - 1
38+
39+
while left < right {
40+
// Re-calc maxArea
41+
let minHeight = min(height[left], height[right])
42+
let currentHeight = minHeight * (right - left)
43+
maxArea = max(maxArea, currentHeight)
44+
45+
// Move pointers
46+
if height[left] < height[right] {
47+
left += 1
48+
} else {
49+
right -= 1
50+
}
51+
}
52+
return maxArea
53+
}
54+
55+
let input = [1, 8, 6, 2, 5, 4, 8, 3, 7]
56+
let result = maxArea(input)
57+
//print("Result: \(result)")

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,57 @@ func spiralOrder(_ matrix: [[Int]]) -> [Int] {
15581558

15591559

15601560

1561+
### [Container With Most Water](https://github.com/lgreydev/Help/blob/master/Help/LeetCode/ContainerWithMostWater.swift)
1562+
1563+
You are given an integer array `height` of length `n`. There are n vertical lines drawn such that the two endpoints of the `ith` line are `(i, 0)` and `(i, height[i])`.
1564+
1565+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
1566+
1567+
Return the maximum amount of water a container can store.
1568+
1569+
**Notice** that you may not slant the container.
1570+
1571+
**Example 1:**<br>
1572+
*Input:* `height = [1,8,6,2,5,4,8,3,7]` <br>
1573+
*Output:* `49` <br>
1574+
*Explanation:* The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
1575+
1576+
**Example 2:**<br>
1577+
*Input:* `height = [1,1]`<br>
1578+
*Output:* `1`<br>
1579+
1580+
``` swift
1581+
1582+
func maxArea(_ height: [Int]) -> Int {
1583+
guard !height.isEmpty else { return -1 }
1584+
1585+
var maxArea = 0
1586+
var left = 0
1587+
var right = height.count - 1
1588+
1589+
while left < right {
1590+
// Re-calc maxArea
1591+
let minHeight = min(height[left], height[right])
1592+
let currentHeight = minHeight * (right - left)
1593+
maxArea = max(maxArea, currentHeight)
1594+
1595+
// Move pointers
1596+
if height[left] < height[right] {
1597+
left += 1
1598+
} else {
1599+
right -= 1
1600+
}
1601+
}
1602+
return maxArea
1603+
}
1604+
1605+
let input = [1, 8, 6, 2, 5, 4, 8, 3, 7]
1606+
let result = maxArea(input)
1607+
//print("Result: \(result)")
1608+
1609+
```
1610+
1611+
15611612
### 🛡️ License
15621613

15631614
This project is licensed under the MIT License - see the [`LICENSE`](https://github.com/lgreydev/Help/blob/master/License) file for details.

0 commit comments

Comments
 (0)