[Leetcode Daily Question] 1605. Given the sum of rows and columns, find a feasible matrix (medium)

1605. Find a Feasible Matrix Given the Sum of Rows and Columns


Simulation will do. We traverse the sum of each row, limit the sum of the current column, regard this sum as a capacity, and divide the value as much as possible each time. For example rowSum = [3,8], colSum = [4,7]. The sum of the first row is 3, and the sum of the first column is 4, so we assign all 3 to the first row and first column. Correspondingly, the remaining capacity of the first column should be changed to 1. The values ​​in the first row are all allocated, so the rest are all 0; then the second row is allocated, and the value in the second row is 8, but the remaining capacity of the first column is 1, so the first column of the second row can only be 1 is assigned, the remaining values ​​are assigned to the remaining columns, and so on.
As for why it is right to do this, because the title guarantees that there must be a solution, and the sum of ranks and columns is calculated based on a certain result. It is of course a solution and correct to use the results to push the initial state according to the constraints.

class Solution {
    
    
public:
    vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
    
    
        int m=rowSum.size();
        int n=colSum.size();
        vector<vector<int>> res(m,vector<int> (n,0));
        for(int i=0;i<m;i++){
    
    
            for(int j=0;j<n;j++){
    
    
                if(rowSum[i]>colSum[j]){
    
    
                    res[i][j]=colSum[j];
                    rowSum[i]-=colSum[j];
                    colSum[j]=0;
                }
                else{
    
    
                    res[i][j]=rowSum[i];
                    colSum[j]-=rowSum[i];
                    rowSum[i]=0;
                    break;
                }
            }
        }
        return res;
    }
};



Bodhisattva Man【Nalan Xingde in Qing Dynasty】
The peach stamens in front of the window are as delicate as tired, and the Dongfeng tears wash the rouge face. People are in Xiaohonglou, singing "Shizhou" from love.
At night, Shuangyan stays, the back of the lamp and the waist of the screen are green. The fragrance is gone and the rain is fading, and the thin quilt is not cold.

  1. Shizhou: Refers to the name of Shang tune, one of the seven Yuefu tunes. The sound of Shang Diao is sad and mournful, expressing desolate and sad feelings. Li Shangyin's "Gift on behalf": "The sunrise in the southeast shines on the tall building, and the people upstairs sing "Shizhou"."
  2. Green on the back of the lamp and on the waist of the screen: Green refers to the shiny black color, which is often used to describe black hair in ancient poems. For example, Li Shangyin of the Tang Dynasty wrote in "Thirty-two Rhymes of the Cao Pavilion on the Play Title Shuyan": "Every age is young and strong, and the green teeth are still uniform." Song Yanji said in "Sheng Chazi": "Your appearance is not red, and my temples are not green. "But here it is extended to be dim and unclear. That is to say, Shuangyan sleeps with a lamp behind her back, and when her two figures fall in the middle of the screen (the waist of the screen), they will present a dim and unclear scene.
  3. The rain is fading: the rain is about to end. Song Hezhu's "Little Heavy Mountain": "The song is broken and the wine is waning, the painting boat is spinning, and the flute is spinning, and the green poplar bay."

Guess you like

Origin blog.csdn.net/qq_44623371/article/details/129538826