forked from dylansun/leetcode-cn-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo964.scala
37 lines (34 loc) · 799 Bytes
/
No964.scala
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
/**
* Created by lilisun on 3/5/19.
*/
object No964 {
var memo = scala.collection.mutable.HashMap[String, Int]()
var x = 0
def leastOpsExpressTarget(x: Int, target: Int): Int = {
this.x = x
val ans = dp(0, target) - 1
this.memo.clear()
ans
}
def dp(i: Int, target: Int):Int = {
val code = "" + i + "#" + target
if(memo.contains(code))
return memo(code)
var ans = 0
if (target == 0) {
ans = 0
} else if (target == 1) {
ans = cost(i)
} else if (i >= 39) {
ans = target + 1
} else {
val t = target / x
val r = target % x
ans = Math.min(r * cost(i) + dp(i+1, t),
(x-r) * cost(i) + dp(i+1, t+1))
}
memo.put(code, ans)
return ans
}
def cost(i: Int): Int = if(i > 0) i else 2
}