leetcode 2545.根据第K场考试的分数排序

1.题目要求:

班里有 m 位学生,共计划组织 n 场考试。给你一个下标从 0 开始、大小为 m x n 的整数矩阵 score ,其中每一行对应一位学生,而 score[i][j] 表示第 i 位学生在第 j 场考试取得的分数。矩阵 score 包含的整数 互不相同 。

另给你一个整数 k 。请你按第 k 场考试分数从高到低完成对这些学生(矩阵中的行)的排序。

返回排序后的矩阵。

 

示例 1:



输入:score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2
输出:[[7,5,11,2],[10,6,9,1],[4,8,3,15]]
解释:在上图中,S 表示学生,E 表示考试。
- 下标为 1 的学生在第 2 场考试取得的分数为 11 ,这是考试的最高分,所以 TA 需要排在第一。
- 下标为 0 的学生在第 2 场考试取得的分数为 9 ,这是考试的第二高分,所以 TA 需要排在第二。
- 下标为 2 的学生在第 2 场考试取得的分数为 3 ,这是考试的最低分,所以 TA 需要排在第三。
示例 2:



输入:score = [[3,4],[5,6]], k = 0
输出:[[5,6],[3,4]]
解释:在上图中,S 表示学生,E 表示考试。
- 下标为 1 的学生在第 0 场考试取得的分数为 5 ,这是考试的最高分,所以 TA 需要排在第一。
- 下标为 0 的学生在第 0 场考试取得的分数为 3 ,这是考试的最低分,所以 TA 需要排在第二。
 

2.题目代码:

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** sortTheStudents(int** score, int scoreSize, int* scoreColSize, int k, int* returnSize, int** returnColumnSizes) {
    int** sort_score = (int**)malloc(sizeof(int*) * scoreSize);
    for(int i = 0;i < scoreSize;i++){
        sort_score[i] = (int*)malloc(sizeof(int) *(*scoreColSize));
    }
    int n = scoreSize;
    for(int i = 0;i < n - 1;i++){
        for(int j = 0;j < n - 1 - i;j++){
            if(score[j][k] < score[j + 1][k]){
                int* temp = (int*)malloc(sizeof(int) * (*scoreColSize));
                memcpy(temp,score[j],sizeof(int) * (*scoreColSize));
                memcpy(score[j],score[j + 1],sizeof(int) * (*scoreColSize));
                memcpy(score[j + 1],temp,sizeof(int) * (*scoreColSize)); 
            }
        }
    }
    for(int i = 0;i < scoreSize;i++){
        for(int j = 0;j < *scoreColSize;j++){
            sort_score[i][j] = score[i][j];
        }
    }
    *returnSize = scoreSize;
    *returnColumnSizes = (int*)malloc(sizeof(int) * (scoreSize));
    for(int i = 0;i < scoreSize;i++){
        (*returnColumnSizes)[i] = *scoreColSize;
    }
    return sort_score;
}

你可能感兴趣的:(leetcode,算法)