Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions solution/0000-0099/0042.Trapping Rain Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions solution/0000-0099/0042.Trapping Rain Water/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions solution/0000-0099/0042.Trapping Rain Water/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
};