-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlib.rs
More file actions
33 lines (31 loc) · 673 Bytes
/
lib.rs
File metadata and controls
33 lines (31 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
* @lc app=leetcode.cn id=11 lang=rust
*
* [11] 盛最多水的容器
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn max_area(height: Vec<i32>) -> i32 {
let (mut left, mut right, mut res) = (0, height.len() - 1, 0);
while left < right {
res = res.max(height[left].min(height[right]) * (right - left) as i32);
if height[left] < height[right] {
left = left + 1;
} else {
right = right - 1;
}
}
return res;
}
}
// @lc code=end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
Solution::max_area(vec![1, 8, 6, 2, 5, 4, 8, 3, 7]);
Solution::max_area(vec![1, 1]);
}
}