<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,5,9], k = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> 
There are three ways to rob at least 2 houses:
- Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5.
- Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9.
- Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9.
Therefore, we return min(5, 9, 9) = 5.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,7,9,3,1], k = 2
<strong>Output:</strong> 2
<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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= (nums.length + 1)/2</code></li>
</ul>

class Solution:
    def _getMaxRob1(self, nums: List[int], m: int) -> int:
        n = len(nums)
        curr = 0        
        # dp = [0]*n
        # for i in range(n):
        #     if i < 2 and nums[i] <= m:
        #         dp[i] = 1
        #     else:
        #         if nums[i] <= m:
        #             dp[i] = max(dp[i-1], dp[i-2]+1)
        #         else:
        #             dp[i] = dp[i-1]
        #     maxrob = max(maxrob, dp[i])
        
        # if n == 1:
        #     if nums[0] <= m:
        #         return 1
        # elif n == 2:
        #     if min(nums) <= m:
        #         return 1
        # else:
        prev = 0
        curr = 0
        for v in nums:
            prevprev = prev
            prev = curr
            if v <= m:
                curr = max(prev, prevprev + 1)
            else:
                curr = prev
            
        return curr
    
    def _getMaxRob2(self, nums: List[int], m: int) -> int:
        
        # This linear scan works
        # Let's say we have an array of [A1, A2, A3 .... An - 1], A1 <= mid, A2 <= mid, if we rob A1, we have options to rob rest of the houses from A3 to An-1, if we rob A2, we only have options from A4 to An-1, so A1 is always better than A2
        
        stole = 0
        maxrob = 0
        n = len(nums)
        
        for v in nums:
            if stole == 1:
                stole = 0
                continue
            else:
                if v <= m:
                    stole = 1
                    maxrob += 1
        
        return maxrob
    
    def minCapability(self, nums: List[int], k: int) -> int:
        # binary search
        
        l = min(nums)
        r = max(nums)
        
        while(l < r):
            m = (l+r) // 2
            
            maxrob = self._getMaxRob1(nums, m)
            # maxrob = self._getMaxRob2(nums, m)
        
            if maxrob >= k:
                r = m
            else:
                l = m+1
                
        return l
843