3sum Smaller - Teeeeeebag/LeetCode GitHub Wiki

  1. mistake: I was trying to increase start as much as I can at the very beginning
public class Solution {
    public int threeSumSmaller(int[] nums, int target) {
        Arrays.sort(nums);
        int cnt = 0;
        for (int i=0; i<nums.length; ++i){
            int start = i+1, end = nums.length-1;
            while(start < end){
                if(nums[i] + nums[start] + nums[end] < target){
                    cnt += end-start;
                    ++start;
                } else {
                    --end;
                }
            }
        }
        return cnt;
    }
}