Package g2501_2600.s2560_house_robber_iv
Class Solution
java.lang.Object
g2501_2600.s2560_house_robber_iv.Solution
2560 - House Robber IV.<p>Medium</p>
<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>
<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>
<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>
<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>
<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>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [2,3,5,9], k = 2</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong> There are three ways to rob at least 2 houses:</p>
<ul>
<li>
<p>Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.</p>
</li>
<li>
<p>Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.</p>
</li>
<li>
<p>Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.</p>
</li>
</ul>
<p>Therefore, we return min(5, 9, 9) = 5.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [2,7,9,3,1], k = 2</p>
<p><strong>Output:</strong> 2</p>
<p><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.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= (nums.length + 1)/2</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minCapability
public int minCapability(int[] nums, int k)
-