Skip to content

Commit 8d864f3

Browse files
committed
🔀 [merge]
2 parents 06ca52f + 26448c6 commit 8d864f3

File tree

5 files changed

+284
-1
lines changed

5 files changed

+284
-1
lines changed

2090/my_solution.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
const getAverages = (nums, k) => {
7+
if (k === 0) return nums;
8+
let result = [];
9+
10+
let sum = 0;
11+
for (let i = 0; i < (k * 2); i++) {
12+
sum += nums[i];
13+
}
14+
console.log("sum:" + sum)
15+
16+
for (let i = 0; i < nums.length; i++) {
17+
if (i < k || i + k >= nums.length) {
18+
result.push(-1);
19+
continue;
20+
}
21+
22+
if (undefined !== nums[i - k - 1]) sum -= nums[i - k - 1];
23+
sum += nums[i + k];
24+
25+
// for (let j = i - k; j < i + k + 1; j++) {
26+
// console.log(`from ${j} to ${i + k}`)
27+
// tmpSum += nums[j];
28+
// }
29+
30+
result.push(Math.floor(sum / ((k * 2) + 1)));
31+
}
32+
33+
return result;
34+
};
35+
36+
// const getAverages = (nums, k) => {
37+
// if (k === 0) return nums;
38+
// let result = [];
39+
//
40+
// for (let i = 0; i < nums.length; i++) {
41+
// if (i < k || i + k >= nums.length) {
42+
// result.push(-1);
43+
// continue;
44+
// }
45+
//
46+
// let tmpSum = 0;
47+
// console.log(`> ${i} `)
48+
// for (let j = i - k; j < i + k + 1; j++) {
49+
// console.log(`from ${j} to ${i + k}`)
50+
// tmpSum += nums[j];
51+
// }
52+
//
53+
// result.push(Math.floor(tmpSum / ((k * 2) + 1)));
54+
// }
55+
//
56+
// return result;
57+
// };
58+
59+
let x = null;
60+
// 9
61+
// _ _
62+
// 0 1 2 3 4 5 6 7 8
63+
x = getAverages([7, 4, 3, 9, 1, 8, 5, 2, 6], 3) // [-1,-1,-1,5,4,4,-1,-1,-1]
64+
// x = getAverages([100000], 0) // [100000]
65+
// x = getAverages([8], 100000)
66+
console.log("Result")
67+
console.log(x)

2090/solution.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
const getAverages = (nums, k) => {
7+
if (k === 0) return nums;
8+
let result = [];
9+
10+
let sum = 0;
11+
for (let i = 0; i < (k * 2); i++) {
12+
sum += nums[i];
13+
}
14+
15+
for (let i = 0; i < nums.length; i++) {
16+
if (i < k || i + k >= nums.length) {
17+
result.push(-1);
18+
continue;
19+
}
20+
21+
if (undefined !== nums[i - k - 1]) sum -= nums[i - k - 1];
22+
sum += nums[i + k];
23+
24+
result.push(Math.floor(sum / ((k * 2) + 1)));
25+
}
26+
27+
return result;
28+
};
29+
// const getAverages = (nums, k) => {
30+
// if (k === 0) return nums;
31+
// let result = [];
32+
//
33+
// for (let i = 0; i < nums.length; i++) {
34+
// if (i < k || i + k >= nums.length) {
35+
// result.push(-1);
36+
// continue;
37+
// }
38+
//
39+
// let tmpSum = 0;
40+
// console.log(`> ${i} `)
41+
// for (let j = i - k; j < i + k + 1; j++) {
42+
// tmpSum += nums[j];
43+
// }
44+
//
45+
// result.push(Math.floor(tmpSum / ((k * 2) + 1)));
46+
// }
47+
//
48+
// return result;
49+
// };

