LeetCode in Kotlin

2545. Sort the Students by Their Kth Score

Medium

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.

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.

Constraints:

Solution

import java.util.Arrays

class Solution {
    fun sortTheStudents(score: Array<IntArray>, k: Int): Array<IntArray> {
        Arrays.sort(score) { o1: IntArray, o2: IntArray -> o2[k] - o1[k] }
        return score
    }
}