Sort the People - Leetcode 2418 - Python

  Рет қаралды 5,940

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 17
@pratyushthakur8478
@pratyushthakur8478 3 ай бұрын
This was refreshing after yesterday
@MP-ny3ep
@MP-ny3ep 3 ай бұрын
Belated guru purnima Navdeep ! You're one of the best teachers I've ever had. Thank you for picking up this profession.
@aashishbathe
@aashishbathe 3 ай бұрын
A 1 liner solution for python peeps - return [name for height, name in sorted(zip(heights, names), reverse=True)] Selection sort code - curr = 0 curr_height = heights[0] while curr < len(names) - 1: tmp, tmp_height = curr, heights[curr] for i in range(curr + 1, len(names)): if heights[i] > tmp_height: tmp = i tmp_height = heights[i] heights[tmp], heights[curr] = heights[curr], heights[tmp] names[tmp], names[curr] = names[curr], names[tmp] curr += 1 return names
@marcosfvarani
@marcosfvarani 3 ай бұрын
Today the problem actually shows an example with non-distinct names! I think the best approach now is to use a tuple of name and heights rather than a map
@DeathSugar
@DeathSugar 3 ай бұрын
the most efficient so far was to make tuples and sort it by height in tuple.
@rameezalipacific
@rameezalipacific 3 ай бұрын
I'm from Pakistan and currently pursuing a degree in software engineering. Alongside my studies, I'm dedicating significant time to mastering data structures and algorithms (DSA). However, I'm uncertain if excelling in DSA alone is enough to secure a job at top tech companies like the one you work for. Do I need to develop additional skills, such as web, app, or game development, to increase my chances of success? Also, could you share your experience on when you started focusing on DSA-was it during your degree or afterward?
@TheGoat1839
@TheGoat1839 3 ай бұрын
class Solution: def sortPeople(self, names: List[str], heights: List[int]) -> List[str]: number_of_people = len(names) # Create a dictionary to store height-name pairs height_to_name_map = dict(zip(heights, names)) sorted_heights = sorted(heights, reverse=True) # Create a list of sorted names based on descending heights sorted_names = [height_to_name_map[height] for height in sorted_heights] return sorted_names is this a better solution , or your solution has less complexcity?
@rahulsbhatt
@rahulsbhatt 3 ай бұрын
What do you think is faster? 1. heights.sort(reverse = True) 2. reversed(sorted(heights))
@NeetCodeIO
@NeetCodeIO 3 ай бұрын
Probably first since it's in place, but I don't think the second actually reverses the array it just returns an iterator in reverse order
@minhtungbui2002
@minhtungbui2002 3 ай бұрын
Thank you so much for your solution. In this problem I used pair and lambda function to solve it and it's also very nice:v
@md_pedia1
@md_pedia1 3 ай бұрын
Can't v use a heap for this question?
@aashishbathe
@aashishbathe 3 ай бұрын
@@md_pedia1 yeah we can, but then we need O(n) to combine the elements and heapify them. Also O(nlogn) for popping elements and getting result.
@md_pedia1
@md_pedia1 3 ай бұрын
@@aashishbathe it didnt occur to me at that time😅
@31redorange08
@31redorange08 3 ай бұрын
What's v?
@rahulsbhatt
@rahulsbhatt 3 ай бұрын
Wish there were comparators in python! None the less, you have written clean code!
@chrischika7026
@chrischika7026 3 ай бұрын
John is a short King
Sort Array by Increasing Frequency - Leetcode 1636 - Python
8:17
Sort the Jumbled Numbers - Leetcode 2191 - Python
12:37
NeetCodeIO
Рет қаралды 8 М.
Motorbike Smashes Into Porsche! 😱
00:15
Caters Clips
Рет қаралды 23 МЛН
Shortest Subarray with Sum at Least K - Leetcode 862 - Python
27:57
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 228 М.
WHY did this C++ code FAIL?
38:10
The Cherno
Рет қаралды 292 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 326 М.
10 FORBIDDEN Sorting Algorithms
9:41
Ardens
Рет қаралды 918 М.
Extra Characters in a String - Leetcode 2707 - Python
21:11
NeetCodeIO
Рет қаралды 18 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 814 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 727 М.
Lucky Numbers in a Matrix - Leetcode 1380 - Python
16:21
NeetCodeIO
Рет қаралды 8 М.