Practice/Apple/Leetcode 2545. Sort the Students by Their Kth Score
CodingMust
You are given a 2D integer matrix scores where each row represents a student and each element in that row represents their score on a particular exam. You need to sort the students (rows) based on their performance in a specific exam identified by column index k, arranging them in descending order (highest score first).
Return the reordered matrix after sorting by the k-th exam score.
kk are distinct, so no tie-breaking logic is neededm == scores.length (number of students)n == scores[i].length (number of exams)1 <= m, n <= 2500 <= k < n1 <= scores[i][j] <= 10^5k are uniqueExample 1:
Input: scores = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2 Output: [[7,5,11,2],[10,6,9,1],[4,8,3,15]] Explanation: The scores in column 2 are [9, 11, 3]. After sorting in descending order by these values, the rows are reordered as shown.
Example 2:
Input: scores = [[3],[2],[5]], k = 0 Output: [[5],[3],[2]] Explanation: Sorting by the single column yields [5, 3, 2] in descending order.
Example 3:
Input: scores = [[50,40,30],[25,35,45]], k = 1 Output: [[50,40,30],[25,35,45]] Explanation: Column 1 has values [40, 35]. Since 40 > 35, the first row remains first.