Skip to content

Commit 976d5cc

Browse files
committed
Sync LeetCode submission Runtime - 295 ms (81.88%), Memory - 28.7 MB (44.11%)
1 parent 2e91e9f commit 976d5cc

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

2690-house-robber-iv/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he <strong>refuses to steal from adjacent homes</strong>.</p>
2+
3+
<p>The <strong>capability</strong> of the robber is the maximum amount of money he steals from one house of all the houses he robbed.</p>
4+
5+
<p>You are given an integer array <code>nums</code> representing how much money is stashed in each house. More formally, the <code>i<sup>th</sup></code> house from the left has <code>nums[i]</code> dollars.</p>
6+
7+
<p>You are also given an integer <code>k</code>, representing the <strong>minimum</strong> number of houses the robber will steal from. It is always possible to steal at least <code>k</code> houses.</p>
8+
9+
<p>Return <em>the <strong>minimum</strong> capability of the robber out of all the possible ways to steal at least </em><code>k</code><em> houses</em>.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [2,3,5,9], k = 2
16+
<strong>Output:</strong> 5
17+
<strong>Explanation:</strong>
18+
There are three ways to rob at least 2 houses:
19+
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
20+
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
21+
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
22+
Therefore, we return min(5, 9, 9) = 5.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> nums = [2,7,9,3,1], k = 2
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
38+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
39+
<li><code>1 &lt;= k &lt;= (nums.length + 1)/2</code></li>
40+
</ul>

2690-house-robber-iv/solution.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Approach: Binary Search
2+
3+
# Time: O(n log n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minCapability(self, nums: List[int], k: int) -> int:
8+
min_reward, max_reward = 1, max(nums)
9+
total_houses = len(nums)
10+
11+
while min_reward < max_reward:
12+
mid_reward = (min_reward + max_reward) // 2
13+
possible_thefts = 0
14+
15+
idx = 0
16+
while idx < total_houses:
17+
if nums[idx] <= mid_reward:
18+
possible_thefts += 1
19+
idx += 2 # Skip next house
20+
else:
21+
idx += 1
22+
23+
if possible_thefts >= k:
24+
max_reward = mid_reward
25+
else:
26+
min_reward = mid_reward + 1
27+
28+
return min_reward

0 commit comments

Comments
 (0)