leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit d683ff4580938ce82eda3a48eed9c913977c31f6
parent cc5d4595e1de76262e5c3428f7c20b99dfec042c
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Tue, 23 May 2023 09:12:37 -0500

Completed 3sum closest problem with two pointers

Diffstat:
A3sum-closest/3sum-closestV2.dart | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)

diff --git a/3sum-closest/3sum-closestV2.dart b/3sum-closest/3sum-closestV2.dart @@ -0,0 +1,31 @@ +//Three sum closest solution using two pointers and a sorted +//list to speed up search. +//Time: 347ms Beats: 20.83% +//Memory: 148.3MB Beats: 12.50% + +class Solution { + int threeSumClosest(List<int> nums, int target) { + nums.sort(); + print(nums); + int min_dist = 10000000; + int val = 0; + for(int i = 0 ; i < nums.length - 2; ++i){ + int left = i + 1; + int right = nums.length - 1; + while(left != right){ + int current = nums[i] + nums[left] + nums[right]; + if((current - target).abs() < min_dist){ + min_dist = (current-target).abs(); + val = current; + } + if(current > target){ + right -= 1; + } + else{ + left += 1; + } + } + } + return val; + } +}