当前位置:网站首页>【LeetCode】1283. 使结果不超过阈值的最小除数
【LeetCode】1283. 使结果不超过阈值的最小除数
2022-08-09 04:55:00 【酥酥~】
题目
给你一个整数数组 nums 和一个正整数 threshold ,你需要选择一个正整数作为除数,然后将数组里每个数都除以它,并对除法结果求和。
请你找出能够使上述结果小于等于阈值 threshold 的除数中 最小 的那个。
每个数除以除数后都向上取整,比方说 7/3 = 3 , 10/2 = 5 。
题目保证一定有解。
示例 1:
输入:nums = [1,2,5,9], threshold = 6
输出:5
解释:如果除数为 1 ,我们可以得到和为 17 (1+2+5+9)。
如果除数为 4 ,我们可以得到和为 7 (1+1+2+3) 。如果除数为 5 ,和为 5 (1+1+1+2)。
示例 2:
输入:nums = [2,3,5,7,11], threshold = 11
输出:3
示例 3:
输入:nums = [19], threshold = 5
输出:4
提示:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
题解
二分法
以最大值最小值为两端,不断修改mid求得最优解
class Solution {
public:
int smallestDivisor(vector<int>& nums, int threshold) {
int left = 1;
int right = *max_element(nums.begin(),nums.end());
int res = 1;
while(left<=right)
{
int mid = left + ((right-left)>>1);
int cnt = 0;
for(int i:nums)
{
cnt += i/mid + (i%mid!=0);
}
//cout<<left<<"=="<<mid<<"=="<<right<<"==>"<<cnt<<endl;
if(cnt > threshold)
{
left = mid+1;
}
else
{
right = mid-1;
res = mid;
}
}
return res;
}
};
边栏推荐
- Alibaba Cloud Tianchi Contest Question (Machine Learning) - Prediction of Industrial Steam Volume (Complete Code)
- uboot中board_init bi_arch_number在哪
- 2022下半年深圳信息系统项目管理师认证招生简章
- Divisible property 1
- Pycharm社区版专业版下载安装环境配置【精细到每一个步骤】
- [OpenCV] - Find and draw contours
- 【HMS Core】【FAQ】【AR Engine】AR Engine常见问题合集
- 供应商对接Chewy的EDI需求
- Docker部署MySQL
- 2022 High-altitude installation, maintenance, and demolition exam practice questions and mock exams
猜你喜欢

力扣202-快乐数——哈希集合

【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed

2022下半年深圳信息系统项目管理师认证招生简章

供应商对接Chewy的EDI需求

JS-全局dom对象的使用---使用htm样式和js函数动作的完全分离

LN论文、五种归一化原理和实现

浙江DAMA-CDGA/CDGP数据治理认证招生简章

杰理之智能充电仓低电发码关机 触摸不开机【篇】

存储系统架构演变

Pycharm社区版专业版下载安装环境配置【精细到每一个步骤】
随机推荐
【Harmony OS】【ARK UI】Public Event Module
dsafasfdasfasf
亚马逊面对风控,自养号测评时应该怎么做?
杰理之智能充电仓低电发码关机 触摸不开机【篇】
Eureka-Server------单节和集群的搭建
leetcode:402. 移掉 K 位数字
关于sys.path.append(‘..‘)失效
【HMS Core】【FAQ】【AR Engine】AR Engine常见问题合集
匿名共享内存 ashmem
MySQL: Implementation Principles of Submitted Read and Repeatable Read | MVCC (Multi-Version Concurrency Control) - Notes for Your Own Use
力扣349-两个数组的交集——HashSet
【学习笔记】AGC044
[21天学习挑战赛——内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
【暑期每日一题】洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here
杰理之采用mix out eq 没有作用【篇】
【日常训练--腾讯精选50】7. 整数反转
What is it like to work at Kuaishou?
Oracle02-sql学习(单表查询,exists子查询,函数入门)
安装pytorch和cuda
[MLT] Analysis of MLT Multimedia Framework Production and Consumption Architecture (2)