Skip to content

Commit 84058c3

Browse files
committed
feat: add solutions to lc problem: No.0011
No.0011.Container With Most Water
1 parent 7fd80af commit 84058c3

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

solution/0000-0099/0011.Container With Most Water/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,45 @@ var maxArea = function (height) {
174174
};
175175
```
176176

177+
### **TypeScript**
178+
179+
```ts
180+
function maxArea(height: number[]): number {
181+
const n = height.length;
182+
let res = 0;
183+
for (let i = 0; i < n - 1; i++) {
184+
for (let j = n - 1; j >= 0; j--) {
185+
if (height[i] * (j - i) < res) {
186+
break;
187+
}
188+
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
189+
}
190+
}
191+
return res;
192+
}
193+
```
194+
195+
### **Rust**
196+
197+
```rust
198+
impl Solution {
199+
pub fn max_area(height: Vec<i32>) -> i32 {
200+
let mut i = 0;
201+
let mut j = height.len() - 1;
202+
let mut res = 0;
203+
while i < j {
204+
res = res.max(height[i].min(height[j]) * (j - i) as i32);
205+
if height[i] <= height[j] {
206+
i += 1;
207+
} else {
208+
j -= 1;
209+
}
210+
}
211+
res
212+
}
213+
}
214+
```
215+
177216
### **...**
178217

179218
```

solution/0000-0099/0011.Container With Most Water/README_EN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,45 @@ var maxArea = function (height) {
149149
};
150150
```
151151

152+
### **TypeScript**
153+
154+
```ts
155+
function maxArea(height: number[]): number {
156+
const n = height.length;
157+
let res = 0;
158+
for (let i = 0; i < n - 1; i++) {
159+
for (let j = n - 1; j >= 0; j--) {
160+
if (height[i] * (j - i) < res) {
161+
break;
162+
}
163+
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
164+
}
165+
}
166+
return res;
167+
}
168+
```
169+
170+
### **Rust**
171+
172+
```rust
173+
impl Solution {
174+
pub fn max_area(height: Vec<i32>) -> i32 {
175+
let mut i = 0;
176+
let mut j = height.len() - 1;
177+
let mut res = 0;
178+
while i < j {
179+
res = res.max(height[i].min(height[j]) * (j - i) as i32);
180+
if height[i] <= height[j] {
181+
i += 1;
182+
} else {
183+
j -= 1;
184+
}
185+
}
186+
res
187+
}
188+
}
189+
```
190+
152191
### **...**
153192

154193
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn max_area(height: Vec<i32>) -> i32 {
3+
let mut i = 0;
4+
let mut j = height.len() - 1;
5+
let mut res = 0;
6+
while i < j {
7+
res = res.max(height[i].min(height[j]) * (j - i) as i32);
8+
if height[i] <= height[j] {
9+
i += 1;
10+
} else {
11+
j -= 1;
12+
}
13+
}
14+
res
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxArea(height: number[]): number {
2+
const n = height.length;
3+
let res = 0;
4+
for (let i = 0; i < n - 1; i++) {
5+
for (let j = n - 1; j >= 0; j--) {
6+
if (height[i] * (j - i) < res) {
7+
break;
8+
}
9+
res = Math.max(res, Math.min(height[i], height[j]) * (j - i));
10+
}
11+
}
12+
return res;
13+
}

0 commit comments

Comments
 (0)