Skip to content

File tree

9 files changed

+452
-3
lines changed

9 files changed

+452
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/sliding-window-maximum/
14+
15+
https://leetcode.cn/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
16+
17+
https://leetcode.cn/problems/solve-the-equation/
18+
1319
https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
1420

1521
https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default function maxSlidingWindow(nums: number[], k: number): number[] {
2+
if (k === 0 || nums.length === 0) return [];
3+
4+
const n = nums.length;
5+
const prefixMax: number[] = new Array(n).fill(0);
6+
const suffixMax: number[] = new Array(n).fill(0);
7+
for (let i = 0; i < n; i++) {
8+
if (i % k === 0) {
9+
prefixMax[i] = nums[i];
10+
} else {
11+
prefixMax[i] = Math.max(prefixMax[i - 1], nums[i]);
12+
}
13+
}
14+
for (let i = n - 1; i >= 0; --i) {
15+
if (i === n || (i + 1) % k === 0) {
16+
suffixMax[i] = nums[i];
17+
} else {
18+
suffixMax[i] = Math.max(suffixMax[i + 1], nums[i]);
19+
}
20+
}
21+
const ans: number[] = [];
22+
for (let i = 0; i < n - k + 1; i++) {
23+
ans.push(Math.max(suffixMax[i], prefixMax[i + k - 1]));
24+
}
25+
return ans;
26+
}

