MediumNo. 2545

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

Topics
Array, Matrix, Sorting
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

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

Complexity

Time
O(m × log m)
Space · auxiliary
O(log m)

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

Related problems
No.ProblemDifficulty
1030Matrix Cells in Distance OrderEasy
1329Sort the Matrix DiagonallyMedium
2033Minimum Operations to Make a Uni-Value GridMedium
0056Merge IntervalsMedium
0164Maximum GapMedium
0274H-IndexMedium

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 × 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.

Free to download. Check it runs on your machine, then pick a plan.