-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution-bfs.ts
44 lines (35 loc) · 869 Bytes
/
solution-bfs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* @lc app=leetcode id=279 lang=javascript
*
* [279] Perfect Squares
*/
// @lc code=start
/**
* @param {number} n
* @return {number}
*/
const numSquares = (n: number): number => {
// * bfs ? ['92 ms', '90.63 %', '41.5 MB', '12.5 %']
// * double memory usage, not good
const queue: [number, number][] = [[n, 1]];
const visited: Record<number, boolean> = {};
while (queue.length) {
const [n, step] = queue.shift()!;
const sqrtN = n ** 0.5;
const intSqrtN = Math.floor(sqrtN);
// * found
if (sqrtN === intSqrtN) return step;
const nextStep = step + 1;
for (let i = intSqrtN; i > 0; i--) {
const rest = n - i ** 2;
if (!visited[rest]) {
queue.push([rest, nextStep]);
visited[rest] = true;
}
}
}
// * really never touched
return 0;
};
// @lc code=end
export { numSquares };