powx-n/test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Deno.test("pow_x-n", () => {
3939
{ input: [0, -Infinity], output: Infinity },
4040
];
4141
examples.forEach(({ input, output }) => {
42-
assert(float64equals(output, pow_x_n(...input)));
42+
const result = pow_x_n(...input);
43+
assert(float64equals(output, result, 1e-10), `${output}!==${result} `);
4344
});
4445
});
4546
Deno.test("pow_bigint", () => {

qIsx9U/test.ts

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import { assert } from "../deps.ts";
2+
import MovingAverage from "./index.ts";
3+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
4+
import { float64equals } from "../utils/float64equals.ts";
5+
6+
Deno.test("moving-average-from-data-stream", () => {
7+
// MovingAverage;
8+
9+
const inputs: Array<[string[], number[][]]> = [
10+
[
11+
["MovingAverage", "next", "next", "next", "next"],
12+
[[3], [1], [10], [3], [5]],
13+
],
14+
[
15+
[
16+
"MovingAverage",
17+
"next",
18+
"next",
19+
"next",
20+
"next",
21+
"next",
22+
"next",
23+
"next",
24+
"next",
25+
"next",
26+
"next",
27+
"next",
28+
"next",
29+
],
30+
[
31+
[3],
32+
[1],
33+
[10],
34+
[3],
35+
[5],
36+
[1],
37+
[10],
38+
[3],
39+
[5],
40+
[1],
41+
[10],
42+
[3],
43+
[5],
44+
],
45+
],
46+
[
47+
[
48+
"MovingAverage",
49+
"next",
50+
"next",
51+
"next",
52+
"next",
53+
"next",
54+
"next",
55+
"next",
56+
"next",
57+
"next",
58+
"next",
59+
"next",
60+
"next",
61+
"next",
62+
"next",
63+
"next",
64+
"next",
65+
],
66+
[
67+
[13],
68+
[17],
69+
[10],
70+
[13],
71+
[5],
72+
[1],
73+
[10],
74+
[773],
75+
[5],
76+
[1],
77+
[7710],
78+
[3],
79+
[5],
80+
[1],
81+
[10],
82+
[73],
83+
[5],
84+
],
85+
],
86+
[
87+
[
88+
"MovingAverage",
89+
"next",
90+
"next",
91+
"next",
92+
"next",
93+
"next",
94+
"next",
95+
"next",
96+
"next",
97+
"next",
98+
"next",
99+
"next",
100+
"next",
101+
],
102+
[
103+
[13],
104+
[17],
105+
[107],
106+
[73],
107+
[5],
108+
[771],
109+
[710],
110+
[37],
111+
[5],
112+
[1],
113+
[7710],
114+
[3],
115+
[57],
116+
],
117+
],
118+
];
119+
const outputs = [
120+
[null, 1.0, 5.5, 4.66667, 6.0],
121+
[
122+
null,
123+
1.0,
124+
5.5,
125+
4.66667,
126+
6.0,
127+
3.0,
128+
5.33333,
129+
4.66667,
130+
6.0,
131+
3.0,
132+
5.33333,
133+
4.66667,
134+
6.0,
135+
],
136+
[
137+
null,
138+
17.0,
139+
13.5,
140+
13.33333,
141+
11.25,
142+
9.2,
143+
9.33333,
144+
118.42857,
145+
104.25,
146+
92.77778,
147+
854.5,
148+
777.09091,
149+
712.75,
150+
658.0,
151+
657.46154,
152+
662.30769,
153+
661.69231,
154+
],
155+
[
156+
null,
157+
17.0,
158+
62.0,
159+
65.66667,
160+
50.5,
161+
194.6,
162+
280.5,
163+
245.71429,
164+
215.625,
165+
191.77778,
166+
943.6,
167+
858.09091,
168+
791.33333,
169+
],
170+
];
171+
172+
inputs.forEach(([input, args], i) => {
173+
const out = outputs[i];
174+
175+
const fn = new Function(
176+
"MovingAverage",
177+
`
178+
179+
const res=[];
180+
${
181+
input
182+
.map(function (name, i) {
183+
if (i === 0) {
184+
return `
185+
const movingAverage =new MovingAverage(${args[i].join(", ")});
186+
187+
res.push(null);
188+
`;
189+
}
190+
return `res.push(movingAverage.${name}(${
191+
args[i].join(", ")
192+
}));`;
193+
})
194+
.join("\n")
195+
}
196+
return res;
197+
`,
198+
);
199+
// console.log(fn)
200+
// console.log(fn.toString());
201+
const results = fn(MovingAverage);
202+
// console.log(results, out);
203+
assert(Array.isArray(results));
204+
results.forEach((result, j) => {
205+
const expected = out[j];
206+
207+
if (typeof result === "number") {
208+
assert(typeof result === typeof expected);
209+
assert(
210+
float64equals(
211+
result,
212+
expected as number,
213+
7.142852039915146e-6,
214+
),
215+
`${result} !== ${expected}`,
216+
);
217+
} else if (result !== null) {
218+
assertEquals(result, expected);
219+
}
220+
});
221+
});
222+
});

sliding-window-maximum/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "../hua-dong-chuang-kou-de-zui-da-zhi-lcof/index.ts";

sliding-window-maximum/test.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import maxSlidingWindow from "./index.ts";
2+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
3+
4+
Deno.test("sliding-window-maximum", () => {
5+
const inputs = [
6+
[[1, 3, -1, -3, 5, 3, 6, 7], 3],
7+
[[1], 1],
8+
[[6], 1],
9+
10+
[
11+
[
12+
1,
13+
3,
14+
-1,
15+
-31,
16+
3,
17+
-1,
18+
-3,
19+
5,
20+
3,
21+
6,
22+
7,
23+
3,
24+
3,
25+
5,
26+
5,
27+
6,
28+
71,
29+
3,
30+
-1,
31+
-3,
32+
5,
33+
3,
34+
6,
35+
7,
36+
3,
37+
3,
38+
5,
39+
5,
40+
6,
41+
7,
42+
5,
43+
3,
44+
6,
45+
7,
46+
3,
47+
3,
48+
5,
49+
5,
50+
6,
51+
71,
52+
3,
53+
-1,
54+
-3,
55+
5,
56+
3,
57+
6,
58+
7,
59+
3,
60+
3,
61+
5,
62+
5,
63+
6,
64+
7,
65+
],
66+
16,
67+
],
68+
[[], 7],
69+
[[2], 0],
70+
] as Array<Parameters<typeof maxSlidingWindow>>;
71+
const outputs = [
72+
[3, 3, 5, 5, 6, 7],
73+
[1],
74+
[6],
75+
76+
[
77+
7,
78+
71,
79+
71,
80+
71,
81+
71,
82+
71,
83+
71,
84+
71,
85+
71,
86+
71,
87+
71,
88+
71,
89+
71,
90+
71,
91+
71,
92+
71,
93+
71,
94+
7,
95+
7,
96+
7,
97+
7,
98+
7,
99+
7,
100+
7,
101+
71,
102+
71,
103+
71,
104+
71,
105+
71,
106+
71,
107+
71,
108+
71,
109+
71,
110+
71,
111+
71,
112+
71,
113+
71,
114+
71,
115+
],
116+
[],
117+
[],
118+
];
119+
assertEquals(
120+
inputs.map(([input, k]) => maxSlidingWindow(input, k)),
121+
outputs,
122+
);
123+
});

0 commit comments

Comments
 (0)