split-array-largest-sum(参考了discuss)
注意,第一种用法,涉及到一些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)的更多相关文章
- [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] 410. Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 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 ...
- [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 ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- Split Array Largest Sum LT410
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
随机推荐
- 【js】两个数相除有余数时结果加1
var all=15; var item=2; var pages=all%item==0?(all/item):(Math.floor(all/item)+1); console.log(pages ...
- es6的Map()构造函数
普通的object对象是键值对的集合,但对于它的键却有着严苛的要求,必须是字符串,这给我们平时带来很多的不方便 Map函数类似于对象,但它是一个更加完美的简直对集合,键可以是任意类型 set()方法可 ...
- vue-router history 模式 iis 配置
首先需要安装 url rewrite模块到IIS点我安装 然后在web.config文件中添加如下配置 <?xml version="1.0" encoding=" ...
- 简单了解Linux的inode与block
Linux常见文件系统类型:ext3(CentOS5),ext4(CentOS6),xfs(CentOS7) Windows常见文件系统类型:FAT32,NTFS (1).inode的内容 1)ino ...
- 切换java版本
First, clean your project: Project > Clean If that doesn't fix things... Second check your projec ...
- Codeforces 992 E. Nastya and King-Shamans
\(>Codeforces\space992 E. Nastya and King-Shamans<\) 题目大意 : 给你一个长度为 \(n\) 的序列,有 \(q\) 次操作,每一次操 ...
- 2018/3/18 noip模拟赛 20分
T1 dp,特别裸特别简单,我放弃了写了个dfs. T2 树归,特别裸特别简单,我不会写. T3 贪心二分不知道什么玩意儿反正不会写就对了. 我是个智障
- loj115 无源汇有上下界可行流
link 题意&题解 code: #include<bits/stdc++.h> #define rep(i,x,y) for (int i=(x);i<=(y);i++) ...
- Flask请求上下文源码讲解,简单的群聊单聊web
请求上下文流程图 群聊html代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 【10.3校内测试【国庆七天乐!】】【DP+组合数学/容斥】【spfa多起点多终点+二进制分类】
最开始想的暴力DP是把天数作为一个维度所以怎么都没有办法优化,矩阵快速幂也是$O(n^3)$会爆炸. 但是没有想到另一个转移方程:定义$f[i][j]$表示每天都有值的$i$天,共消费出总值$j$的方 ...