Advertisement

Sort the Students by Their Kth Score - LeetCode 2545 Solution

Sort the Students by Their Kth Score - Complete Solution Guide

Sort the Students by Their Kth Score is LeetCode problem 2545, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 i th student got in the j th 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 k th ( 0-indexed ) exam from the highest to the lowest. Return the matrix after sorting it. Example 1: Input: score = [[10,6,9,1],[7

Detailed Explanation

The problem asks us to sort a matrix (representing student scores on different exams) based on the scores obtained by the students in a specific exam (indicated by the index 'k'). The goal is to rearrange the rows of the matrix so that students with higher scores in the k-th exam come before students with lower scores. The input is the matrix `score` and the index `k` of the exam to sort by. The output is the sorted matrix.

Solution Approach

The provided solutions utilize the built-in sorting functions available in their respective languages, along with a custom comparator. The comparator compares two rows based on the value at the k-th column. The sorting is done in descending order, placing the rows with larger values in the k-th column earlier in the sorted matrix. The qsort_r function in the C code performs this task.

Step-by-Step Algorithm

  1. Step 1: Define a comparison function that takes two rows and the column index `k` as input.
  2. Step 2: Inside the comparison function, compare the elements at the k-th index of the two rows.
  3. Step 3: The comparison function returns a negative value if the first row should come before the second, a positive value if the second row should come before the first, and zero if they are equal. Since we want descending order, we subtract the element of row A from the element of row B (B[k]-A[k] for Java and C, A[k] > B[k] in C++). Python uses a lambda function for this concisely.
  4. Step 4: Use the language's built-in sorting function (e.g., `Arrays.sort` in Java, `sort` in C++, `sorted` in Python, `qsort_r` in C) with the matrix and the custom comparison function as arguments.
  5. Step 5: Return the sorted matrix.

Key Insights

  • Insight 1: We need to sort the rows of the matrix based on the values in the k-th column.
  • Insight 2: A custom sorting function (comparator) is required to compare rows based on the element at the k-th index.
  • Insight 3: Most languages provide built-in sorting functionalities that can accept a custom comparison function, simplifying the implementation.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting, Matrix.

Companies

Asked at: IBM.