Sort the Students by Their Kth Score — LeetCode 2545 Python Solution

MediumArrayMatrixSorting
Problem
#2545
Reading time
2 min

The problem

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.

Example

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.

Python solution

Python
class Solution:
    def sortTheStudents(self, score: List[List[int]], k: int) -> List[List[int]]:
        return sorted(score, key=lambda x: -x[k])

Complexity

MeasureComplexity
TimeO(m \times \log m)
SpaceO(\log m) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2545. Sort the Students by Their Kth Score is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2545. Sort the Students by Their Kth Score?
LeetCode 2545. Sort the Students by Their Kth Score is rated Medium on LeetCode.
What is the time complexity of LeetCode 2545. Sort the Students by Their Kth Score?
The Python solution on this page runs in O(m \times \log m).
What is the space complexity of LeetCode 2545. Sort the Students by Their Kth Score?
The Python solution on this page uses O(\log m) auxiliary space.
What topics does LeetCode 2545. Sort the Students by Their Kth Score cover?
LeetCode 2545. Sort the Students by Their Kth Score is tagged Array, Matrix and Sorting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview