Sort the People - Leetcode 2418 - Python

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


0:00 - Read the problem
0:30 - Drawing Explanation
2:59 - Coding Explanation

leetcode 2418

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Belated guru purnima Navdeep ! You're one of the best teachers I've ever had. Thank you for picking up this profession.

MP-nyep
Автор

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

aashishbathe
Автор

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

marcosfvarani
Автор

the most efficient so far was to make tuples and sort it by height in tuple.

DeathSugar
Автор

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?

rameezalipacific
Автор

What do you think is faster?
1. heights.sort(reverse = True)
2. reversed(sorted(heights))

rahulsbhatt
Автор

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

minhtungbui
Автор

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?

TheGoat
Автор

Wish there were comparators in python!
None the less, you have written clean code!

rahulsbhatt
Автор

Can't v use a heap for this question?

md_pedia
visit shbcf.ru