力扣410.分割数组的最大值

力扣410.分割数组的最大值

  • 分割数组 使得子数组最大值最小

    • 凡是最小化最大值和最大化最小值的题都是二分答案
    • 二分子数组最大值 求当前数组可以分为几段
cpp 复制代码
  class Solution {
  public:
      int splitArray(vector<int>& nums, int k) {
          auto check = [&](int mid) -> bool 
          {
              int cnt = 1,s = 0;
              for(int x:nums)
              {
                  if(s + x <= mid)
                      s += x;
                  else
                  {
                      if(cnt ++ == k) return false;
                      s = x;
                  }
              }
              return true;
          };
          int l = ranges::max(nums) , r = accumulate(nums.begin(),nums.end(),0);
          while(l<r)
          {
              int mid = l + r >> 1;
              if(check(mid)) r = mid;
              else l = mid + 1;
          }
          return l;
      }
  };
相关推荐
技术专家40 分钟前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
dllxhcjla1 小时前
微服务全套
java
csdn_aspnet1 小时前
C# (QuickSort using Random Pivoting)使用随机枢轴的快速排序
数据结构·算法·c#·排序算法
亚历克斯神1 小时前
JVM 内存管理 2026:深度解析与调优实战
java·spring·微服务
鹿角片ljp1 小时前
最长回文子串(LeetCode 5)详解
算法·leetcode·职场和发展
逻辑驱动的ken2 小时前
Java高频面试题:03
java·开发语言·面试·求职招聘·春招
广师大-Wzx2 小时前
一篇文章看懂MySQL数据库(下)
java·开发语言·数据结构·数据库·windows·python·mysql
野生技术架构师2 小时前
Java NIO到底是个什么东西?
java·开发语言·nio
paeamecium3 小时前
【PAT甲级真题】- Cars on Campus (30)
数据结构·c++·算法·pat考试·pat
likerhood4 小时前
简单工厂设计模式
java·ide·intellij-idea