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

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

// 参考 https://discuss.leetcode.com/topic/61315/java-easy-binary-search-solution-8ms
// 开始用类似回溯的方法做,ETL了 public class Solution { public int splitArray(int[] nums, int m) {
int mlen = nums.length - m;
int minM = 0;
int maxM = 0;
int sum = 0;
for (int k=0; k<nums.length; k++) {
sum += nums[k];
if (k > mlen) {
sum -= nums[k-1-mlen];
}
maxM = Math.max(maxM, sum);
minM = Math.max(minM, nums[k]);
}
System.out.printf("min:%d, max %d\n", minM, maxM);
int result = bsearch(nums, m, minM, maxM);
return result;
} private int bsearch(int[] nums, int m, int low, int high) {
int mid = 0;
while (low < high) {
mid = low + (high-low) / 2;
if (isValid(nums, m, mid)) {
high = mid; } else {
low = mid + 1;
}
}
return high;
} private boolean isValid(int[] nums, int m, int cand) {
int split = 1;
int sum = 0;
for (int i=0; i<nums.length; i++) {
sum += nums[i];
if (sum > cand) {
split++;
if (split > m) {
return false;
}
sum = nums[i];
}
}
return true;
} /*
class KPair {
public int pos;
public int m; @Override
public int hashCode() {
int ret = String.valueOf(pos).hashCode() ^ String.valueOf(m).hashCode();
return ret;
} @Override
public boolean equals(Object obj) { if (null == obj) {
return false;
}
if (!(obj instanceof KPair)) {
return false;
}
KPair kp = (KPair)obj;
//System.out.printf("kp%d p%d km%d m%d\n", kp.pos, pos, kp.m, m);
return kp.pos == pos && kp.m == m;
}
} public int splitArray(int[] nums, int m) {
Map mp = new HashMap(); KPair okp = new KPair();
int tmp = 0;
int newval = 0; KPair kp = new KPair();
kp.pos = 0;
kp.m = 1;
//System.out.printf("in1 p%d m%d\n", kp.pos, kp.m);
mp.put(kp, nums[0]); for (int i=1; i<nums.length; i++) { okp.pos = i-1;
okp.m = 1;
tmp = (int)(mp.get(okp))+nums[i]; KPair kp2 = new KPair();
kp2.pos = i;
kp2.m = 1;
//System.out.printf("in2 p%d m%d\n", kp2.pos, kp2.m);
mp.put(kp2, tmp); for (int k=0; k<i; k++) {
// tmp is sum of k+1 to i
tmp -= nums[k];
okp.pos = k; for (int j=2; j<=m && j<=k+2; j++) {
okp.m = j-1;
//System.out.printf("for2 p%d m%d\n", okp.pos, okp.m);
newval = (int)(mp.get(okp));
if (tmp > newval) {
newval = tmp;
} KPair kp3 = new KPair();
kp3.pos = i;
kp3.m = j;
if (mp.get(kp3) == null || (int)(mp.get(kp3)) > newval) {
//System.out.printf("in3 p%d m%d\n", kp3.pos, kp3.m);
mp.put(kp3, newval);
}
}
}
} KPair kpr = new KPair();
kpr.pos = nums.length-1;
kpr.m = m;
return (int)(mp.get(kpr));
}
*/ }

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. 【JavaWeb开发】初步实现网站应用钉钉扫码登录

    http://blog.csdn.net/baofeidyz/article/details/59059379 版权声明:转载请注明我的个人微信平台 暴沸 目录(?)[+] 写在前面:如果你还不知道钉 ...

  2. c++ 单例模式研究

    一篇博文:C++ 单例模式的几种实现研究 中 看到的几段代码 懒汉模式 class Singleton { public: static Singleton* GetInstance() { if ( ...

  3. 初识 Fuzzing 工具 WinAFL

    转:https://paper.seebug.org/323/ 初识 Fuzzing 工具 WinAFL 作者:xd0ol1(知道创宇404实验室) 0 引子 本文前两节将简要讨论 fuzzing 的 ...

  4. 可随意交换位置的gridview

    自定义gridview import android.app.Activity; import android.content.Context; import android.graphics.Poi ...

  5. Android之 解析XML文件(1)—— Pull解析

    (以下文章基本照抄<第一行代码>) 解析XML文件有很多方法,这里主要讲Pull解析和SAX解析.这篇文章主要是讲Pull解析. 一.Pull解析参考代码 先上代码: private vo ...

  6. XSS漏洞自动化攻击工具XSSer

    XSS漏洞自动化攻击工具XSSer   XSS是Web应用常见的漏洞.利用该漏洞,安全人员在网站注入恶意脚本,控制用户浏览器,并发起其他渗透操作.XSSer是Kali Linux提供的一款自动化XSS ...

  7. 通过因特网连接Beaglebone Black

    通过因特网连接Beaglebone Black 通过网络连接,可以使你方便地从各种地方以及各种不同的电脑访问到Beaglebone Black.这种连接Beaglebone Black方式通常使用5V ...

  8. 【DFS】BZOJ3522-[Poi2014]Hotel

    [题目大意] 给出一棵树,求三个节点使得它们两两之间的距离相等,问共有多少种可能性? [思路] 显然,这三个节点是关于一个中心点对称地辐射出去的. 枚举中心点,往它的各个子树跑Dfs.tmp[i]表示 ...

  9. 【插头DP】BZOJ1187- [HNOI2007]神奇游乐园

    [题目大意] 在n*m的网格中选一条回路,使权值和最大. [思路] 和之前裸的插头DP差不多,只不过现在回路不需要经过所有的格子.所以有以下几个注意点(具体看注释): (1)left和up插头相等的时 ...

  10. php回溯

    $sl = debug_backtrace(); 返回的$sl 是一个二维数组 包含如下元素: function string 当前的函数名,参见: __FUNCTION__. line intege ...