-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.kt
63 lines (58 loc) · 1.66 KB
/
Solution.kt
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
/**
* Created by Inno Fang on 2017/12/3.
*/
/**
* 586 / 586 test cases passed.
* Status: Accepted
* Runtime: 1112 ms
*/
class Solution {
fun numSquares(n: Int): Int {
val dp = (0..n + 1).toMutableList()
// perfect number that lower than n
val pn = (1..n).map { it * it }.takeWhile { it <= n }.toMutableList()
(1..n).forEach { i ->
pn.takeWhile { it <= i }.forEach { j ->
dp[i] = Math.min(dp[i], 1 + dp[i - j])
}
}
return dp[n]
}
}
/**
* 586 / 586 test cases passed.
* Status: Accepted
* Runtime: 494 ms
*/
class Solution2 {
fun numSquares(n: Int): Int {
val que = java.util.LinkedList<Pair<Int, Int>>()
val visited = MutableList(n) { false }
que.add(Pair(n, 0))
while (que.isNotEmpty()) {
val num = que.peek().first
val step = que.peek().second
que.poll()
for (i in (1..n)) {
val tmp = num - i * i
if (tmp < 0) break
if (tmp == 0) return step + 1
if (!visited[tmp]) {
que.add(Pair(tmp, step + 1))
visited[tmp] = true
}
}
}
// cannot find.
return -1
}
}
fun main(args: Array<String>) {
Solution2().numSquares(1).let(::println) // 1
Solution2().numSquares(4).let(::println) // 1
Solution2().numSquares(12).let(::println) // 3
Solution2().numSquares(13).let(::println) // 2
Solution2().numSquares(16).let(::println) // 1
Solution2().numSquares(24).let(::println) // 3
Solution2().numSquares(50).let(::println) // 2
}