-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2560HouseRobberIV.java
More file actions
37 lines (34 loc) · 1.22 KB
/
Copy path_2560HouseRobberIV.java
File metadata and controls
37 lines (34 loc) · 1.22 KB
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
package com.heatwave.leetcode.contest.weekly._20230205_331;
public class _2560HouseRobberIV {
static class Solution {
// From https://leetcode.cn/problems/house-robber-iv/solution/er-fen-da-an-dp-by-endlesscheng-m558/
public int minCapability(int[] nums, int k) {
int left = 0, right = getMaxFromArray(nums);
while (left + 1 < right) {
int mid = left + ((right - left) >>> 1); // binary search
int f0 = 0, f1 = 0; // DP
for (int x : nums)
if (x > mid) f0 = f1;
else {
int tmp = f1;
f1 = Math.max(f1, f0 + 1);
f0 = tmp;
}
if (f1 >= k) right = mid;
else left = mid;
}
return right;
}
private int getMaxFromArray(int[] nums) {
int max = Integer.MIN_VALUE;
for (int num : nums) {
max = Math.max(num, max);
}
return max;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
solution.minCapability(new int[]{2, 3, 5, 9}, 2);
}
}