Leetcode 2545: Sort the Students by Their Kth Score

grid47
grid47
Exploring patterns and algorithms
Feb 26, 2024 7 min read

You are given a matrix of scores, where each row represents a student and each column represents an exam. You need to sort the students based on their scores in the k-th exam, from the highest to the lowest. Return the sorted matrix.
Problem
Approach
Steps
Complexity
Input: You are given an m x n matrix `score` representing the scores of m students in n exams. You are also given an integer k, the index of the exam to use for sorting.
Example: score = [[20, 5, 11], [10, 9, 14], [15, 18, 7]], k = 1
Constraints:
• 1 <= m, n <= 250
• 1 <= score[i][j] <= 10^5
• score contains distinct integers
• 0 <= k < n
Output: Return the matrix of students sorted by their scores in the k-th exam from highest to lowest.
Example: [[15, 18, 7], [10, 9, 14], [20, 5, 11]]
Constraints:
Goal: Sort the students based on the score in the k-th exam.
Steps:
• 1. Extract the score for the k-th exam for each student.
• 2. Sort the students based on their k-th exam score in descending order.
• 3. Return the sorted matrix.
Goal: Ensure the solution handles the constraints efficiently, especially for the upper bounds of m and n.
Steps:
• 1 <= m, n <= 250
• 1 <= score[i][j] <= 10^5
• score consists of distinct integers
• 0 <= k < n
Assumptions:
• All exam scores are distinct across students.
• The number of students and exams are within the given constraints.
Input: score = [[20, 5, 11], [10, 9, 14], [15, 18, 7]], k = 1
Explanation: In this example, the students are sorted by their score in the second exam (k = 1). The sorted order is based on the scores: 18, 9, and 5.

Input: score = [[30, 40], [20, 50], [10, 30]], k = 0
Explanation: Here, the sorting is done by the scores in the first exam (k = 0), resulting in the order: 30, 20, and 10.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus