【leetcode】1283. Find the Smallest Divisor Given a Threshold
题目如下:
Given an array of integers
nums
and an integerthreshold
, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal tothreshold
.Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).
It is guaranteed that there will be an answer.
Example 1:
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).Example 2:
Input: nums = [2,3,5,7,11], threshold = 11
Output: 3Example 3:
Input: nums = [19], threshold = 5
Output: 4Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
解题思路:除数越大,计算出来的和越小,因此可以使用二分查找法。
代码如下:
class Solution(object):
def smallestDivisor(self, nums, threshold):
"""
:type nums: List[int]
:type threshold: int
:rtype: int
"""
res = 0
low,high = 1,1000000
while low <= high:
mid = (low + high)/2
count = 0
for i in nums:
count += (i/mid)
if i%mid != 0:count += 1
if count <= threshold:
res = mid
high = mid - 1
else:
low = mid + 1
return res
【leetcode】1283. Find the Smallest Divisor Given a Threshold的更多相关文章
- 【leetcode】719. Find K-th Smallest Pair Distance
题目如下: 解题思路:对于这一类知道上限和下限,求第N位是什么的题目,可以先看看二分查找的方法可不可行.首先对nums进行排序,很显然任意两个元素距离绝对值最小是0,最大是nums[-1] - num ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【leetcode】1019. Next Greater Node In Linked List
题目如下: We are given a linked list with head as the first node. Let's number the nodes in the list: n ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
随机推荐
- SQLServer启动和关闭bat脚本
原文:SQLServer启动和关闭bat脚本 安装完毕SQL SERVER 2005后,会默认自动启动SQL Server等几个服务,这几个服务比较占用系统资源.当不运行SQL Server时,最 ...
- zookeeper安装 配置集群
zookeeper下载 http://zookeeper.apache.org/releases.html. 解压 重命名 新建data log两个文件夹配置单个启动 tar -xvf zookeep ...
- CString VariantToString(VARIANT var);转换
Variant类型转换成CString代码 对数据库的操作常常会用Variant和_bstr_t类型,在进行MFC等程序中,通常会用到CString类.从variant转换成CString代码如下:v ...
- Minimum Cut(2015沈阳online)【贪心】
Minimum Cut[贪心]2015沈阳online 题意:割最少的边使得图不连通,并且割掉的边中有且仅有一条是生成树的边. 首先,我们选择一条树中的边进行切割,此时仅考虑树上的边集,有两种情况:1 ...
- c++11 跨平台多线程demo和qt 静态链接(std::thread有join函数,设置 QMAKE_LFLAGS = -static)
#include <stdio.h>#include <stdlib.h> #include <chrono> // std::chrono::seconds#in ...
- 怎样在 js 中实现 反转字符串 的功能?
"string".split('').reverse().join('');
- application.properties参数详解
# ----------------------------------------# CORE PROPERTIES# --------------------------------------- ...
- CSS中为什么有的元素能够设置高度,而有的元素却不能设置高度与宽度?
可以使用{display:block}将内联元素变为块级元素,同时使用{display:inline}将块级元素变为内联元素. {display:inline-block}又是怎么回事,根据张鑫旭老师 ...
- Windows 10 安装FileZilla Server
在windows 10本机安装了FileZilla Server 本机用FilleZilla Client连接localhost或者192.168.0.197 port 21 都可以连通,但是在同 ...
- vue项目 时间戳转 格式
项目用了 element UI的日期插件,修改时 时间回显不了,打印出来是换行了,因此要转换 changeTime(value){ let date = new Date(value); let y ...