北京正规网站建设单价群晖自行安装wordpress
news/
2025/10/8 0:24:32/
文章来源:
北京正规网站建设单价,群晖自行安装wordpress,wordpress 域,凤岗镇仿做网站文章目录1. 题目2. 解题1. 题目
描述
给你一个整数数组 nums 和一个正整数 threshold #xff0c;你需要选择一个正整数作为除数#xff0c;然后将数组里每个数都除以它#xff0c;并对除法结果求和。
请你找出能够使上述结果小于等于阈值 threshold 的除数中 最小 的那个…
文章目录1. 题目2. 解题1. 题目
描述
给你一个整数数组 nums 和一个正整数 threshold 你需要选择一个正整数作为除数然后将数组里每个数都除以它并对除法结果求和。
请你找出能够使上述结果小于等于阈值 threshold 的除数中 最小 的那个。
每个数除以除数后都向上取整比方说 7/3 3 10/2 5 。
题目保证一定有解。
1 nums.length 5 * 10^4
1 nums[i] 10^6
nums.length threshold 10^6示例 1
输入nums [1,2,5,9], threshold 6
输出5
解释如果除数为 1 我们可以得到和为 17 1259。
如果除数为 4 我们可以得到和为 7 (1123) 。
如果除数为 5 和为 5 (1112)。示例 2
输入nums [2,3,5,7,11], threshold 11
输出3示例 3
输入nums [19], threshold 5
输出4https://www.lintcode.com/problem/find-the-smallest-divisor-given-a-threshold/description
2. 解题
类似题目 LeetCode 410. 分割数组的最大值极小极大化 二分查找 LeetCode 668. 乘法表中第k小的数二分查找 LeetCode 774. 最小化去加油站的最大距离极小极大化 二分查找 LeetCode 875. 爱吃香蕉的珂珂二分查找 LeetCode LCP 12. 小张刷题计划二分查找 LeetCode 1011. 在 D 天内送达包裹的能力二分查找 LeetCode 1102. 得分最高的路径优先队列BFS/极大极小化 二分查找 LeetCode 1062. 最长重复子串二分查找 LeetCode 5438. 制作 m 束花所需的最少天数二分查找 LeetCode 5489. 两球之间的磁力极小极大化 二分查找 LeetCode 5548. 最小体力消耗路径DFS 二分查找
二分查找答案除数变大和变小或不变有单调性
class Solution {
public:/*** param nums: an array of integers* param threshold: an integer* return: return the smallest divisor*/int smallestDivisor(vectorint nums, int threshold) {// write your code hereint l 1, r INT_MAX, mid, ans;while(l r){mid l((r-l)1);if(sumok(nums, mid, threshold)){ans mid;r mid-1;}elsel mid1;}return ans;}bool sumok(vectorint a, int div, int threshold){long long sum 0;for(auto n : a){sum ceil(n/double(div));if(sum threshold)return false;}return true;}
};53ms C 我的CSDN博客地址 https://michael.blog.csdn.net/
长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/930998.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!