-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2560-house-robber-iv.py
More file actions
33 lines (29 loc) · 767 Bytes
/
2560-house-robber-iv.py
File metadata and controls
33 lines (29 loc) · 767 Bytes
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
# time complexity: O(nlogm)
# space complexity: O(1)
from typing import List
class Solution:
def minCapability(self, nums: List[int], k: int) -> int:
left = 1
right = max(nums)
n = len(nums)
while left < right:
mid = left + (right - left) // 2
thefts = 0
i = 0
while i < n:
if nums[i] <= mid:
thefts += 1
i += 2
else:
i += 1
if thefts >= k:
right = mid
else:
left = mid + 1
return left
nums = [2, 3, 5, 9]
k = 2
print(Solution().minCapability(nums, k))
nums = [2, 7, 9, 3, 1]
k = 2
print(Solution().minCapability(nums, k))