注意,第一种用法,涉及到一些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. LoadRunner截取字符串操作

    LoadRunner截取字符串操作 在使用LoadRunner winsockets协议写脚本,遇到下面问题: 在接收到的查询数据库的结果中我要取红色部份用于下面的select recv buf60 ...

  2. Could not apply the stored configuration for monitors

    在用户目录下$user.home/.config/monitors.xml,要解决上面的问题,最简单的办法就是删除这个monitors.xml文件,重启一下电脑

  3. HttpServletRequest继承字ServletRequest的常用方法

    //获取请求的完整路径String origUrl = req.getRequestURL().toString(); //获取查询的参数String queryStr = req.getQueryS ...

  4. jquery 美化弹出提示 漂亮的Dialog 对话框

    三个不同的效果,分别是普通的警告,确认/取消,带一个输入框 本例用了jquery.alertify.js,请到演示页面查看 css文件也请到演示页面查看 JavaScript Code <scr ...

  5. CodeForces 909D Colorful Points

    题解: 暴力,模拟. 把字符串压缩一下,相同的处理成一位,记录下个数,然后暴力模拟即可. #include <bits/stdc++.h> using namespace std; con ...

  6. Python对Excel的操作

    Python几个读取Excel库的介绍: xlwings 可结合 VBA 实现对 Excel 编程,强大的数据输入分析能力,同时拥有丰富的接口,结合 pandas/numpy/matplotlib 轻 ...

  7. python 异步 select pooll epoll

    概念: 首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当se ...

  8. dp 加搜索 百练1088 滑雪

    描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长的 ...

  9. Hibernate fetching strategies(抓取策略)

    抓取策略(fetching strategies)是指:当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.抓取策略可以在O/R映射的 ...

  10. [BZOJ4521][CQOI2016]手机号码(数位DP)

    4521: [Cqoi2016]手机号码 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 875  Solved: 507[Submit][Status ...