leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

3sum-closestV2.dart (792B)


      1 //Three sum closest solution using two pointers and a sorted
      2 //list to speed up search.
      3 //Time: 347ms Beats: 20.83%
      4 //Memory: 148.3MB Beats: 12.50%
      5 
      6 class Solution {
      7   int threeSumClosest(List<int> nums, int target) {
      8     nums.sort();
      9     print(nums);
     10     int min_dist = 10000000;
     11     int val = 0;
     12     for(int i = 0 ; i < nums.length - 2; ++i){
     13       int left = i + 1;
     14       int right = nums.length - 1;
     15       while(left != right){
     16           int current = nums[i] + nums[left] + nums[right];
     17           if((current - target).abs() < min_dist){
     18               min_dist = (current-target).abs();
     19               val = current;
     20           }
     21           if(current > target){
     22               right -= 1;
     23           }
     24           else{
     25               left += 1;
     26           }
     27       }
     28     }
     29     return val;
     30   }
     31 }