Skip to content

Commit 7e72967

Browse files
committed
feat: leetcode contest 300
1 parent 6b25fc9 commit 7e72967

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} key
3+
* @param {string} message
4+
* @return {string}
5+
*/
6+
const decodeMessage = function (key, message) {
7+
const mp = {}; let c = 0
8+
for (const ch of key) {
9+
if (ch === ' ') continue
10+
if (mp[ch] == null) mp[ch] = String.fromCharCode((c++) + 'a'.charCodeAt(0))
11+
}
12+
return message.split('').map(x => {
13+
if (x === ' ') return x
14+
return mp[x]
15+
}).join('')
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} delay
4+
* @param {number} forget
5+
* @return {number}
6+
*/
7+
const peopleAwareOfSecret = function (n, d, f) {
8+
const a = [[1, 0]]; let ans = 1; const P = 1e9 + 7
9+
for (let i = 1; i < n; i++) {
10+
while (a.length && i - a[0][1] >= f) ans = (ans - a[0][0] + P) % P, a.shift()
11+
let j = a.length - 1
12+
while (j >= 0 && a[j][1] > i - d) j--
13+
let tmp = 0
14+
for (let k = j; k >= 0; k--) tmp = (tmp + a[k][0]) % P
15+
// console.log(i, a, ans)
16+
a.push([tmp, i]), ans = (ans + tmp) % P
17+
}
18+
return ans
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const countPaths = function (g) {
6+
const dx = [0, 0, 1, -1]; const dy = [1, -1, 0, 0]
7+
const m = g.length; const n = g[0].length
8+
const mp = {}; const P = 1e9 + 7
9+
const dfs = (i, j) => {
10+
if (mp[`${i}_${j}`] != null) return mp[`${i}_${j}`]
11+
let ans = 1
12+
for (let k = 0; k < 4; k++) {
13+
const x = i + dx[k]; const y = j + dy[k]
14+
if (x >= 0 && x < m && y >= 0 && y < n && g[i][j] < g[x][y]) ans = (ans + dfs(x, y)) % P
15+
}
16+
return mp[`${i}_${j}`] = ans
17+
}
18+
let ans = 0
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
ans = (ans + dfs(i, j)) % P
22+
}
23+
}
24+
return ans
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {number} m
10+
* @param {number} n
11+
* @param {ListNode} head
12+
* @return {number[][]}
13+
*/
14+
const spiralMatrix = function (m, n, h) {
15+
const ans = Array(m).fill(0).map(() => Array(n).fill(-1))
16+
let i = 0; let j = 0
17+
const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; let cd = 0
18+
while (h) {
19+
ans[i][j] = h.val
20+
let ni = i + dx[cd]; let nj = j + dy[cd]
21+
if (ni < 0 || ni >= m || nj < 0 || nj >= n || ans[ni][nj] !== -1) cd = (cd + 1) % 4
22+
ni = i + dx[cd], nj = j + dy[cd]
23+
i = ni, j = nj
24+
h = h.next
25+
}
26+
return ans
27+
}

0 commit comments

Comments
 (0)