Skip to content

Commit 701a57a

Browse files
authored
Merge pull request #10 from rickrain/rickrain/prob-11-water-container
Rickrain/prob 11 water container
2 parents 24fdb50 + d94b6e0 commit 701a57a

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Each challenge in this repo is organized into a module with tests specific to th
1616
- [string to int](./src/prob_0008_string_to_int/README.md)
1717
- [palindrome](./src/prob_0009_palindrome/README.md)
1818
- [permutations](./src/prob_0046_permutations/README.md)
19+
- [container w/most water](./src/prob_0011_container_with_most_water/README.md)
1920

2021
## Running the challenges
2122

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod prob_0006_zigzag_conversation;
77
mod prob_0007_reverse_integer;
88
mod prob_0008_string_to_int;
99
mod prob_0009_palindrome;
10+
mod prob_0011_container_with_most_water;
1011
mod prob_0046_permutations;
1112

1213
fn main() {}

src/prob_0004_median_two_sorted_arrays/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,20 @@ impl Solution {
2828
let mid2 = ((nums1.len() + nums2.len() + 1) / 2) - mid1;
2929

3030
// Get the values on either side of the partition for the smaller array
31-
let l1 = if mid1 == 0 { i32::MIN } else { nums1[mid1 - 1] };
32-
let r1 = if mid1 >= nums1.len() { i32::MAX } else { nums1[mid1] };
31+
let l1 = if mid1 == 0 { i32::MIN } else { nums1[mid1 - 1] };
32+
let r1 = if mid1 >= nums1.len() {
33+
i32::MAX
34+
} else {
35+
nums1[mid1]
36+
};
3337

3438
// Get the values on either side of the partition for the larger array
35-
let l2 = if mid2 == 0 { i32::MIN } else { nums2[mid2 - 1] };
36-
let r2 = if mid2 >= nums2.len() { i32::MAX } else { nums2[mid2] };
39+
let l2 = if mid2 == 0 { i32::MIN } else { nums2[mid2 - 1] };
40+
let r2 = if mid2 >= nums2.len() {
41+
i32::MAX
42+
} else {
43+
nums2[mid2]
44+
};
3745

3846
// We're done partitioning the array and can calculate the median
3947
if l1 <= r2 && l2 <= r1 {
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Container With Most Water
2+
3+
[Back to root-level README](../../README.md)
4+
5+
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that the two endpoints of the i<sup>th</sup> line are `(i, 0)` and `(i, height[i])`.
6+
7+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
8+
9+
Return _the maximum amount of water a container can store_.
10+
11+
**Notice** that you may not slant the container.
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
![Example 1](./11_ex_01.jpg)
18+
19+
```console
20+
Input: height = [1,8,6,2,5,4,8,3,7]
21+
Output: 49
22+
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.
23+
```
24+
25+
### Example 2
26+
27+
```console
28+
Input: height = [1,1]
29+
Output: 1
30+
```
31+
32+
## Constraints
33+
34+
- `n == height.length`
35+
- `2 <= n <= 10^5`
36+
- `0 <= height[i] <= 10^4`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::cmp;
2+
3+
pub struct Solution;
4+
5+
#[allow(dead_code)]
6+
impl Solution {
7+
pub fn max_area(height: Vec<i32>) -> i32 {
8+
let mut left_idx = 0;
9+
let mut right_idx = height.len() - 1;
10+
let mut max_area = 0;
11+
12+
while left_idx < right_idx {
13+
let gap = (right_idx - left_idx) as i32;
14+
max_area = max_area.max(cmp::min(height[left_idx], height[right_idx]) * gap);
15+
16+
if height[left_idx] < height[right_idx] {
17+
left_idx += 1;
18+
} else {
19+
right_idx -= 1;
20+
}
21+
}
22+
23+
max_area
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use crate::prob_0011_container_with_most_water::Solution;
30+
31+
#[test]
32+
fn solution_works() {
33+
assert_eq!(Solution::max_area(vec![1, 8, 6, 2, 5, 4, 8, 3, 7]), 49);
34+
assert_eq!(Solution::max_area(vec![1, 1]), 1);
35+
}
36+
}

0 commit comments

Comments
 (0)