Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Have you met this question in a real interview? Yes
Example
Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4]. Challenge
O(nlogn) time
Analysis:
s[i+1] = nums[0]+....nums[i], also record the index i into s[i+1]. Sort array s, and the minimum difference between two consecutive element, is the the subarray.
class Element implements Comparable<Element> {
int index;
int value;
public Element(int index, int value) {
this.index = index;
this.value = value;
} public int compareTo(Element other) {
return this.value - other.value;
} public int getIndex() {
return index;
} public int getValue() {
return value;
}
} public class Solution {
/**
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number
* and the index of the last number
*/ public int[] subarraySumClosest(int[] nums) {
// write your code here
int[] res = new int[2];
Element[] sums = new Element[nums.length+1];
sums[0] = new Element(-1, 0);
int sum = 0;
for (int i=0; i<nums.length; i++) {
sum += nums[i];
sums[i+1] = new Element(i, sum);
}
Arrays.sort(sums);
int minDif = Integer.MAX_VALUE;
for (int i=1; i<sums.length; i++) {
int dif = sums[i].getValue() - sums[i-1].getValue();
if (dif < minDif) {
minDif = dif;
res[0] = Math.min(sums[i].getIndex(), sums[i-1].getIndex()) + 1;
res[1] = Math.max(sums[i].getIndex(), sums[i-1].getIndex());
}
}
return res;
}
}
Lintcode: Subarray Sum Closest的更多相关文章
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- [LintCode] Subarray Sum & Subarray Sum II
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- LintCode Subarray Sum
For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...
- LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
随机推荐
- var wi = 0; wi < arr.length; wi++
思维 <?php$w = 123;$wb = $w;$w = 456;echo $wb;?><script type="text/javascript"> ...
- sql 语句查询练习题
1. 查询Student表中的所有记录的Sname.Ssex和Class列. select sname,ssex,class from student 2. 查询教师所有的单位即不重复的Depart列 ...
- 异常处理与MiniDump详解(4) MiniDump
http://blog.csdn.net/vagrxie/article/details/4398721 异常处理与MiniDump详解(4) MiniDump 分类: [Wi ...
- C/C++ 获取汉字拼音首字母
#include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...
- .Net程序员安卓学习之路6:等待条
一般在需要访问网络或者长时间操作的时候避免界面无响应才使用:等待条 本例将实现一个无框架的等待条,效果如下: 点击后,使线程Sleep5秒,就出现如下效果: 实现代码如: private Progre ...
- <q>标签,短文本引用;<blockquote>标签,长文本引用
<q>标签,短文本引用 <q>引用文本</q>,默认显示双引号,不需要在文本中添加 <blockquote>标签,长文本引用 浏览器对<block ...
- Java 实现导出excel表 POI
1.首先下载poi-3.6-20091214.jar 2.Student.java import java.util.Date; public class Student { private int ...
- The All-purpose Zero---hdu5773(LIS变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 给出n个数,其中 0 可当作任何数,求能够得到的最长上升子序列(严格上升)的长度; 我们 ...
- c#网页爬虫初探
一个简单的网页爬虫例子! html代码: <head runat="server"> <title>c#爬网</title> </head ...
- mongoDB 读书笔记(初级命令)
关于mongoDB的相关知识,读书笔记,便于自己查阅用,不定期更新(纯手打) <mongoDB权威指南> 一.创建更新和删除 1.创建 //批量插入一个集合可以节省时间,只用 ...