1. 题目

2. 解答

此题目为 今日头条 2018 AI Camp 5 月 26 日在线笔试编程题第二道——最小分割分数


  1. class Solution {
  2. public:
  3. // 若分割数组的最大值为 value,判断能否进行划分
  4. bool cansplit(vector<int>& nums, int value, int m)
  5. {
  6. int len = nums.size();
  7. int i = 0;
  8. int sum = 0;
  9. int split_count = 0; // 分割次数
  10. // 依次往后求和,若和小于等于 value,则继续加;
  11. // 若和大于 value,则分割次数加 1,从当前位置开始将和清零,重新开始
  12. for (i = 0; i < len; i++)
  13. {
  14. if (sum + nums[i] <= value)
  15. {
  16. sum += nums[i];
  17. }
  18. else
  19. {
  20. split_count++;
  21. sum = nums[i];
  22. }
  23. if (split_count == m) // 分割次数超出 m, 不满足划分
  24. {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. int splitArray(vector<int>& nums, int m) {
  31. int len = nums.size();
  32. int i = 0;
  33. // 分割数组的最大值介于数组的最大元素和数组所有元素的和之间
  34. int max = nums[0];
  35. int sum = 0;
  36. for (i = 0; i < len; i++)
  37. {
  38. if (nums[i] > max)
  39. {
  40. max = nums[i];
  41. }
  42. sum += nums[i];
  43. }
  44. int left = max;
  45. int right = sum;
  46. int mid = 0;
  47. while(left < right)
  48. {
  49. mid = left + (right - left) / 2;
  50. if (cansplit(nums, mid, m)) // 能划分,继续找有没有更小的值
  51. {
  52. right = mid; //不减是因为无法确保减一之后的数是否满足划分
  53. }
  54. else // 不能划分,增大数值继续寻找
  55. {
  56. left = mid + 1;
  57. }
  58. }
  59. return left;
  60. // 最终 left = right 结束,left 值就是所求
  61. }
  62. };

获取更多精彩,请关注「seniusen」!

LeetCode 410——分割数组的最大值的更多相关文章

  1. Java实现 LeetCode 410 分割数组的最大值

    410. 分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: ...

  2. leetcode 410. 分割数组的最大值(二分法)

    1. 题目描述 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: 1 ≤ n ...

  3. Leetcode 410.分割数组的最大值

    分割数组的最大值 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意:数组长度 n 满足以下条件: 1 ≤ n ...

  4. [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 ...

  5. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  6. Leetcode 659.分割数组为连续子序列

    分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...

  7. Java实现 LeetCode 659 分割数组为连续子序列 (哈希)

    659. 分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [ ...

  8. 410 Split Array Largest Sum 分割数组的最大值

    给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件:    1 ≤ n ≤ 1000 ...

  9. [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 ...

随机推荐

  1. Notepad++ 插件之 TextFX (安装及作用)

    <安装:打开 notepad++  插件 -> Plugin Manager -> Show Plugin Manager -> available ->选中 TextF ...

  2. 使用带有对象的data-ng-bind

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. 用$(this)选择其下带有class的子元素

    $(this).find('.son').removeClass("disn")

  4. spring入门(六) spring mvc+mybatis

    1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...

  5. ios 判断用户是否开启权限---并跳转设置

    ios 判断用户是否开启权限---并跳转设置 ios 判断用户是否开启权限---并跳转“系统设置” 1.判断 访问相册 或 相机 权限是否开启 2.检测是否开启定位 后面将持续更新 只有在应用请求过位 ...

  6. 在cmd下面执行.py文件时提示ModuleNotFoundError 但是 IDE 不报错

    原理是 python 解释器寻找 模块的顺序决定,不细说 简略来讲就是 在 IDE中运行,会自动帮你把项目根目录添加到 PYTHONPATH 中,但是在 cmd 运行需要自己添加. 解决方法: 1. ...

  7. 关于 laravel 集合的使用

    常用的有 count() count方法返回集合中所有项的数目: $collection = collect([1, 2, 3, 4]); $collection->count(); forPa ...

  8. hadoop搭建----centos免密码登录、修改hosts文件

    分布式系统在传输数据时需要多台电脑免密码登录 如:A(192.168.227.12)想ssh免密码登录到B(192.168.227.12),需要把A的公钥文件(~/.ssh/id_rsa.pub)里内 ...

  9. 【Leetcode】647. Palindromic Substrings

    Description Given a string, your task is to count how many palindromic substrings in this string. Th ...

  10. cf776D Mahmoud and a Dictionary

    Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...