LeetCode 410——分割数组的最大值
1. 题目
2. 解答
此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数。
class Solution {
public:
// 若分割数组的最大值为 value,判断能否进行划分
bool cansplit(vector<int>& nums, int value, int m)
{
int len = nums.size();
int i = 0;
int sum = 0;
int split_count = 0; // 分割次数
// 依次往后求和,若和小于等于 value,则继续加;
// 若和大于 value,则分割次数加 1,从当前位置开始将和清零,重新开始
for (i = 0; i < len; i++)
{
if (sum + nums[i] <= value)
{
sum += nums[i];
}
else
{
split_count++;
sum = nums[i];
}
if (split_count == m) // 分割次数超出 m, 不满足划分
{
return false;
}
}
return true;
}
int splitArray(vector<int>& nums, int m) {
int len = nums.size();
int i = 0;
// 分割数组的最大值介于数组的最大元素和数组所有元素的和之间
int max = nums[0];
int sum = 0;
for (i = 0; i < len; i++)
{
if (nums[i] > max)
{
max = nums[i];
}
sum += nums[i];
}
int left = max;
int right = sum;
int mid = 0;
while(left < right)
{
mid = left + (right - left) / 2;
if (cansplit(nums, mid, m)) // 能划分,继续找有没有更小的值
{
right = mid; //不减是因为无法确保减一之后的数是否满足划分
}
else // 不能划分,增大数值继续寻找
{
left = mid + 1;
}
}
return left;
// 最终 left = right 结束,left 值就是所求
}
};
获取更多精彩,请关注「seniusen」!
LeetCode 410——分割数组的最大值的更多相关文章
- Java实现 LeetCode 410 分割数组的最大值
410. 分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: ...
- leetcode 410. 分割数组的最大值(二分法)
1. 题目描述 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: 1 ≤ n ...
- Leetcode 410.分割数组的最大值
分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意:数组长度 n 满足以下条件: 1 ≤ n ...
- [LeetCode] 410. Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Leetcode 659.分割数组为连续子序列
分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...
- Java实现 LeetCode 659 分割数组为连续子序列 (哈希)
659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...
- 410 Split Array Largest Sum 分割数组的最大值
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件: 1 ≤ n ≤ 1000 ...
- [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Notepad++ 插件之 TextFX (安装及作用)
<安装:打开 notepad++ 插件 -> Plugin Manager -> Show Plugin Manager -> available ->选中 TextF ...
- 使用带有对象的data-ng-bind
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 用$(this)选择其下带有class的子元素
$(this).find('.son').removeClass("disn")
- spring入门(六) spring mvc+mybatis
1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...
- ios 判断用户是否开启权限---并跳转设置
ios 判断用户是否开启权限---并跳转设置 ios 判断用户是否开启权限---并跳转“系统设置” 1.判断 访问相册 或 相机 权限是否开启 2.检测是否开启定位 后面将持续更新 只有在应用请求过位 ...
- 在cmd下面执行.py文件时提示ModuleNotFoundError 但是 IDE 不报错
原理是 python 解释器寻找 模块的顺序决定,不细说 简略来讲就是 在 IDE中运行,会自动帮你把项目根目录添加到 PYTHONPATH 中,但是在 cmd 运行需要自己添加. 解决方法: 1. ...
- 关于 laravel 集合的使用
常用的有 count() count方法返回集合中所有项的数目: $collection = collect([1, 2, 3, 4]); $collection->count(); forPa ...
- hadoop搭建----centos免密码登录、修改hosts文件
分布式系统在传输数据时需要多台电脑免密码登录 如:A(192.168.227.12)想ssh免密码登录到B(192.168.227.12),需要把A的公钥文件(~/.ssh/id_rsa.pub)里内 ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...
- cf776D Mahmoud and a Dictionary
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...