Skip to content

Commit ece29b3

Browse files
committed
322 & undone 76
1 parent ae9937a commit ece29b3

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

322.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
const minNums: number[] = [0];
3+
4+
coins.forEach((c) => (minNums[c] = 1));
5+
6+
let i = 1;
7+
while (i <= amount) {
8+
let minCount = Infinity;
9+
coins.forEach((c) => {
10+
if (i - c >= 0) {
11+
minCount = Math.min(minCount, minNums[i - c] + 1);
12+
}
13+
});
14+
minNums[i] = minCount;
15+
i++;
16+
}
17+
18+
return minNums[amount] === Infinity ? -1 : minNums[amount];
19+
}
20+
21+
const coins = [1, 2, 5],
22+
amount = 13;
23+
24+
console.log(coinChange(coins, amount));

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* Visit https://aka.ms/tsconfig.json to read more about this file */
44
/* Basic Options */
55
// "incremental": true, /* Enable incremental compilation */
6-
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
6+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
77
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
88
// "lib": [], /* Specify library files to be included in the compilation. */
99
// "allowJs": true, /* Allow javascript files to be compiled. */

undone-76.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function minWindow(s: string, t: string): string {
2+
const char2Indexes = new Map<string, number[]>();
3+
const ind2Char = {};
4+
5+
t.split("").forEach((c) =>
6+
char2Indexes.has(c) ? null : char2Indexes.set(c, [])
7+
);
8+
9+
s.split("").forEach((c, ind) => {
10+
char2Indexes.has(c) ? char2Indexes.get(c).push(ind) : null;
11+
ind2Char[ind] = c;
12+
});
13+
14+
const indexes = Object.keys(ind2Char)
15+
.map((k) => Number(k))
16+
.sort((a, b) => a - b);
17+
18+
const initInd: number[] = [];
19+
20+
for (let key of [...char2Indexes.keys()]) {
21+
if (char2Indexes.get(key).length === 0) return "";
22+
initInd.push(char2Indexes.get(key).shift());
23+
}
24+
25+
let i = Math.max(...initInd),
26+
j = Math.min(...initInd);
27+
let minLen = j - i;
28+
let minPair = [i, j];
29+
30+
while (1) {
31+
// 去除左边
32+
const toRemove = s[i];
33+
const nextInd = char2Indexes.get(toRemove).shift();
34+
if (!nextInd) return s.slice(minPair[0], minPair[1]);
35+
36+
// 向右滑, 得到valid的窗口
37+
}
38+
}

0 commit comments

Comments
 (0)