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的更多相关文章

  1. Subarray Sum Closest

    Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...

  2. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  3. [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 ...

  4. 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 ...

  5. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  6. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  7. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  9. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

随机推荐

  1. a read only variable

    SHOW VARIABLES LIKE '%FORMAT%' SET DATE_FORMAT ='%Y-%m-%d' [SQL]SET DATE_FORMAT ='%Y-%m-%d' [Err] 12 ...

  2. find grep 组合使用

    1. 查找所有".h"文件 find /PATH -name "*.h" 2. 查找所有".h"文件中的含有"helloworld ...

  3. random circle

    <!doctype html><meta charset="utf-8"><html><head><title>D3 t ...

  4. 蓝牙的L2CAP协议

    1.概述     L2CAP能向上层提供面向连接的或者无连接的数据服务,拥有multiplexing capability and segmentation and reassembly operat ...

  5. Linq中常用的方法

    这几天闲着也是闲着,就仔细的研究了一下Linq的语法,还有他的一些扩展方法的使用. 下面是一些常用的扩展方法. Aggregate 自定义的聚合计算 All 检测序列中所有元素是否都满足指定的条件 A ...

  6. HBase的数据迁移(含HDFS的数据迁移)

    1.启动两个HDFS集群 hadoop0,hadoop1,都是伪分布式的集群 2.启动hadoop3的zookeeper与hbase 注意点:需要开启yarn服务,因为distcp需要yarn. 3. ...

  7. qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)

    qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...

  8. 汇编查看StackFrame栈帧

    INCLUDE Irvine32.inc myProc PROTO, x:DWORD, y:DWORD .data .code main proc mov eax,0EAEAEAEAh mov ebx ...

  9. HTML5中表单的创建

    一.常用表单标签如下: (1)<input>中的“type”属性: 复选框-checkbox:单选按钮-radio;按钮-button:提交-submit; (2)文本域 行-cols:列 ...

  10. 二级c程序设计题(1)

    原文:http://www.cnblogs.com/imaker/p/6120951.html 所属年份:2010.9;2011.3;2012.3;请编写函数fun,其功能是:找出2×M整型二维数组中 ...