LeetCode 3Sum Closest Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

after calculating current_sum, just checking if(current_sum==target) return sum; also makes it faster in situations where target can be found.

ersinerdem
Автор

Try adding if(i > 0 && numbers[i-1] == numbers[i])
continue; to line 7 to bypass identical sequential numbers => huge performance boost

georgechen
Автор

was there any particular reason for setting the initial value of the result like you did? Also, would it matter if we sort before or after initializing result?

meghachauhan
Автор

Y u sorted after setting initial result

narendrakothamire
Автор

Suggestion: Please keep a link of the problem in the video description.

heisenberg
Автор

Why did we use abs method to compare currSum and result?

girishkrishna
Автор

can anyone explain why we are taking length - 2 on line 6

vishaljaiswal
Автор

the case of [0, 1, 2] and target 3 gives time limit exceeded

reemabdelrazek
Автор

what if you can't use inbuild sort function?
This is my solution (python version):
def three_closest(given, x):
if len(given) == 0:
return 0
if len(given)==1:
return given[0]
if len(given)==2:
return given[0] + given[1]
if len(given)==3:
return given[0] + given[1] + given[3]

heap = Heap([999, 999], [999, 999], [999, 999])
for i, numb in enumerate(given):
score = abs(x - numb)
score_pos = [score, i]
if (heap.val[0] > score):
tmp = heap.val
heap.val = score_pos
tmp_2 = heap.left
heap.left = tmp
heap.right = tmp_2
elif (heap.left[0] > score):
tmp = heap.left
heap.left = score_pos
heap.right = tmp
elif (heap.right[0] > score):
heap.right = score_pos
heap_sum = given[heap.val[1]] + given[heap.left[1]] + given[heap.right[1]]
return heap_sum

jugsma
Автор

This algorithm fails now actually. Unless you change the differences to a positive integer before comparing

frimpongopokuagyemang
Автор

Could you have taken the initial result to be any large value than summing 0, 1 and last index value and it would work fine?

Nobody
Автор

This problem is not specified well. They specify that it's basically N choose 3, with minimum error to target, but they don't specify with or without replacement. It *does* say you can assume there's one solution, but with replacement then -1, -1, 2=0 is equally close to the target 1 (i.e. two solutions). So the problem is underspecified. They should have said choosing 3 different element offsets without replacement or somesuch.

Gideon_Judges
Автор

Please find the error it is same as java one...
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
result = nums[0] + nums[1]+nums[len(nums)-1]
nums.sort()
for i in range(len(nums)-2):
a = i+1
b = len(nums)-1

while a<b:
current = nums[i] + nums[a] + nums[b]
# if current == target:
# return target
if current > target:
b-=1
else:
a-=1
if math.fabs(target-current) < math.fabs(target-result):
result = current


return result

ameyamahadevgonal
Автор

Merci, mais rien dans votre video n'est argumente !

salamanas