Subarray Sum Closet

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

这题求和最接近0的子数组,属于Subarray Sum的follow up.思路也很近似,每次求的是当前位置及之前的数组的一个和.但是这题并不是等于0,不会有一样的和出现,所以使用hashmap没什么可能.

在这种情况下,维护一个<sum,index>的pair的一个数组,将数组按sum值排个序.在排序后数组中找相邻位置的插值的绝对值最小的index. 小的是subarray开始的前面的一个index, 大的就是结尾的index.复杂度分析:

1.求<sum,index>的pair数组时间复杂度O(n).

2.排序O(nlogn)

3.相邻的挨个比较O(n)

总体时间复杂度为O(n),空间复杂度为O(n).

注意<res,index>这种pair的技术在two pointer等题中经常用,设置key值比较的技术一定要会.代码如下:

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
    """
    def subarraySumClosest(self, nums):
        #two pointer
        res = []
        sum = 0
        for i in xrange(len(nums)):
            sum += nums[i]
            res.append((sum,i))
        res.sort(key = lambda x: x[0])

        left = -1
        right = 0
        diff = abs(res[0][0])
        for i in xrange(1,len(nums)-1):
            if abs(res[i][0] - res[i-1][0]) < diff:
                left = min(res[i-1][1], res[i][1])
                right = max(res[i-1][1], res[i][1])
                diff = abs(res[i][0] - res[i-1][0])

        return [left+1, right]
            
时间: 2024-10-27 09:50:41

Subarray Sum Closet的相关文章

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

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

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存起来.这里

LeetCode OJ 209. Minimum Size Subarray Sum

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

leetcode_209题——Minimum Size Subarray Sum(两个指针)

Minimum Size Subarray Sum Total Accepted: 10318 Total Submissions: 44504My Submissions Question Solution Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, r

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

LeetCode:Continuous Subarray Sum

523. Continuous Subarray Sum Add to List Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where

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 and the index of the last nu