Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21-Insange #75

Merged
merged 3 commits into from
Aug 5, 2024
Merged

21-Insange #75

merged 3 commits into from
Aug 5, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Jul 21, 2024

πŸ”— 문제 링크

Find Valid Matrix Given Row and Column Sums

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

8μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

문제 μ„€λͺ…

μž…λ ₯κ°’μœΌλ‘œ 각 ν–‰μ˜ μš”μ†Œλ“€μ„ λ”ν•œ κ°’κ³Ό 각 μ—΄μ˜ μš”μ†Œλ“€μ„ λ”ν•œ 값이 주어진닀.
Input: rowSum = [3,8], colSum = [4,7]
rowSum[0] 번째 ν–‰μ˜ μš”μ†Œλ“€μ„ λͺ¨λ‘ λ”ν•œ 값은 3이 되고
colSum[0] 번째 μ—΄μ˜ μš”μ†Œλ“€μ„ λͺ¨λ‘ λ”ν•œ 값은 4κ°€ λœλ‹€.
image
와 같이 ꡬ성이 λ˜μ–΄μ•Ό ν•œλ‹€.

첫 번째 μ ‘κ·Ό 방식

ν–‰ 인덱슀 rIndex와 μ—΄ 인덱슀 cIndexκ°€ 0λΆ€ν„° μ‹œμž‘ν•΄μ„œ ans 행렬에 ν•΄λ‹Ήν•˜λŠ” λΆ€λΆ„ ans[rindex][cindex]κ°€ μœ„μΉ˜ν•˜λŠ” λΆ€λΆ„μ˜ κ°’ 쀑 μ΅œμ†Œ 값을 λ„£μœΌλ©΄μ„œ ν•˜λ©΄ λ˜μ§€ μ•Šμ„κΉŒμ˜€λ‹€.

μ΅œμ†Œκ°’μ„ λ°°μΉ˜ν•œ 만큼 rowSumκ³Ό colSumμ—μ„œ λΉΌμ„œ ν–‰κ³Ό μ—΄μ˜ 인덱슀λ₯Ό 점차 μ¦κ°€μ‹œν‚€λŠ” λ°©μ‹μœΌλ‘œ μ ‘κ·Όν•˜μ˜€μœΌλ‚˜ λŸ°νƒ€μž„ μ—λŸ¬κ°€ λ°œμƒν•˜μ˜€λ‹€.

μ—λŸ¬λŠ” νž™ 버퍼 μ˜€λ²„ν”Œλ‘œμš°λ‘œ μ΅œμ†Œκ°’μ„ ꡬ할 λ•Œ μΈλ±μŠ€κ°€ rowSumκ³Ό colSum을 λ²—μ–΄λ‚˜λŠ” 상황이 λ°œμƒν•œ κ²ƒμ΄μ—ˆλ‹€.

μ—¬κΈ°μ„œ 문득든 생각은 μš°λ¦¬κ°€ λ§Œλ“œλŠ” 행렬은 λͺ¨λ“  수λ₯Ό μ¨μ„œ ν–‰κ³Ό μ—΄λ“€μ˜ 합듀을 μΌμΉ˜μ‹œμΌœμ€˜μ•Όν•˜λŠ”κ²Œ μ•„λ‹Œκ°€? μ˜€κ³  μ˜ˆμ™Έμ‚¬ν•­λ“€μ„ μ°Ύλ‹€λ³΄λ‹ˆ μ‹œκ°„μ΄ 였래걸리게 λ˜μ—ˆλ‹€.

풀이 방식

rowSumκ³Ό colSum을 λ‹€ νƒμƒ‰ν•˜λ˜ 배열에 값이 남을 경우 λ‹€ μ±„μ›Œμ£ΌλŠ” λ°©μ‹μœΌλ‘œ κ°€κΈ°λ‘œν•˜μ˜€λ‹€.

  1. ν•΄λ‹Ή ν–‰κ³Ό μ—΄ 인덱슀 rIndex, cIndex의 값듀을 톡해 μ΅œμ†Œκ°’μ„ νŒŒμ•…ν•˜κ³  μ—…λ°μ΄νŠΈν•œλ‹€.
  2. rowSumκ³Ό colSumμ—μ„œ 0이 λ°œμƒν•˜λ©΄ ν•΄λ‹Ήν•˜λŠ” 인덱슀의 값을 μ¦κ°€μ‹œμΌœμ€€λ‹€.
  3. λ§Œμ•½ 인덱슀의 값이 λ°°μ—΄μ˜ 크기보닀 ν¬κ±°λ‚˜ 같을 경우 λ‚˜λ¨Έμ§€ 배열을 μ‹€ν–‰μ‹œμΌœμ€€λ‹€.

