simple n^2 Ruby solution


  • 0
    T
    # @param {Integer[]} nums
    # @param {Integer} target
    # @return {Integer}
    def three_sum_smaller(nums, target)
        count = 0
        nums = nums.sort
        len = nums.length
        (0...nums.length).each do |i|
            left = i + 1
            right = len - 1
            while left < right
              if(nums[i] + nums[left] + nums[right] < target)
                count += right - left
                left += 1
              else
                right -= 1
              end
            end
        end
        return count
    end
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.