Skip to content

Commit 2e21d2f

Browse files
committed
change hitcounter
1 parent effb56a commit 2e21d2f

File tree

2 files changed

+16
-72
lines changed

2 files changed

+16
-72
lines changed

src/main.ts

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,5 @@
11
import { printTree, TreeNode } from "./bst";
2+
import { HitCounterDBG } from "./scratch_structure/hit-counter";
23

34

4-
function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
5-
if (!root) {
6-
return null;
7-
}
8-
if (key < root.val) {
9-
root.left = deleteNode(root.left, key);
10-
}
11-
else if (key > root.val) {
12-
root.right = deleteNode(root.right, key);
13-
}
14-
else {
15-
if (!root.left) {
16-
return root.right;
17-
}
18-
else if (!root.right) {
19-
return root.left;
20-
}
21-
22-
let minNode = root.right;
23-
while (minNode.left) {
24-
minNode = minNode.left;
25-
}
26-
root.val = minNode.val;
27-
root.right = deleteNode(root.right, minNode.val);
28-
}
29-
return root;
30-
}
31-
32-
function deleteNodeDBG() {
33-
const tests = [
34-
{
35-
input: { root: new TreeNode(5, new TreeNode(3, new TreeNode(2), new TreeNode(4)), new TreeNode(6, null, new TreeNode(7))), key: 3 },
36-
result: new TreeNode(5, new TreeNode(4, new TreeNode(2)), new TreeNode(6, null, new TreeNode(7)))
37-
},
38-
{
39-
input: { root: new TreeNode(5, new TreeNode(3), new TreeNode(6, null, new TreeNode(7))), key: 5 },
40-
result: new TreeNode(6, new TreeNode(3), new TreeNode(7))
41-
}
42-
];
43-
44-
tests.forEach((test, index) => {
45-
console.log(`init tree`)
46-
printTree(test.input.root)
47-
const result = deleteNode(test.input.root, test.input.key);
48-
const success = JSON.stringify(result) === JSON.stringify(test.result);
49-
if (success) {
50-
console.log(`result`)
51-
printTree(test.input.root)
52-
console.log(`${index} success`);
53-
} else {
54-
console.log(`${index} fail`);
55-
console.log(`expected ${test.result}`);
56-
console.log(`got ${result}`);
57-
}
58-
console.log(`-------------------`)
59-
});
60-
}
61-
5+
HitCounterDBG()

src/scratch_structure/hit-counter.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ class HitCounter {
1717
while (this.hits.length > 0 && this.hits[0][0] <= timestamp - 300) {
1818
this.hits.shift();
1919
}
20-
let totalHits = 0;
21-
for (const [time, count] of this.hits) {
22-
totalHits += count;
23-
}
24-
return totalHits;
20+
return this.hits.reduce((acc, hit) => acc += hit[1], 0);
2521
}
2622
}
2723

28-
29-
const hitCounter = new HitCounter();
30-
hitCounter.hit(1);
31-
hitCounter.hit(2);
32-
hitCounter.hit(3);
33-
console.log(hitCounter.getHits(4)); // Ожидаемый результат: 3
34-
hitCounter.hit(300);
35-
console.log(hitCounter.getHits(300)); // Ожидаемый результат: 4
36-
console.log(hitCounter.getHits(301)); // Ожидаемый результат: 3
24+
export function HitCounterDBG() {
25+
const hitCounter = new HitCounter();
26+
hitCounter.hit(1);
27+
hitCounter.hit(2);
28+
hitCounter.hit(3);
29+
// Ожидаемый результат: 3
30+
console.log(hitCounter.getHits(4));
31+
hitCounter.hit(4);
32+
// Ожидаемый результат: 4
33+
console.log(hitCounter.getHits(300));
34+
// Ожидаемый результат: 3
35+
console.log(hitCounter.getHits(301));
36+
}

0 commit comments

Comments
 (0)