79/my_solution.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
const exist = (board, word) => {
7+
// let doesExist = false;
8+
// , wordList = word.split("");
9+
// console.log(wordList);
10+
// wordList.shift()
11+
// console.log(wordList);
12+
console.log(board);
13+
console.log(word);
14+
// let visit = Array.from({ length: board.length }, () => Array.from({ length: board[0].length }, () => 0))
15+
let visit = new Set();
16+
17+
const expand = (r, c, i) => {
18+
console.log(`r:${r}, c:${c}, i:${i}, visit:`)
19+
// console.log(wordList)
20+
console.log(visit)
21+
console.log("visited?:" + visit.has(`${r}${c}`))
22+
// console.log(i)
23+
// console.log(word.length)
24+
// console.log(i === word.length)
25+
26+
if (i === word.length) {
27+
console.log(">>>>>>>> >TRUE")
28+
return true;
29+
}
30+
31+
if (
32+
r === board.length ||
33+
c === board[0].length ||
34+
r < 0 ||
35+
c < 0 ||
36+
visit.has(`${r}${c}`) ||
37+
word[i] !== board[r][c]
38+
) {
39+
return false;
40+
}
41+
42+
// visit[r][c] = 1;
43+
visit.add(`${r}${c}`);
44+
console.log(visit)
45+
i++
46+
47+
let res = expand(r, c + 1, i) ||
48+
expand(r + 1, c, i) ||
49+
expand(r, c - 1, i) ||
50+
expand(r - 1, c, i);
51+
52+
console.log("... Deleting visit")
53+
console.log(visit)
54+
visit.delete(`${r}${c}`);
55+
console.log(visit)
56+
return res;
57+
// return expand(r, c + 1, i) ||
58+
// expand(r + 1, c, i) ||
59+
// expand(r, c - 1, i) ||
60+
// expand(r - 1, c, i);
61+
}
62+
63+
// console.log(board)
64+
for (let r = 0; r < board.length; r++) {
65+
for (let c = 0; c < board[r].length; c++) {
66+
if (word[0] === board[r][c]) {
67+
console.log(`>>>>> ${word[0]} === ${board[r][c]}`)
68+
// let wordList = word.split("");
69+
// expand(r, c, visit, wordList);
70+
if (expand(r, c, 0)) return true;
71+
}
72+
}
73+
}
74+
75+
return false;
76+
};
77+
78+
let x = null;
79+
// x = exist([["A", "B", "C", "E"], ["S", "F", "C", "S"], ["A", "D", "E", "E"]], "ABCCED")
80+
81+
// x = exist([
82+
// ["A", "B", "C", "E"],
83+
// ["S", "F", "C", "S"],
84+
// ["A", "D", "E", "E"]
85+
// ], "ABCCED")//true
86+
87+
// x = exist([
88+
// ["A", "B", "C", "E"],
89+
// ["S", "F", "E", "S"],
90+
// ["A", "D", "E", "E"]
91+
// ], "ABCEFSADEESE") // true
92+
93+
// x = exist(
94+
// [
95+
// ["A", "B", "C", "E"],
96+
// ["S", "F", "C", "S"],
97+
// ["A", "D", "E", "E"]
98+
// ], "ABCB") // false
99+
100+
// x = exist(
101+
// [
102+
// ["a", "b"],
103+
// ["c", "d"]
104+
// ], "abcd") // false
105+
106+
// x = exist(
107+
// [
108+
// ["a", "a", "a", "a"],
109+
// ["a", "a", "a", "a"],
110+
// ["a", "a", "a", "a"]
111+
// ], "aaaaaaaaaaab") // false
112+
113+
x = exist(
114+
[
115+
["C", "A", "A"],
116+
["A", "A", "A"],
117+
["B", "C", "D"]
118+
], "AAB") // true
119+
120+
121+
console.log("Result")
122+
console.log(x)

79/solution.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
const exist = (board, word) => {
7+
let visit = new Set();
8+
9+
const expand = (r, c, i) => {
10+
if (i === word.length) return true;
11+
12+
if (
13+
r === board.length ||
14+
c === board[0].length ||
15+
r < 0 ||
16+
c < 0 ||
17+
visit.has(`${r}${c}`) ||
18+
word[i] !== board[r][c]
19+
) return false;
20+
21+
visit.add(`${r}${c}`);
22+
i++
23+
24+
let res = expand(r, c + 1, i) ||
25+
expand(r + 1, c, i) ||
26+
expand(r, c - 1, i) ||
27+
expand(r - 1, c, i);
28+
29+
visit.delete(`${r}${c}`);
30+
31+
return res;
32+
}
33+
34+
for (let r = 0; r < board.length; r++) {
35+
for (let c = 0; c < board[r].length; c++) {
36+
if (word[0] === board[r][c]) {
37+
if (expand(r, c, 0)) return true;
38+
}
39+
}
40+
}
41+
42+
return false;
43+
};

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- [57. Insert Interval](./57/)
3535
- [73. Set Matrix Zeroes](./73/)
3636
- [76. Minimum Window Substring](./76/)
37+
- [79. Word Search](./79/)
3738
- [83. Remove Duplicates from Sorted List](./83/)
3839
- [121. Best Time to Buy and Sell Stock](./121/)
3940
- [128. Longest Consecutive Sequence](./128/)
@@ -76,6 +77,7 @@
7677
- [1791. Find Center of Star Graph](./1791/)
7778
- [1909. Remove One Element to Make the Array Strictly Increasing](./1909/)
7879
- [1971. Find if Path Exists in Graph](./1971/)
80+
- [2090. K Radius Subarray Averages](./2090/)
7981

8082
---
8183

@@ -119,7 +121,7 @@ Batch create:
119121
NOTE: JS IS HERE
120122
-->
121123
```ssh
122-
chapter=48 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
124+
chapter=79 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
123125
```
124126
> then you can use `x` for quick debug.
125127

0 commit comments

Comments
 (0)