Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9485836

Browse filesBrowse files
author
night
committed
more concise code
1 parent 9c85a3a commit 9485836
Copy full SHA for 9485836

File tree

1 file changed

+5
-10
lines changed
Filter options

1 file changed

+5
-10
lines changed

‎src/main/java/com/blankj/medium/_213/Solution.kt

Copy file name to clipboardExpand all lines: src/main/java/com/blankj/medium/_213/Solution.kt
+5-10Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,18 @@ import kotlin.math.max
66
class Solution {
77
fun rob(nums: IntArray): Int {
88
if (nums.isEmpty()) return 0
9-
if (nums.size == 1) {
10-
return nums[0];
11-
} else if (nums.size == 2) {
12-
return max(nums[0], nums[1]);
13-
}
9+
if (nums.size <= 2) return nums.max()
1410
return max(
1511
helper(nums, 0, nums.size - 2),
1612
helper(nums, 1, nums.size - 1)
1713
)
1814
}
19-
20-
fun helper(nums: IntArray, start: Int, end: Int): Int {
15+
private fun helper(nums: IntArray, start: Int, end: Int): Int {
2116
var first = nums[start]
22-
var second = max(nums[start], nums[end])
23-
for (i in start + 2..end) {
17+
var second = max(nums[start], nums[start + 1])
18+
for (i in start+2..end) {
2419
val temp = second
25-
second = max(second, first + nums[i])
20+
second = max(first + nums[i], second)
2621
first = temp
2722
}
2823
return second

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.