Coding Interview: Find K Closest Elements

  Рет қаралды 3,987

James Cutajar

James Cutajar

Күн бұрын

Пікірлер: 7
@empowercode
@empowercode 2 жыл бұрын
Hey James, great video! Particularly, I like how clear and detailed your explanations are as well as the depth of knowledge you have surrounding the general programming approach. Since I run a tech education channel as well, I love to see fellow Content Creators sharing, educating, and inspiring a large global audience. Keep up the great work!
@shristisrivastava7054
@shristisrivastava7054 2 ай бұрын
Very clear explanation. Thanks for posting this video.
@lvkaz
@lvkaz 2 жыл бұрын
This content is so underrated! Keep up the excellent work. I hope You will get a lot of recognition soon for such great explanations!
@vijayakumareyunni6010
@vijayakumareyunni6010 Жыл бұрын
Excellent explanation and solution
@yashchoube576
@yashchoube576 Жыл бұрын
Best
@Adaetro
@Adaetro 2 жыл бұрын
Since array is ordered what if you stop the cycle when current abs_sum is higher then previous abs_sum? In the first example since abs_sum of [8 , 9, 10] = 3 and previous abs_sum of [7, 8, 9] = 2 then there is no point in calculating [9, 10, 13] since abs_sum is only going to increase from now on?
@Adaetro
@Adaetro 2 жыл бұрын
def find_closest_elements(arr, k, x): abs_list = list(map(lambda n:abs(n-x), arr)) prev_abs_sum = abs_sum_min = abs_sum = sum(abs_list[0:k]) min_i = 0 for i in range(1, len(arr)-k+1): abs_sum = abs_list[i+k-1] + abs_sum - abs_list[i-1] if prev_abs_sum < abs_sum: break if abs_sum < abs_sum_min: abs_sum_min = abs_sum min_i = i prev_abs_sum = abs_sum return arr[min_i:min_i+k], i #print number of loops print(find_closest_elements([5, 7, 8, 9, 10, 13], k=3, x=8))
Coding Interview Workshop
1:11:20
James Cutajar
Рет қаралды 6 М.
Go Lang's Mutex for Thread Synchronization
9:00
James Cutajar
Рет қаралды 2,8 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 8 МЛН
When u fight over the armrest
00:41
Adam W
Рет қаралды 28 МЛН
[Java] Leetcode 658. Find K Closest Elements [Binary Search #8]
15:06
Eric Programming
Рет қаралды 4,5 М.
Bug in Binary Search - Computerphile
11:31
Computerphile
Рет қаралды 287 М.
How I Failed the Google Coding Interview (and lessons I learned)
14:24
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 434 М.
Internationalization and localization in Web Applications
46:27
James Cutajar
Рет қаралды 2,8 М.
Subarray Sum Equals K - LeetCode 560 - Coding Interview Questions
10:38
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 672 М.
Python's Thread Barriers in 8 mins
8:50
James Cutajar
Рет қаралды 3,1 М.
Algorithms: Memoization and Dynamic Programming
11:17
HackerRank
Рет қаралды 972 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 8 МЛН