Skip to content

Commit

Permalink
leetcode: Add container-with-most-water
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Dec 7, 2023
1 parent 1aab9f8 commit 8d3b107
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions leetcode/0011.container-with-most-water/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-11-container-with-most-water"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
30 changes: 30 additions & 0 deletions leetcode/0011.container-with-most-water/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2023 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be
// found in the LICENSE file.

pub fn max_area(height: Vec<i32>) -> i32 {
let len = height.len();
assert!(len > 1);

let mut max_area = 0;
for i in 0..(len - 1) {
for j in (i + 1)..len {
let min_height = height[i].min(height[j]);
let area = min_height * (j - i) as i32;
if max_area < area {
max_area = area;
}
}
}
max_area
}

fn main() {
let height = vec![1, 8, 6, 2, 5, 4, 8, 3, 7];
let out = max_area(height);
assert_eq!(out, 49);

let height = vec![1, 1];
let out = max_area(height);
assert_eq!(out, 1);
}

0 comments on commit 8d3b107

Please sign in to comment.