image

class Solution {
public:
    vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
        vector<vector<int>> ans;
        int rsize = rowSum.size();
        int csize = colSum.size();
        int rindex = 0;
        int cindex = 0;
        
        ans.assign(rsize, vector<int>(csize, 0));
        
        while(rindex < rsize || cindex < csize)
        {
            if(rindex >= rsize)
            {
                ans[rindex-1][cindex] = colSum[cindex];
                cindex++;
                continue;
            }
            else if(cindex >= csize)
            {
                ans[rindex][cindex-1] = rowSum[rindex];
                rindex++;
                continue;
            }
            
            int val = min(colSum[cindex], rowSum[rindex]);
            
            ans[rindex][cindex] = val;
            rowSum[rindex] -= val;
            colSum[cindex] -= val;
            
            if(!rowSum[rindex])
            {
                rindex++;
            }
            if(!colSum[cindex])
            {
                cindex++;
            }
        }
        
        return ans;
    }
};

image

λ‘λ²ˆμ§Έ 풀이

λ¬Έμ œμ—μ„œλŠ” μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” 2D 배열을 λ°˜ν™˜ν•œλ‹€κ³  λ˜μ–΄μžˆλ‹€.
즉, rowSum의 μš”μ†Œλ“€μ˜ λͺ¨λ“  ν•©κ³Ό colSum의 μš”μ†Œλ“€μ˜ λͺ¨λ“  합이 κ°™μ•„μ•Ό μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λŠ” 배열이 λ°˜ν™˜μ΄ λœλ‹€.

그럼 κ²°κ΅­ μœ„μ˜ μ•Œκ³ λ¦¬μ¦˜μ„ μ§„ν–‰ν•˜λ‹€λ³΄λ©΄ 맨 λ§ˆμ§€λ§‰μœΌλ‘œ λΉ„κ΅ν•˜λŠ” 값듀은 μ„œλ‘œ 같은 값이 되며 좔가적인 연산이 ν•„μš”μ—†μ–΄μ§€κ²Œ λœλ‹€.

class Solution {
public:
    vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
        int numRows = rowSum.size();
        int numCols = colSum.size();
        vector<vector<int>> result(numRows, vector<int>(numCols, 0));

        int i = 0, j = 0;

        while (i < numRows && j < numCols) {
            int val = min(rowSum[i], colSum[j]);
            result[i][j] = val;
            rowSum[i] -= val;
            colSum[j] -= val;

            if (rowSum[i] == 0) {
                i++;
            }
            if (colSum[j] == 0) {
                j++;
            }
        }
        return result;
    }
};

image

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ‹ κΈ°ν•˜λ„€μš”.. 각 값을 0으둜 λ§Œλ“€λ©΄μ„œ λ‚΄λ €κ°€λŠ”κ²Œ κ°€μš°μŠ€ 쑰던 μ†Œκ±°λ²• 생각도 λ‚˜λŠ”λ° μ–΄λ–»κ²Œ 이런 풀이λ₯Ό λ– μ˜¬λ ΈλŠ”μ§€ μ‹ κΈ°ν•˜λ„€μš”

μ΄λ ‡κ²Œ ν’€λ©΄ 였λ₯˜κ°€ λ‚  μˆ˜λ„ μžˆμ§€ μ•Šλ‚˜..? μ‹Άμ—ˆλŠ”λ° μ–΄μ°¨ν”Ό 음수 값도 μ—†κ³  항상 κ°€λŠ₯ν•œ 경우의 수만 μ£Όμ–΄μ§€λ‹ˆ λ§ˆμ§€λ§‰μ—λŠ” 무쑰건 0이 될 수 밖에 μ—†κ²Œ λ‚¨κ² λ„€μš”.. 풀이도 풀이고 μ½”λ“œλ„ κ°„κ²°ν•˜κ³  μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

row와 col의 sumμ—μ„œ 계속 μ°¨κ°ν•˜λ©΄μ„œ ν‘œλ₯Ό κ°±μ‹ ν•΄μ£ΌλŠ” 방법이 μ°Έμ‹ ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλ ‡κ²Œ ν•˜λ©΄ ν‘œ 크기가 컀져도 μž‘μ€ ν‘œλΆ€ν„° 확정지어 λ‚˜κ°ˆ 수 μžˆμ–΄μ„œ ν™•μ‹€νžˆ 쒋은 해결법인 것 κ°™μ•„μš”! 풀이 잘 λ΄€μŠ΅λ‹ˆλ‹Ή

@InSange InSange merged commit bced723 into main Aug 5, 2024
1 check passed
@InSange InSange deleted the 21-InSange branch August 5, 2024 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants