题目如下:

Given an array of integers nums and an integer threshold, 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 to threshold.

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: 3

Example 3:

Input: nums = [19], threshold = 5
Output: 4

Constraints:

  • 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的更多相关文章

  1. 【leetcode】719. Find K-th Smallest Pair Distance

    题目如下: 解题思路:对于这一类知道上限和下限,求第N位是什么的题目,可以先看看二分查找的方法可不可行.首先对nums进行排序,很显然任意两个元素距离绝对值最小是0,最大是nums[-1] - num ...

  2. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  3. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  5. 【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 ...

  6. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  7. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  8. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  9. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

随机推荐

  1. delphicbuilder10_2_1 安装破解注册

    安装程序 1.解压delphicbuilder10_2_1.iso,以管理员身份运行..\delphicbuilder10_2_1\Install\Setup.exe——选择安装语言——点击OK(推荐 ...

  2. 查询统计SQL分组求和使用小技巧

    我们在做查询统计时,肯定会遇到将查询结果再次分组求和这种需求,但是往往查询的sql本身就比较复杂,再使用分组函数不太可能,那么这时候我们就想到了用临时表的办法,通过联合临时表我们就可以获得想要的分组求 ...

  3. Linux切换root超级用户问题

    具体方法如下: Ubuntu 1.使用终端工具的快捷键Ctrl + Alt +T 打开终端. 2.终端工具打开后如下图所示,操作就在这个窗口中进行 3.切换root用户的的方式一 执行命令 sudo ...

  4. idea配置glassFish

    FIile ---> settings ---> 查找Application Server .. 点击加号,, 点击glassFish Server.. 找到glassFish存放路径 点 ...

  5. 第二周Java课堂作业

    演示一: public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size ...

  6. py2和py3之间的不同

    1.print函数 很琐碎,而 print 语法的变化可能是最广为人知的了,但是仍值得一提的是: Python 2 的 print 声明已经被 print() 函数取代了,这意味着我们必须包装我们想打 ...

  7. Dom编程-左侧菜单栏设计模型实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Java函数式接口

    函数式接口定义且只定义了一个抽象方法.函数式接口的抽象方法的签名称为函数描述符.Java 8的java.util.function包中引入了几个新的函数式接口. 1.Predicate java.ut ...

  9. 大数据学习(2)- export、source(附带多个服务器一起启动服务器)

    linux环境中, A=1这种命名方式,变量作用域为当前线程 export命令出的变量作用域是当前进程及其子进程. 可以通过source 脚本,将脚本里面的变量放在当前进程中 附带自己写的tomcat ...

  10. Spring Boot 版本支持

    一.Spring Boot 版本支持 Spring Boot Spring Framework Java Maven Gradle 1.2.0之前版本   6 3.0+ 1.6+ 1.2.0 4.1. ...