Skip to content

Commit 19a05e4

Browse files
committed
https://leetcode.cn/problems/rle-iterator
1 parent c11a7d5 commit 19a05e4

File tree

3 files changed

+87
-54
lines changed

3 files changed

+87
-54
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,8 @@ https://leetcode.cn/problems/generate-random-point-in-a-circle/
650650

651651
https://leetcode.cn/problems/design-an-atm-machine/
652652

653+
https://leetcode.cn/problems/rle-iterator
654+
653655
#### 安装教程
654656

655657
1. 安装`deno`

design-an-atm-machine/index.ts

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
1-
export default function ATM() {
2-
const moneyStore = new Map([
3-
[20, 0],
4-
[50, 0],
5-
[100, 0],
6-
[200, 0],
7-
[500, 0],
8-
]);
9-
const indexToMoney = [20, 50, 100, 200, 500];
10-
return {
11-
deposit(banknotesCount: number[]): void {
12-
for (const [index, count] of banknotesCount.entries()) {
13-
const key = indexToMoney[index];
14-
if (!moneyStore.has(key)) {
15-
throw new Error("moneyStore Not Found");
16-
}
17-
moneyStore.set(key, (moneyStore.get(key) ?? 0) + count);
18-
}
19-
},
20-
21-
withdraw(amount: number): number[] {
22-
const delta = Object.fromEntries([
23-
[20, 0],
24-
[50, 0],
25-
[100, 0],
26-
[200, 0],
27-
[500, 0],
28-
]);
29-
const changedStore = new Map(moneyStore);
30-
for (const money of [500, 200, 100, 50, 20]) {
31-
if ((changedStore.get(money) ?? 0) > 0 && amount >= money) {
32-
const d = Math.min(
33-
Math.floor(amount / money),
34-
changedStore.get(money) ?? 0
35-
);
36-
amount -= money * d;
37-
delta[money] += d;
38-
changedStore.set(
39-
money,
40-
-d + (changedStore.get(money) ?? 0)
41-
);
42-
}
43-
}
44-
if (amount > 0) {
45-
return [-1];
46-
} else {
47-
changedStore.forEach((value, key) =>
48-
moneyStore.set(key, value)
49-
);
50-
return Object.values(delta);
51-
}
52-
},
53-
};
54-
}
1+
export default function ATM() {
2+
const moneyStore = new Map([
3+
[20, 0],
4+
[50, 0],
5+
[100, 0],
6+
[200, 0],
7+
[500, 0],
8+
]);
9+
const indexToMoney = [20, 50, 100, 200, 500];
10+
return {
11+
deposit(banknotesCount: number[]): void {
12+
for (const [index, count] of banknotesCount.entries()) {
13+
const key = indexToMoney[index];
14+
if (!moneyStore.has(key)) {
15+
throw new Error("moneyStore Not Found");
16+
}
17+
moneyStore.set(key, (moneyStore.get(key) ?? 0) + count);
18+
}
19+
},
20+
21+
withdraw(amount: number): number[] {
22+
const delta = Object.fromEntries([
23+
[20, 0],
24+
[50, 0],
25+
[100, 0],
26+
[200, 0],
27+
[500, 0],
28+
]);
29+
const changedStore = new Map(moneyStore);
30+
for (const money of [500, 200, 100, 50, 20]) {
31+
if ((changedStore.get(money) ?? 0) > 0 && amount >= money) {
32+
const d = Math.min(
33+
Math.floor(amount / money),
34+
changedStore.get(money) ?? 0,
35+
);
36+
amount -= money * d;
37+
delta[money] += d;
38+
changedStore.set(
39+
money,
40+
-d + (changedStore.get(money) ?? 0),
41+
);
42+
}
43+
}
44+
if (amount > 0) {
45+
return [-1];
46+
} else {
47+
changedStore.forEach((value, key) =>
48+
moneyStore.set(key, value)
49+
);
50+
return Object.values(delta);
51+
}
52+
},
53+
};
54+
}

rle-iterator/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default function RLEIterator(encoding: number[]) {
2+
const generator = (function* () {
3+
for (let i = 0; i < encoding.length; i += 2) {
4+
yield { count: encoding[i], value: encoding[i + 1] };
5+
}
6+
})();
7+
8+
let count = 0;
9+
let value = 0;
10+
let done = false;
11+
return {
12+
next(n: number): number {
13+
if (done) return -1;
14+
while (n > 0 && !done) {
15+
if (count) {
16+
const delta = Math.min(count, n);
17+
n -= delta;
18+
count -= delta;
19+
if (n === 0) return value;
20+
} else {
21+
const result = generator.next();
22+
done = !!result.done;
23+
if (result.done) return -1;
24+
value = result.value.value;
25+
count = result.value.count;
26+
}
27+
}
28+
return -1;
29+
},
30+
};
31+
}

0 commit comments

Comments
 (0)