【力扣-每日一题】2560. 打家劫舍 IV

cpp 复制代码
class Solution {
public:
    bool check(vector<int> &nums,int max_num,int k)
    {
        //只需要计算可以偷的房间。在满足最大值为max_num下时,能偷的最多的房间,与k值比较
        //如果大于K,说明max_num还可以缩小
        //如果小于看,说明max_num需要放大
        int count=0;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]<=max_num){
                count++;    //计数
                i++;    //下一个不能偷
            }
        }
        return count>=k;
    }
    int minCapability(vector<int>& nums, int k) {
        //在满足偷k个房间的所有情况下,找出最小的窃取能力。每种情况中房屋金额最大值,为该情况的窃取能力
        //目标:所有情况中最小的窃取能力
    //二分答案
    int left=0,right=*max_element(nums.begin(),nums.end());
    while(left+1<right)   //开
    {
        int mid=(left+right)/2;
        if(check(nums,mid,k))
            //能整好偷,或偷的个数要多,需要减小最大值
            right=mid;
        else left=mid;

    }
        return right;
    }
};
相关推荐
雾月5533 分钟前
LeetCode 1780 判断一个数字是否可以表示成三的幂的和
java·数据结构·算法·leetcode·职场和发展·idea
Demons_kirit17 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
Wendy_robot20 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.20 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针
Y1nhl1 天前
力扣hot100_链表(3)_python版本
python·算法·leetcode·链表·职场和发展
前端 贾公子1 天前
详解 LeetCode 第 242 题 - 有效的字母组
算法·leetcode·职场和发展
Demons_kirit1 天前
LeetCode 2799、2840题解
算法·leetcode·职场和发展
软行1 天前
LeetCode 每日一题 2845. 统计趣味子数组的数目
数据结构·c++·算法·leetcode
雾月551 天前
LeetCode 1292 元素和小于等于阈值的正方形的最大边长
java·数据结构·算法·leetcode·职场和发展
OpenC++1 天前
【C++QT】Buttons 按钮控件详解
c++·经验分享·qt·leetcode·microsoft