Skip to content

Commit

Permalink
feat: 增加 11. 盛最多水的容器
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Jul 9, 2024
1 parent fe565f5 commit 1819d2a
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default defineConfig({
{ text: '6. Z 字形变换', link: '/leetcode/zigzagConversion' },
{ text: '7. 整数反转', link: '/leetcode/reverseInteger' },
{ text: '9. 回文数', link: '/leetcode/palindromeNumber' },
{ text: '11. 盛最多水的容器', link: '/leetcode/containerWithMostWater' },
{ text: '83. 删除排序链表中的重复元素', link: '/leetcode/removeDuplicatesFromSortedList' },
{ text: '2085. 统计出现过一次的公共字符串', link: '/leetcode/countCommonWordsWithOneOccurrence' },
{ text: '2182. 构造限制重复的字符串', link: '/leetcode/constructStringWithRepeatLimit' },
Expand Down
12 changes: 12 additions & 0 deletions docs/leetcode/containerWithMostWater.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 11. 盛最多水的容器

[11. 盛最多水的容器](https://leetcode.cn/problems/container-with-most-water/description/)

## 代码

::: code-group

<<< ../../src/leetcode/containerWithMostWater/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/containerWithMostWater/typescript.ts{typescript} [typescript]

:::
20 changes: 20 additions & 0 deletions src/leetcode/containerWithMostWater/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 输入:[1,8,6,2,5,4,8,3,7]
// 输出:49

export function maxArea(height) {
if (!height.length || height.length === 1) return 0
let max = 0
let l = 0
let r = height.length - 1
max = (r - l) * Math.min(height[l], height[r])
while (l < r) {
if (height[l] < height[r]) {
l++
} else {
r--
}
const area = (r - l) * Math.min(height[l], height[r])
if (max < area) max = area
}
return max
}
31 changes: 31 additions & 0 deletions src/leetcode/containerWithMostWater/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 输入:[1,8,6,2,5,4,8,3,7]
// 输出:49

// function maxArea(height: number[]): number {
// if (!height.length || height.length === 1) return 0
// for (let i = 0; i < height.length; i++) {
// for (let j = i + 1; j < height.length; j++) {
// const area = (j - i) * Math.min(height[i], height[j])
// if (max < area) max = area
// }
// }
// return max
// }

export function maxArea(height: number[]): number {
if (!height.length || height.length === 1) return 0
let max = 0
let l = 0
let r = height.length - 1
max = (r - l) * Math.min(height[l], height[r])
while (l < r) {
if (height[l] < height[r]) {
l++
} else {
r--
}
const area = (r - l) * Math.min(height[l], height[r])
if (max < area) max = area
}
return max
}
3 changes: 3 additions & 0 deletions src/leetcode/palindromeNumber/javascript.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// 输入:x = 121
// 输出:true

export function isPalindrome(x) {
if (x < 0) return false
let num = x
Expand Down
3 changes: 3 additions & 0 deletions src/leetcode/palindromeNumber/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// 输入:x = 121
// 输出:true

export function isPalindrome(x: number): boolean {
if (x < 0) return false
let num = x
Expand Down
7 changes: 7 additions & 0 deletions test/containerWithMostWater.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'
import { maxArea } from '../src/leetcode/containerWithMostWater/typescript.ts'


test(`[1,8,6,2,5,4,8,3,7]`, () => {
expect(maxArea([1,8,6,2,5,4,8,3,7])).toBe(49)
})

0 comments on commit 1819d2a

Please sign in to comment.