8000 Push trapping true answer but too long · bezzad/LeetCode@53b69c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53b69c6

Browse files
committed
Push trapping true answer but too long
1 parent f277de9 commit 53b69c6

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var trap = function (height) {
6+
let sum = 0;
7+
let floorCount = Math.max(...height);
8+
9+
for (let f = 1; f <= floorCount; f++) {
10+
let firstLeftStone = height.length, firstRightStone = 0;
11+
for (let l = 0; l < height.length; l++) {
12+
if (height[l] >= f) {
13+
firstLeftStone = l;
14+
break;
15+
}
16+
}
17+
for (let r = height.length - 1; r > firstLeftStone; r--) {
18+
if (height[r] >= f) {
19+
firstRightStone = r;
20+
break;
21+
}
22+
}
23+
for (let i = firstLeftStone; i < firstRightStone; i++) {
24+
if (height[i] < f) {
25+
sum++;
26+
}
27+
}
28+
}
29+
30+
return sum;
31+
};
32+
33+
console.assert(trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]) == 6, "1");
34+
console.assert(trap([4, 2, 0, 3, 2, 5]) == 9, "2");
35+
console.assert(trap([2, 0, 2]) == 2, "3");

42. Trapping Rain Water/Question.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.
2+
3+
## Example 1:
4+
5+
![](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)
6+
7+
```
8+
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
9+
Output: 6
10+
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
11+
```
12+
13+
## Example 2:
14+
```
15+
Input: height = [4,2,0,3,2,5]
16+
Output: 9
17+
```
18+
19+
Constraints:
20+
21+
* `n == height.length`
22+
* `0 <= n <= 3 * 104`
23+
* `0 <= height[i] <= 105`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {number}
5+
*/
6+
var minDistance = function (word1, word2) {
7+
let largWord = word1.length >= word2.length ? word1 : word2;
8+
let smallWord = word1.length >= word2.length ? word2 : word1;
9+
let distance = 0;
10+
for (let i = 0; i < smallWord.length; i++) {
11+
for (let j = 0; j < largWord.length; j++) {
12+
13+
}
14+
}
15+
return distance;
16+
};
17+
18+
function insert(word, index, char) {
19+
return word.slice(0, index) + char + word.slice(index);
20+
}
21+
22+
function replace(word, index, char) {
23+
return word.slice(0, index) + char + word.slice(index + 1);
24+
}
25+
26+
function remove(word, index) {
27+
return word.slice(0, index) + word.slice(index + 1);
28+
}
29+
30+
31+
console.assert(minDistance("horse", "ros") == 3, "1");
32+
console.assert(minDistance("intention", "execution") == 5, "2");
33+
console.assert(minDistance("tour", "ruot") == 4, "3");

72. Edit Distance/Question.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Given two strings `word1` and `word2`, return the minimum number of operations required to convert `word1` to `word2`.
2+
3+
You have the following three operations permitted on a word:
4+
5+
`Insert` a character
6+
`Delete` a character
7+
`Replace` a character
8+
9+
## Example 1:
10+
```
11+
Input: word1 = "horse", word2 = "ros"
12+
Output: 3
13+
Explanation:
14+
horse -> rorse (replace 'h' with 'r')
15+
rorse -> rose (remove 'r')
16+
rose -> ros (remove 'e')
17+
```
18+
## Example 2:
19+
```
20+
Input: word1 = "intention", word2 = "execution"
21+
Output: 5
22+
Explanation:
23+
intention -> inention (remove 't')
24+
inention -> enention (replace 'i' with 'e')
25+
enention -> exention (replace 'n' with 'x')
26+
exention -> exection (replace 'n' with 'c')
27+
exection -> execution (insert 'u')
28+
```
29+
30+
## Constraints:
31+
32+
* `0 <= word1.length, word2.length <= 500`
33+
* `word1 and word2 consist of lowercase English letters.`

0 commit comments

Comments
 (0)
0