注意,第一种用法,涉及到一些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. python通过帐号和密码访问mysql

    数据库:test1  数据表:a1 用input输入姓名和密码,对了的话显示信息.不对的话显示error import pymysql import hashlib conn = pymysql.co ...

  2. lr的脚本调试方法

    1)  设置调试断点(快捷键F9)当设置断点的脚本,脚本运行到断点处,自动停止运行,我们可以通过查看运行日志,来观察脚本执行的情况: LR中也能设置断点,具体菜单在:Insert - Toggle B ...

  3. CodeForces 785B Anton and Classes

    简单判断. 找第一类区间中$R$最大的,以及第二类区间中$L$最小的,判断距离. 找第二类区间中$R$最大的,以及第一类区间中$L$最小的,判断距离. 两种情况取个最大值即可. #include &l ...

  4. BZOJ1878 [SDOI2009] HH的项链 [莫队,卡常]

    BZOJ传送门,洛谷传送门 HH的项链 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义. ...

  5. jquery选择器详细说明

    jquery中选择器感觉是用的特别多并且特别方便的一个方法,今天就在这里详细的记载下大多数常用的选择器,一起学习探讨. 首先介绍的是css3的选择器,其中包括了标签选择器(div),ID选择器(#ID ...

  6. 【JAVAWEB学习笔记】网上商城实战4:订单模块

    今日任务 完成订单模块的功能 1.1      订单 模块的功能 1.1.1    我的订单: [我的订单的查询] * 在header.jsp中点击我的订单. * 提交到Servlet: * 获得用户 ...

  7. 外行人都能看懂的SpringCloud

    一.前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦,去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)... 所以,这篇主要来讲讲SpringCloud的一些基础的 ...

  8. ubuntu 安装qq 及解决安装完搜狗输入法不显示键盘的方法

    安装qq: https://zhuanlan.zhihu.com/p/27549700 解决搜狗输入法不显示的问题: http://blog.csdn.net/crystal_zero/article ...

  9. FastReport.Net使用:[7]打印空白行

    方法一:使用子报表的最少数据行属性 1.以前面的[简单报表一]为例,右键“数据区”在右键菜单中选择“Add Child Band”菜单添加子报表. 2.为原报表添加一列[序号],使用系统变量中的行号( ...

  10. POJ1741 Tree 树分治模板

    http://poj.org/problem?id=1741   题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数.   dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...