← [ BACK_TO_NOTES ]

使结果不超过阈值的最小除数

1283. 使结果不超过阈值的最小除数 - 力扣(LeetCode)

#algorithm #snippet #evergreen #leetcode #binary-search

[!info] related notes 算法面试题型 MOC 二分查找

使结果不超过阈值的最小除数

题目

1283. 使结果不超过阈值的最小除数 - 力扣(LeetCode)

题解

二分查找

题目数组由正整数组成,并保证 threshold ≥ nums.length。因此除数 max(nums) 时每项上取整为 1,一定可行;答案在 [1, max(nums)]。除数增大时上取整之和不增,找第一个使总和不超过 threshold 的值。

var smallestDivisor = function(nums, threshold) {
    function compute(ans) {
        let sum = 0;
        for (const a of nums) {
            sum += Math.ceil(a / ans);
            if (sum > threshold) return false;
        }
        return true;
    }
    
    let left = 1, right = 1;
    for (const x of nums) right = Math.max(right, x);
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        if (compute(mid)) {
             right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return left;
};
创建于 2025/1/1 更新于 2026/10/7
Digital Biome
Access

键盘导航

j / k / ↑ / ↓
选择对象
Enter / o
打开选择
/ 或 Ctrl+K
全站搜索
i / f / 1–4
筛选当前列表
g → h / l / p / t / a
首页 / 笔记 / 项目 / 工具 / About
r → 1–9
打开所选对象的关联
u
返回上一页
Esc
关闭浮层 / 取消命令;输入框内清空文本