LeetCode·One question per day·2500. Delete the maximum value in each row·Simulation

Author: Xiao Xun
Link: https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/2360713/mo-ni-zhu-shi-chao-ji-xiang-xi-by- xun-ge-rhmz/
Source: LeetCode
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

 

example

 

train of thought

Question meaning -> Given a two-dimensional array, sum according to the given requirements.

  • Take a maximum value from each row and delete the changed element
  • Take a maximum sum from the above maximum values
  • Repeat the above operation until the array is empty
  • return cumulative value

The topic has been very clear, and the simulation is carried out directly according to the meaning of the topic. First, enumerate each row, take out the maximum value of the row, and set the position element to -1, which means deleting the modified element, and then set the maximum value of each row The values ​​are compared with the global maximum value, the current overall maximum element value is saved, and accumulated, and finally the accumulated value is returned.

Code comments are super detailed

the code

int deleteGreatestValue(int** grid, int gridSize, int* gridColSize){
    int sum = 0;
    for (int i = 0; i < gridColSize[0]; ++i) {//枚举删除次数
        int count = -1;//全局最大值
        for (int n = 0; n < gridSize; ++n) {//枚举行
            int max = -1;
            int temp_i = 0;
            int temp_j = 0;
            for (int m = 0; m < gridColSize[0]; ++m) {//枚举该行中的最大值
                if (max < grid[n][m]) {//保存最大值和位置
                    max = grid[n][m];
                    temp_i = n;
                    temp_j = m;
                }
            }
            grid[temp_i][temp_j] = -1;//最大值删除
            count = fmax(count, max);//保存每行最大值
        }
        sum += count;
    }
    return sum;
}

作者:小迅
链接:https://leetcode.cn/problems/delete-greatest-value-in-each-row/solutions/2360713/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-rhmz/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/131953102