Re: [闲聊] 每日leetcode

楼主: JerryChungYC (JerryChung)   2024-07-22 13:20:10
https://leetcode.com/problems/sort-the-people
2418. Sort the People
※ 引述《enmeitiryous (enmeitiryous)》之铭言:
: 2418 sort the people
: 给定两个array:names和heights,heights[i]是names[i]的对应身高,回传根据身高排序
: 由高到矮的名字array
: 思路:根据身高倒序sort回传配对人名或是用身高当索引的map依序回传人名再颠倒
: vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
: int n=heights.size();
: map<int,string> dd;
: vector<string> ans;
: for(int i=0;i<n;++i){
: dd[heights[i]]=names[i];
: }
: for(auto j: dd){
: ans.push_back(j.second);
: }
: reverse(ans.begin(),ans.end());
: return ans;
Python Code:
class Solution:
def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
return [names[i] for i, _ in sorted(enumerate(heights), key=lambda h: h[1], reverse=True)]
剩我一开始只想到enumerate了
不过zip还是比较好看
一二三四五
作者: sustainer123 (caster)   2024-07-22 13:23:00
one liner大神

Links booklink

Contact Us: admin [ a t ] ucptt.com