LeetCode solution summary 2560. Robbery IV

 Directory link:

Likou Programming Questions-Solution Summary_Sharing + Recording-CSDN Blog

GitHub synchronous question brushing project:

https://github.com/September26/java-algorithms

Original title link:

LeetCode official website - the technology growth platform loved by geeks around the world


describe:

There is a continuous row of houses along the street. There is a certain amount of cash hidden in each house. Now a thief plans to steal cash from these homes.

Thieves will not be able to steal from neighboring houses because they have interconnected anti-theft systems   .

A thief's  stealing power is defined as the maximum amount  of money he can steal from a single house during the theft process   .

You are given an array of integers  nums representing the amount of cash stored in each house. Formally, the first  i house from the left contains  nums[i] U.S. dollars.

You are also given an integer  that represents the minimumk number of houses  that a thief will steal   . A thief can always steal at least   one house.k

Returns the thief's  minimum  stealing ability.

Example 1:

Input: nums = [2,3,5,9], k = 2
 Output: 5
 Explanation: 
The thief steals at least 2 houses, there are 3 ways: 
- Steal the houses at index 0 and 2, the stealing ability is max( nums[0], nums[2]) = 5. 
- Steal the houses at index 0 and 3, the stealing ability is max(nums[0], nums[3]) = 9. 
- Steal the houses at subscripts 1 and 3, the stealing ability is max(nums[1], nums[3]) = 9. 
Therefore, min(5, 9, 9) = 5 is returned.

Example 2:

Input: nums = [2,7,9,3,1], k = 2
 Output: 2
 Explanation: There are 7 ways to steal. The minimum stealing capability corresponds to stealing houses at subscripts 0 and 4. Return max(nums[0], nums[4]) = 2.

hint:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 1 <= k <= (nums.length + 1)/2

Problem-solving ideas:

* Problem-solving ideas:

* Binary search method, first find the middle and see if the middle supports k requirements.

* To determine whether K is supported, we can try to find out the maximum number of ones smaller than middle within the range of nums, such as count.

* If count>=k, it means that middle is supported, and upper is updated; otherwise, lower is updated.

Code:

class Solution2560
{
public:
    int minCapability(vector<int> &nums, int k)
    {
        int maxValue = *max_element(nums.begin(), nums.end());
        int minValue = *min_element(nums.begin(), nums.end());
        int abs = maxValue;
        while (minValue <= maxValue)
        {
            int middle = (minValue + maxValue) / 2;
            bool visted = false;
            int count = 0;
            for (int num : nums)
            {
                if (num <= middle && !visted)
                {
                    count++;
                    visted = true;
                }
                else
                {
                    visted = false;
                }
            }
            if (count >= k)
            {
                abs = middle;
                maxValue = middle - 1;
            }
            else
            {
                minValue = middle + 1;
            }
        }
        return abs;
    }
};

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/133045517