We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1cab7c9 commit 5b0dfefCopy full SHA for 5b0dfef
house-robber-ii/yhkee0404.scala
@@ -0,0 +1,17 @@
1
+object Solution {
2
+ def rob(nums: Array[Int]): Int = {
3
+ if (nums.length == 1) {
4
+ return nums(0)
5
+ }
6
+ val dp = Array.fill(nums.length)(Array.fill(2)(0)) // T(n) = S(n) = O(n)
7
+ dp(0)(1) = nums(0)
8
+ dp(1)(0) = nums(1)
9
+ dp(1)(1) = nums(0)
10
+ for (i <- 2 until nums.length) {
11
+ for (j <- 0 to 1) {
12
+ dp(i)(j) = Math.max(dp(i - 1)(j), dp(i - 2)(j) + nums(i))
13
14
15
+ Math.max(dp(nums.length - 2)(1), dp(nums.length - 1)(0))
16
17
+}
0 commit comments