-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path279.完全平方数.js
74 lines (60 loc) · 1.02 KB
/
279.完全平方数.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* @lc app=leetcode.cn id=279 lang=javascript
*
* [279] 完全平方数
*/
// @lc code=start
/**
* @param {number} n
* @return {number}
*/
var numSquares = function(n) {
// BFS
let q = [n]
let s = new Set(q)
let index = 0
while(true){
let nq = []
index++
while(q.length){
let on = q.shift()
for(let i=0;on-i*i>=0;i++){
let nn = on-i*i
console.log(nn)
if(nn === 0)return index
if(!s.has(nn)){
s.add(nn)
nq.push(nn)
}
// console.log(nq)
}
}
q = nq
}
//dp
// return new Array(n+1).fill(0).map((v,i,a)=>{
// a[i] = i;
// for(let j=1;i-j*j>=0;j++)a[i] = Math.min(a[i],a[i-j*j]+1)
// return a[i]
// })[n]
// let q = [n]
// while(q.length){
// let res = []
// let t = q.shift()
// for(let i = 1;t-i*i>0;i++){
// t = t-i*i
// if(t){
// q.push(t)
// res.push(t)
// console.log(res)
// }else{
// console.log(res)
// return res.length
// }
// }
// }
};
// @lc code=end
let N = 12
let res = numSquares(N)
console.log(res)