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. Bigtable: A Distributed Storage System for Structured Data

    https://static.googleusercontent.com/media/research.google.com/en//archive/bigtable-osdi06.pdf Abstr ...

  2. 8添加一些样式:开始学习CSS

    CSS中简单的表达式,成为规则.一个典型的规则包括一个选择符.若干属性和属性值. 1.在XHTML中直接添加CSS样式,必须在<head>元素里添加样式开始和结束标记.(但这未必是最好的方 ...

  3. C#读取shapefile文件(不用AE)

    1.创建工程文件,选窗体应用程序,命名为:EsriShpReader 2.添加SplitContainer控件到窗体 3.在SplitContainer.panel1中添加两个按钮Button,tex ...

  4. 【转】设计模式(三)建造者模式Builder(创建型)

    (http://blog.csdn.net/hguisu/article/details/7518060) 1. 概述 在软件开发的过程中,当遇到一个"复杂的对象"的创建工作,该对 ...

  5. IOS NSDate NSDateFormatter 导致相差8小时

    时间问题应该是所有编程语言都要处理的.详细学过php的同学知道,php中也会有相差8小时的问题,然而php可以非常方便的解决的,直接设置下就好了 我最近在学习IOS的过程中,发现IOS的日期处理也是个 ...

  6. Oracle一些基本操作

    查看表以及列: Select * From all_tables where owner = 'userName' ---注意,这里需要区分大小写! select * from user_tab_co ...

  7. .Net程序员安卓学习之路1:登陆界面

    任何编程学习起步均是HelloWorld,作为稍有>net编程经验的我们来说就跳过这步吧,咱们且从简单登录界面开始.先看看效果: 一.准备知识: 1. 安卓环境:安装好JDK,直接去官网下载AD ...

  8. IIS是如何处理ASP.NET请求的

    每次服务器接受到请求,都要先经IIS处理.这不是一篇描述ASP.NE生命周期的文章,仅仅是关于IIS操作的.在我们开始之前,先了解这些会有助于对全文的理解,同时欢迎反馈和建议. 什么是Web Serv ...

  9. 一些html页面资料

    一下没有什么重要的,只是我平时积累的一些页面,紧急时或许会有用,相信过一段时间去东宇(公司分公司)了,这些资料页带不走,还不如留在博客里,趁组长级别们开会去了,他们已经开了一个点啦!我的组长去东宇查看 ...

  10. [LeetCode]题解(python):102 Binary Tree Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...