2418 - Sort the People
JAVA
class Solution { public String[] sortPeople(String[] names, int[] heights) { int n = names.length; Integer[] idx = new Integer[n]; for (int i = 0; i < n; ++i) { idx[i] = i; } Arrays.sort(idx, (i, j) -> heights[j] - heights[i]); String[] ans = new String[n]; for (int i = 0; i < n; ++i) { ans[i] = names[idx[i]]; } return ans; } }
C++
class Solution { public: vector<string> sortPeople(vector<string>& names, vector<int>& heights) { int n = names.size(); vector<int> idx(n); iota(idx.begin(), idx.end(), 0); sort(idx.begin(), idx.end(), [&](int i, int j) { return heights[j] < heights[i]; }); vector<string> ans; for (int i : idx) { ans.push_back(names[i]); } return ans; } };
Comments
Post a Comment