From 5f650ab9fc90208ec2925eee4dfe35961ed25296 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sun, 18 Jul 2021 22:59:42 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0042.Trapping Rain Water --- .../0042.Trapping Rain Water/README.md | 30 +++++++++++++++++++ .../0042.Trapping Rain Water/README_EN.md | 30 +++++++++++++++++++ .../0042.Trapping Rain Water/Solution.ts | 25 ++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 solution/0000-0099/0042.Trapping Rain Water/Solution.ts diff --git a/solution/0000-0099/0042.Trapping Rain Water/README.md b/solution/0000-0099/0042.Trapping Rain Water/README.md index 5a666bb283351..3f84f971b3136 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/README.md +++ b/solution/0000-0099/0042.Trapping Rain Water/README.md @@ -102,6 +102,36 @@ class Solution { } ``` +### **TypeScript** + +```ts +function trap(height: number[]): number { + let ans = 0; + let left = 0, right = height.length - 1; + let maxLeft = 0, maxRight = 0; + while (left < right) { + if (height[left] < height[right]) { + // move left + if (height[left] >= maxLeft) { + maxLeft = height[left]; + } else { + ans += (maxLeft - height[left]); + } + ++left; + } else { + // move right + if (height[right] >= maxRight) { + maxRight = height[right]; + } else { + ans += (maxRight - height[right]); + } + --right; + } + } + return ans; +}; +``` + ### **C++** ```cpp diff --git a/solution/0000-0099/0042.Trapping Rain Water/README_EN.md b/solution/0000-0099/0042.Trapping Rain Water/README_EN.md index 791b1461a9def..c00c414234d3e 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/README_EN.md +++ b/solution/0000-0099/0042.Trapping Rain Water/README_EN.md @@ -84,6 +84,36 @@ class Solution { } ``` +### **TypeScript** + +```ts +function trap(height: number[]): number { + let ans = 0; + let left = 0, right = height.length - 1; + let maxLeft = 0, maxRight = 0; + while (left < right) { + if (height[left] < height[right]) { + // move left + if (height[left] >= maxLeft) { + maxLeft = height[left]; + } else { + ans += (maxLeft - height[left]); + } + ++left; + } else { + // move right + if (height[right] >= maxRight) { + maxRight = height[right]; + } else { + ans += (maxRight - height[right]); + } + --right; + } + } + return ans; +}; +``` + ### **C++** ```cpp diff --git a/solution/0000-0099/0042.Trapping Rain Water/Solution.ts b/solution/0000-0099/0042.Trapping Rain Water/Solution.ts new file mode 100644 index 0000000000000..5cc965adafc9d --- /dev/null +++ b/solution/0000-0099/0042.Trapping Rain Water/Solution.ts @@ -0,0 +1,25 @@ +function trap(height: number[]): number { + let ans = 0; + let left = 0, right = height.length - 1; + let maxLeft = 0, maxRight = 0; + while (left < right) { + if (height[left] < height[right]) { + // move left + if (height[left] >= maxLeft) { + maxLeft = height[left]; + } else { + ans += (maxLeft - height[left]); + } + ++left; + } else { + // move right + if (height[right] >= maxRight) { + maxRight = height[right]; + } else { + ans += (maxRight - height[right]); + } + --right; + } + } + return ans; +}; \ No newline at end of file