Skip to content

Commit f1b79ce

Browse files
committed
feat: add ts solution to lc problems: No.2325~2328
* No.2325.Decode the Message * No.2326.Spiral Matrix IV * No.2327.Number of People Aware of a Secret * No.2328.Number of Increasing Paths in a Grid
1 parent 4a67763 commit f1b79ce

File tree

12 files changed

+272
-4
lines changed

12 files changed

+272
-4
lines changed

solution/2300-2399/2325.Decode the Message/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,22 @@ func decodeMessage(key string, message string) string {
154154
### **TypeScript**
155155

156156
```ts
157-
157+
function decodeMessage(key: string, message: string): string {
158+
let decodeMap = new Map();
159+
const m = key.length, n = 26;
160+
for (let i = 0, j = 0; i < m; i++) {
161+
let char = key.charAt(i);
162+
if (char != ' ' && !decodeMap.has(char)) {
163+
decodeMap.set(char, String.fromCharCode(j + 97));
164+
j++;
165+
}
166+
}
167+
let ans = [];
168+
for (let char of message) {
169+
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
170+
}
171+
return ans.join('');
172+
};
158173
```
159174

160175
### **...**

solution/2300-2399/2325.Decode the Message/README_EN.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,22 @@ func decodeMessage(key string, message string) string {
140140
### **TypeScript**
141141

142142
```ts
143-
143+
function decodeMessage(key: string, message: string): string {
144+
let decodeMap = new Map();
145+
const m = key.length, n = 26;
146+
for (let i = 0, j = 0; i < m; i++) {
147+
let char = key.charAt(i);
148+
if (char != ' ' && !decodeMap.has(char)) {
149+
decodeMap.set(char, String.fromCharCode(j + 97));
150+
j++;
151+
}
152+
}
153+
let ans = [];
154+
for (let char of message) {
155+
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
156+
}
157+
return ans.join('');
158+
};
144159
```
145160

146161
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function decodeMessage(key: string, message: string): string {
2+
let decodeMap = new Map();
3+
const m = key.length, n = 26;
4+
for (let i = 0, j = 0; i < m; i++) {
5+
let char = key.charAt(i);
6+
if (char != ' ' && !decodeMap.has(char)) {
7+
decodeMap.set(char, String.fromCharCode(j + 97));
8+
j++;
9+
}
10+
}
11+
let ans = [];
12+
for (let char of message) {
13+
ans.push(char == ' ' ? ' ' : decodeMap.get(char));
14+
}
15+
return ans.join('');
16+
};

solution/2300-2399/2326.Spiral Matrix IV/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,35 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
206206
### **TypeScript**
207207

208208
```ts
209+
/**
210+
* Definition for singly-linked list.
211+
* class ListNode {
212+
* val: number
213+
* next: ListNode | null
214+
* constructor(val?: number, next?: ListNode | null) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.next = (next===undefined ? null : next)
217+
* }
218+
* }
219+
*/
209220

221+
function spiralMatrix(m: number, n: number, head: ListNode | null): number[][] {
222+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
223+
let ans = Array.from({ length: m }, v => new Array(n).fill(-1));
224+
let i = 0, j = 0, k = 0;
225+
while (head) {
226+
ans[i][j] = head.val;
227+
head = head.next;
228+
let x = i + dirs[k][0];
229+
let y = j + dirs[k][1];
230+
if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || ans[x][y] != -1) {
231+
k = (k + 1) % 4;
232+
}
233+
i = i + dirs[k][0];
234+
j = j + dirs[k][1];
235+
}
236+
return ans;
237+
};
210238
```
211239

212240
### **...**

solution/2300-2399/2326.Spiral Matrix IV/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,35 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
198198
### **TypeScript**
199199

200200
```ts
201+
/**
202+
* Definition for singly-linked list.
203+
* class ListNode {
204+
* val: number
205+
* next: ListNode | null
206+
* constructor(val?: number, next?: ListNode | null) {
207+
* this.val = (val===undefined ? 0 : val)
208+
* this.next = (next===undefined ? null : next)
209+
* }
210+
* }
211+
*/
201212

213+
function spiralMatrix(m: number, n: number, head: ListNode | null): number[][] {
214+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
215+
let ans = Array.from({ length: m }, v => new Array(n).fill(-1));
216+
let i = 0, j = 0, k = 0;
217+
while (head) {
218+
ans[i][j] = head.val;
219+
head = head.next;
220+
let x = i + dirs[k][0];
221+
let y = j + dirs[k][1];
222+
if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || ans[x][y] != -1) {
223+
k = (k + 1) % 4;
224+
}
225+
i = i + dirs[k][0];
226+
j = j + dirs[k][1];
227+
}
228+
return ans;
229+
};
202230
```
203231

204232
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function spiralMatrix(m: number, n: number, head: ListNode | null): number[][] {
14+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
15+
let ans = Array.from({ length: m }, v => new Array(n).fill(-1));
16+
let i = 0, j = 0, k = 0;
17+
while (head) {
18+
ans[i][j] = head.val;
19+
head = head.next;
20+
let x = i + dirs[k][0];
21+
let y = j + dirs[k][1];
22+
if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || ans[x][y] != -1) {
23+
k = (k + 1) % 4;
24+
}
25+
i = i + dirs[k][0];
26+
j = j + dirs[k][1];
27+
}
28+
return ans;
29+
};

solution/2300-2399/2327.Number of People Aware of a Secret/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,27 @@ func peopleAwareOfSecret(n int, delay int, forget int) int {
176176
### **TypeScript**
177177

178178
```ts
179-
179+
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
180+
let dp = new Array(n + 1).fill(0n);
181+
dp[1] = 1n;
182+
for (let i = 2; i <= n; i++) {
183+
let pre = 0n;
184+
for (let j = i - forget + 1; j <= i - delay; j++) {
185+
if (j > 0) {
186+
pre += dp[j];
187+
}
188+
}
189+
dp[i] = pre;
190+
}
191+
let pre = 0n;
192+
let i = n + 1;
193+
for (let j = i - forget; j < i; j++) {
194+
if (j > 0) {
195+
pre += dp[j];
196+
}
197+
}
198+
return Number(pre % BigInt(10 ** 9 + 7));
199+
};
180200
```
181201

182202
### **...**

solution/2300-2399/2327.Number of People Aware of a Secret/README_EN.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,27 @@ func peopleAwareOfSecret(n int, delay int, forget int) int {
164164
### **TypeScript**
165165

166166
```ts
167-
167+
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
168+
let dp = new Array(n + 1).fill(0n);
169+
dp[1] = 1n;
170+
for (let i = 2; i <= n; i++) {
171+
let pre = 0n;
172+
for (let j = i - forget + 1; j <= i - delay; j++) {
173+
if (j > 0) {
174+
pre += dp[j];
175+
}
176+
}
177+
dp[i] = pre;
178+
}
179+
let pre = 0n;
180+
let i = n + 1;
181+
for (let j = i - forget; j < i; j++) {
182+
if (j > 0) {
183+
pre += dp[j];
184+
}
185+
}
186+
return Number(pre % BigInt(10 ** 9 + 7));
187+
};
168188
```
169189

170190
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
2+
let dp = new Array(n + 1).fill(0n);
3+
dp[1] = 1n;
4+
for (let i = 2; i <= n; i++) {
5+
let pre = 0n;
6+
for (let j = i - forget + 1; j <= i - delay; j++) {
7+
if (j > 0) {
8+
pre += dp[j];
9+
}
10+
}
11+
dp[i] = pre;
12+
}
13+
let pre = 0n;
14+
let i = n + 1;
15+
for (let j = i - forget; j < i; j++) {
16+
if (j > 0) {
17+
pre += dp[j];
18+
}
19+
}
20+
return Number(pre % BigInt(10 ** 9 + 7));
21+
};

solution/2300-2399/2328.Number of Increasing Paths in a Grid/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,32 @@ func countPaths(grid [][]int) int {
198198
### **TypeScript**
199199

200200
```ts
201+
function countPaths(grid: number[][]): number {
202+
const mod = BigInt(10 ** 9 + 7);
203+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
204+
const m = grid.length, n = grid[0].length;
205+
const dp = Array.from({ length: m }, v => new Array(n).fill(-1n));
206+
207+
function dfs (x, y) {
208+
if (dp[x][y] != -1) return dp[x][y];
209+
let count = 1n;
210+
for (let [dx, dy] of dirs) {
211+
let i = x + dx, j = y + dy;
212+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] <= grid[x][y]) continue;
213+
count = (count + dfs(i, j)) % mod;
214+
}
215+
dp[x][y] = count;
216+
return count;
217+
}
201218

219+
let sum = 0n;
220+
for (let i = 0; i < m; i++) {
221+
for (let j = 0; j < n; j++) {
222+
sum = (sum + dfs(i, j)) % mod;
223+
}
224+
}
225+
return Number(sum);
226+
};
202227
```
203228

204229
### **...**

solution/2300-2399/2328.Number of Increasing Paths in a Grid/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,32 @@ func countPaths(grid [][]int) int {
184184
### **TypeScript**
185185

186186
```ts
187+
function countPaths(grid: number[][]): number {
188+
const mod = BigInt(10 ** 9 + 7);
189+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
190+
const m = grid.length, n = grid[0].length;
191+
const dp = Array.from({ length: m }, v => new Array(n).fill(-1n));
192+
193+
function dfs (x, y) {
194+
if (dp[x][y] != -1) return dp[x][y];
195+
let count = 1n;
196+
for (let [dx, dy] of dirs) {
197+
let i = x + dx, j = y + dy;
198+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] <= grid[x][y]) continue;
199+
count = (count + dfs(i, j)) % mod;
200+
}
201+
dp[x][y] = count;
202+
return count;
203+
}
187204

205+
let sum = 0n;
206+
for (let i = 0; i < m; i++) {
207+
for (let j = 0; j < n; j++) {
208+
sum = (sum + dfs(i, j)) % mod;
209+
}
210+
}
211+
return Number(sum);
212+
};
188213
```
189214

190215
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function countPaths(grid: number[][]): number {
2+
const mod = BigInt(10 ** 9 + 7);
3+
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]];
4+
const m = grid.length, n = grid[0].length;
5+
const dp = Array.from({ length: m }, v => new Array(n).fill(-1n));
6+
7+
function dfs (x, y) {
8+
if (dp[x][y] != -1) return dp[x][y];
9+
let count = 1n;
10+
for (let [dx, dy] of dirs) {
11+
let i = x + dx, j = y + dy;
12+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] <= grid[x][y]) continue;
13+
count = (count + dfs(i, j)) % mod;
14+
}
15+
dp[x][y] = count;
16+
return count;
17+
}
18+
19+
let sum = 0n;
20+
for (let i = 0; i < m; i++) {
21+
for (let j = 0; j < n; j++) {
22+
sum = (sum + dfs(i, j)) % mod;
23+
}
24+
}
25+
return Number(sum);
26+
};

0 commit comments

Comments
 (0)