lintcode-medium-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.

Example

Given [-3, 1, 1, -3, 5], return [0, 2][1, 3][1, 1][2, 2] or[0, 4].

Challenge

O(nlogn) time

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

        if(nums == null || nums.length == 0)
            return new int[2];

        int[] res = new int[2];

        if(nums.length == 1){
            res[0] = 0;
            res[1] = 0;
            return res;
        }

        node[] nodes = new node[nums.length];
        int sum = 0;

        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
            nodes[i] = new node(i, sum);
        }

        Arrays.sort(nodes, new Comparator<node>(){
            public int compare(node n1, node n2){
                return n1.sum - n2.sum;
            }
        });

        int index = 0;
        int diff = nodes[index + 1].sum - nodes[index].sum;

        for(int i = 0; i < nums.length - 1; i++){
            if(nodes[i + 1].sum - nodes[i].sum < diff){
                index = i;
                diff = nodes[i + 1].sum - nodes[i].sum;
            }
        }

        res[0] = Math.min(nodes[index].index, nodes[index + 1].index) + 1;
        res[1] = Math.max(nodes[index].index, nodes[index + 1].index);

        return res;
    }

    class node{
        int index;
        int sum;

        public node(int index, int sum){
            this.index = index;
            this.sum = sum;
        }
    }

}
时间: 2024-10-21 20:31:53

lintcode-medium-Subarray Sum Closest的相关文章

Subarray Sum Closest

Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number. Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3],[1, 1], [2, 2] or [0, 4]. Answer 这道题延续Subarray Sum的思路,即将[0, i]的sum存起来.这里

LintCode &quot;Continuous Subarray Sum II&quot;

Flip over your mind: in rotated subarray case, we can simply cut the continuous smallest subarray. class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the las

[LintCode] Continuous Subarray Sum 连续子数组之和

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) Have you met this quest

[LintCode] 1844. subarray sum equals k II

Given an array of integers and an integer k, you need to find the minimum size of continuous subarrays whose sum equals to k, and return its length. if there are no such subarray, return -1. Example Example1 Input: nums = [1,1,1,2] and k = 3 Output:

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

LintCode &quot;Continuous Subarray Sum&quot;

A variation to a classical DP: LCS. class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number */ vector<int> continuousSubarraySum(vector<int>

Leetcode: Maximum Size Subarray Sum Equals k

Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

[LintCode] Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive) Example Given [1,2,3,4] and interval = [1,3], return 4. Bra

Continuous Subarray Sum II(LintCode)

Continuous Subarray Sum II Given an circular integer array (the next element of the last element is the first element), find a continuous subarray in it, where the sum of numbers is the biggest. Your code should return the index of the first number a