2545. Sort the Students by Their Kth Score LeetCode Solution
In this guide, you will get 2545. Sort the Students by Their Kth Score LeetCode Solution with the best time and space complexity. The solution to Sort the Students by Their Kth Score problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Sort the Students by Their Kth Score solution in C++
- Sort the Students by Their Kth Score solution in Java
- Sort the Students by Their Kth Score solution in Python
- Additional Resources
Problem Statement of Sort the Students by Their Kth Score
There is a class with m students and n exams. You are given a 0-indexed m x n integer matrix score, where each row represents one student and score[i][j] denotes the score the ith student got in the jth exam. The matrix score contains distinct integers only.
You are also given an integer k. Sort the students (i.e., the rows of the matrix) by their scores in the kth (0-indexed) exam from the highest to the lowest.
Return the matrix after sorting it.
Example 1:
Input: score = [[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: In the above diagram, S denotes the student, while E denotes the exam.
– The student with index 1 scored 11 in exam 2, which is the highest score, so they got first place.
– The student with index 0 scored 9 in exam 2, which is the second highest score, so they got second place.
– The student with index 2 scored 3 in exam 2, which is the lowest score, so they got third place.
Example 2:
Input: score = [[3,4],[5,6]], k = 0
Output: [[5,6],[3,4]]
Explanation: In the above diagram, S denotes the student, while E denotes the exam.
– The student with index 1 scored 5 in exam 0, which is the highest score, so they got first place.
– The student with index 0 scored 3 in exam 0, which is the lowest score, so they got second place.
Constraints:
m == score.length
n == score[i].length
1 <= m, n <= 250
1 <= score[i][j] <= 105
score consists of distinct integers.
0 <= k < n
Complexity Analysis
- Time Complexity: O(mn\log m)
- Space Complexity: O(mn)
2545. Sort the Students by Their Kth Score LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> sortTheStudents(vector<vector<int>>& score, int k) {
ranges::sort(score, [k](const vector<int>& a, const vector<int>& b) {
return a[k] > b[k];
});
return score;
}
};
/* code provided by PROGIEZ */
2545. Sort the Students by Their Kth Score LeetCode Solution in Java
class Solution {
public int[][] sortTheStudents(int[][] score, int k) {
Arrays.sort(score, (a, b) -> Integer.compare(b[k], a[k]));
return score;
}
}
// code provided by PROGIEZ
2545. Sort the Students by Their Kth Score LeetCode Solution in Python
class Solution:
def sortTheStudents(self, score: list[list[int]], k: int) -> list[list[int]]:
return sorted(score, key=lambda x: -x[k])
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.