Friday, August 14, 2015

Leetcode 16. 3Sum Closest

https://leetcode.com/problems/3sum-closest/

This problem is similar with last problem, and we can still use two side clamps. Also we use a parameter to indicate the distance to the sum.

Solution:
# T:O(n^2) S:O(1)
class Solution:
    # @return an integer
    def threeSumClosest(self, nums, target):
        nums, result, min_diff, i = sorted(nums), float("inf"), float("inf"), 0
        while i < len(nums) - 2:
            j, k = i + 1, len(nums) - 1
            while j < k:
                diff = nums[i] + nums[j] + nums[k] - target
                if abs(diff) < min_diff:
                    min_diff = abs(diff)
                    result = nums[i] + nums[j] + nums[k]
                if diff < 0:
                    j += 1
                elif diff > 0:
                    k -= 1
                else:
                    return target
            i += 1
            while i < len(nums) - 2 and nums[i] == nums[i - 1]:
                i += 1
        return result
Run Time: 96 ms

No comments:

Post a Comment