Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

    • 二分边界问题
    • 最大值最小问题
      • 875. 爱吃香蕉的珂珂
      • 1011. 在 D 天内送达包裹的能力
      • 2594. 修车的最少时间
      • 410. 分割数组的最大值
      • 2517. 礼盒的最大甜蜜度
      • 1552. 两球之间的磁力
      • 1482. 制作 m 束花所需的最少天数
      • LCP 12. 小张刷题计划
      • 1870. 准时到达的列车最小时速
      • 1894. 找到需要补充粉笔的学生编号
  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 二分查找
Nreal
2023-11-30
目录

最大值最小问题

# 875. 爱吃香蕉的珂珂 (opens new window)

class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        int l = 1;
        int r = Arrays.stream(piles).max().getAsInt();
        while(l<r){
            int mid = (l+r)>>>1;
            if(retHour(piles,mid)>h)
                l = mid+1;
            else
                r = mid;
        }
        return l;
    }
    int retHour(int[] nums,int v){
        int h = 0;
        for(int num:nums){
            h += num/v;
            if(num%v!=0)
                h++;
        }
        return h;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 1011. 在 D 天内送达包裹的能力 (opens new window)

class Solution {
    public int shipWithinDays(int[] weights, int days) {
        int l = Arrays.stream(weights).max().getAsInt();
        int r = Arrays.stream(weights).sum();
        while(l<r){
            int mid = (l+r)>>>1;
            if(retDay(weights,mid)>days)
                l = mid+1;
            else
                r = mid;
        }
        return l;
    }
    int retDay(int[] nums,int x){
        int days = 0;
        for(int i=0;i<nums.length;){
            int tmp = x;
            while(i<nums.length){
                if(tmp<nums[i]){
                    break;
                }else{
                    tmp -= nums[i];
                }
                i++;
            }
            days++;
        }
        return days;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 2594. 修车的最少时间 (opens new window)

class Solution {
    public long repairCars(int[] ranks, int cars) {
        long l = 1;
        //long r = Arrays.stream(ranks).max().getAsInt()*cars*cars+1;
        long r = (long)1e14;
        while(l<=r){
            long mid = (l+r)>>>1;
            if(f(ranks,cars,mid))
                r = mid-1;
            else
                l = mid+1;
        }
        return l;
    }
    // time时间内 机械工修车总和数
    boolean f(int[] ranks,int cars,long time){
        long cnt = 0;
        for(int rank:ranks){
            cnt += (int)Math.sqrt(time/rank);
        }
        return cnt>=cars;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 410. 分割数组的最大值 (opens new window)

# 2517. 礼盒的最大甜蜜度 (opens new window)

# 1552. 两球之间的磁力 (opens new window)

# 1482. 制作 m 束花所需的最少天数 (opens new window)

# LCP 12. 小张刷题计划 (opens new window)

# 1870. 准时到达的列车最小时速 (opens new window)

# 1894. 找到需要补充粉笔的学生编号 (opens new window)

二分边界问题
贪心

← 二分边界问题 贪心→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式