注意,第一种用法,涉及到一些Java的知识。就是采用Object作为HashMap的key的时候,需要重载这个Class的 equals 和 hashCode 这两个方法。其中equals需要判断一下比较元素的类型,而hashCode 里面可以采用 String.valueOf(val).hashCode() ^ 的方法来处理。

在HashMap里面查找的时候,会调用HashMap里面的元素的equals方法,把待查找的元素作为参数传给这个方法,来进行比较和判断元素是否存在于HashMap中。

  1. // 参考 https://discuss.leetcode.com/topic/61315/java-easy-binary-search-solution-8ms
  2. // 开始用类似回溯的方法做,ETL了
  3.  
  4. public class Solution {
  5.  
  6. public int splitArray(int[] nums, int m) {
  7. int mlen = nums.length - m;
  8. int minM = 0;
  9. int maxM = 0;
  10. int sum = 0;
  11. for (int k=0; k<nums.length; k++) {
  12. sum += nums[k];
  13. if (k > mlen) {
  14. sum -= nums[k-1-mlen];
  15. }
  16. maxM = Math.max(maxM, sum);
  17. minM = Math.max(minM, nums[k]);
  18. }
  19. System.out.printf("min:%d, max %d\n", minM, maxM);
  20. int result = bsearch(nums, m, minM, maxM);
  21. return result;
  22. }
  23.  
  24. private int bsearch(int[] nums, int m, int low, int high) {
  25. int mid = 0;
  26. while (low < high) {
  27. mid = low + (high-low) / 2;
  28. if (isValid(nums, m, mid)) {
  29. high = mid;
  30.  
  31. } else {
  32. low = mid + 1;
  33. }
  34. }
  35. return high;
  36. }
  37.  
  38. private boolean isValid(int[] nums, int m, int cand) {
  39. int split = 1;
  40. int sum = 0;
  41. for (int i=0; i<nums.length; i++) {
  42. sum += nums[i];
  43. if (sum > cand) {
  44. split++;
  45. if (split > m) {
  46. return false;
  47. }
  48. sum = nums[i];
  49. }
  50. }
  51. return true;
  52. }
  53.  
  54. /*
  55. class KPair {
  56. public int pos;
  57. public int m;
  58.  
  59. @Override
  60. public int hashCode() {
  61. int ret = String.valueOf(pos).hashCode() ^ String.valueOf(m).hashCode();
  62. return ret;
  63. }
  64.  
  65. @Override
  66. public boolean equals(Object obj) {
  67.  
  68. if (null == obj) {
  69. return false;
  70. }
  71. if (!(obj instanceof KPair)) {
  72. return false;
  73. }
  74. KPair kp = (KPair)obj;
  75. //System.out.printf("kp%d p%d km%d m%d\n", kp.pos, pos, kp.m, m);
  76. return kp.pos == pos && kp.m == m;
  77. }
  78. }
  79.  
  80. public int splitArray(int[] nums, int m) {
  81. Map mp = new HashMap();
  82.  
  83. KPair okp = new KPair();
  84. int tmp = 0;
  85. int newval = 0;
  86.  
  87. KPair kp = new KPair();
  88. kp.pos = 0;
  89. kp.m = 1;
  90. //System.out.printf("in1 p%d m%d\n", kp.pos, kp.m);
  91. mp.put(kp, nums[0]);
  92.  
  93. for (int i=1; i<nums.length; i++) {
  94.  
  95. okp.pos = i-1;
  96. okp.m = 1;
  97. tmp = (int)(mp.get(okp))+nums[i];
  98.  
  99. KPair kp2 = new KPair();
  100. kp2.pos = i;
  101. kp2.m = 1;
  102. //System.out.printf("in2 p%d m%d\n", kp2.pos, kp2.m);
  103. mp.put(kp2, tmp);
  104.  
  105. for (int k=0; k<i; k++) {
  106. // tmp is sum of k+1 to i
  107. tmp -= nums[k];
  108. okp.pos = k;
  109.  
  110. for (int j=2; j<=m && j<=k+2; j++) {
  111. okp.m = j-1;
  112. //System.out.printf("for2 p%d m%d\n", okp.pos, okp.m);
  113. newval = (int)(mp.get(okp));
  114. if (tmp > newval) {
  115. newval = tmp;
  116. }
  117.  
  118. KPair kp3 = new KPair();
  119. kp3.pos = i;
  120. kp3.m = j;
  121. if (mp.get(kp3) == null || (int)(mp.get(kp3)) > newval) {
  122. //System.out.printf("in3 p%d m%d\n", kp3.pos, kp3.m);
  123. mp.put(kp3, newval);
  124. }
  125. }
  126. }
  127. }
  128.  
  129. KPair kpr = new KPair();
  130. kpr.pos = nums.length-1;
  131. kpr.m = m;
  132. return (int)(mp.get(kpr));
  133. }
  134. */
  135.  
  136. }

split-array-largest-sum(参考了discuss)的更多相关文章

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

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

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

  3. Split Array Largest Sum

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

  4. Leetcode: Split Array Largest Sum

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

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

  6. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

  7. Split Array Largest Sum LT410

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

  8. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  9. 【leetcode】410. Split Array Largest Sum

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  10. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

随机推荐

  1. jquery validate表单验证插件的基本使用方法及功能拓展

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.    1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮助提 ...

  2. 二叉排序树实现(C++封装)

    设计思路 设计一个类,根结点只可读取,具备构造二叉树.插入结点.删除结点.查找. 查找最大值.查找最小值.查找指定结点的前驱和后继等功能接口. 二叉排序树概念 它或者是一棵空树:或者是具有下列性质的二 ...

  3. Java8所有的包介绍(由英文文档翻译而来)

    转载: Java8所有的包介绍(由英文文档翻译而来)

  4. windows环境下模仿Linux环境发起curl请求

    1.到官网下载curl工具包 2.在curl.exe目录中使用(使用方式1) 解压下载后的压缩文件,通过cmd命令进入到curl.exe所在的目录. 由于博主使用的是windows 64位 的系统,因 ...

  5. ZOJ 3957 Knuth-Morris-Pratt Algorithm

    暴力. #include<bits/stdc++.h> using namespace std; ]; int main() { int T; scanf("%d",& ...

  6. Mybatis源码分析之参数处理

    Mybatis对参数的处理是值得推敲的,不然在使用的过程中对发生的一系列错误直接懵逼了. 以前遇到参数绑定相关的错误我就是直接给加@param注解,也稀里糊涂地解决了,但是后来遇到了一些问题推翻了我的 ...

  7. JavaSE1

    <The Pragmatic Programmer><The Mythical Man-month><Clean Code><The Clean Coder& ...

  8. Python操作Mongo数据库

    连接数据库 import pymongo # 连接到数据库,以下两种方式均可 client = pymongo.MongoClient(host='localhost', port=27017) cl ...

  9. 【WIN10】Bind、Binding與Converter的使用

    Demo源碼下載:http://yunpan.cn/cHuCmI4NK4xwr  访问密码 8201 1.Bind Bind的使用方式是: <Button Content="{x:Bi ...

  10. HihoCoder - 1756 打怪

    题面在这里! 拆成两个部分分别算显然比较简单. 前面一个部分排个序枚举最大值算就好啦. 后面的就相当于把每一种数值的贡献加起来,也可以在排完序之后的a[]上面直接算出来. #include<bi ...