leetcode

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

commit cc5d4595e1de76262e5c3428f7c20b99dfec042c
parent d184996d2f3e898222bc2b73d0842f8aaf3e016b
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Tue, 23 May 2023 08:55:49 -0500

Completed 3sum closest using brute force dart solution

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

diff --git a/3sum-closest/3sum-closest.dart b/3sum-closest/3sum-closest.dart @@ -0,0 +1,23 @@ +//Find the three values in a list that add up to the number +//closest to the target. Once found return that number. +//This is the brute force solution which is very slow. +//Time: 3180ms Beats: 8.33% +//Memory: 158.3MB Beats: 8.33% + +class Solution { + int threeSumClosest(List<int> nums, int target) { + int closest = (nums[0] + nums[1] + nums[2] - target).abs(); + int val = nums[0] + nums[1] + nums[2]; + for(int i = 1 ; i < nums.length ; ++i){ + for(int x = i + 1; x < nums.length ; ++x){ + for(int z = x + 1; z < nums.length ; ++z){ + if((nums[i] + nums[x] + nums[z] - target).abs() < closest){ + closest = (nums[i] + nums[x] + nums[z] - target).abs(); + val = nums[i] + nums[x] + nums[z]; + } + } + } + } + return val; + } +}