LeetCode 2545: Sort the Students by Their Kth Score

ArraySortingMatrix

Problem Description

Explanation

To solve this problem, we need to sort the students based on their scores in the kth exam. We can achieve this by using a custom comparator to sort the rows of the matrix based on the kth element in each row.

  1. Create a custom comparator that compares rows based on the kth element.
  2. Sort the rows of the matrix using the custom comparator.
  3. Return the sorted matrix.

Time Complexity

The time complexity of this approach is O(m log m) where m is the number of students.

Space Complexity

The space complexity is O(1) as we are sorting the matrix in-place.

Solutions

class Solution {
    public int[][] sortStudentsByScore(int[][] score, int k) {
        Arrays.sort(score, (a, b) -> Integer.compare(b[k], a[k]));
        return score;
    }
}

Loading editor...