iT邦幫忙

2024 iThome 鐵人賽

DAY 16
0
佛心分享-刷題不只是刷題

轉生理工組後從零開始的leetcode刷題系列 第 16

day-16[medium.2545]sort the student by their Kth score

  • 分享至 

  • xImage
  •  

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.


題目說做一個 m(學生數)*n(考試數) 的矩陣,裡面存放的每個數字都是唯一不重複的。題目還會給一個整數K,要求根據第k次考試(k是從0開始數)成績從高到低對學生進行排序並返回矩陣。

我的解題思路:

  1. 做矩陣
  2. 遍歷矩陣的第K列,依照成績高低排序(for迴圈比大小)
  • 重點在於同一位學生的成績(同行ㄏㄤˊ的數)要綁定

class Solution {
    public int[][] sortTheStudents(int[][] score, int k) {
        int m = score.length; 
        
        for (int i = 0; i < m-1 ; i++) {
            for (int j = 0; j < m-1 - i; j++) {
                // 比較第j行和第j+1行的第k列
                if (score[j][k] < score[j + 1][k]) {
                    // 交換整行數據
                    int[] temp = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = temp;
                }
            }
        }
        return score;
    }
}

普通的完成了
https://ithelp.ithome.com.tw/upload/images/20241001/20169432jcUKv0WrwR.png
話說難度的依據到底是啥啊?
我覺得前幾天寫的那個石頭遊戲跟這題根本不同維度,明明都是中等題,這差距也太大!!


上一篇
day-15[medium.2593]find score of array after marking all element
下一篇
day-17[medium.2637]count the number of good subarrays
系列文
轉生理工組後從零開始的leetcode刷題30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言