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

22-InSange #81

Merged
merged 4 commits into from
Aug 9, 2024
Merged

22-InSange #81

merged 4 commits into from
Aug 9, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Jul 31, 2024

πŸ”— 문제 링크

Filling Bookcase Shelves

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

12μ‹œκ°„

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

문제 μ„€λͺ…

μž…λ ₯으둜 주어진 books 2차원 λ°°μ—΄μ˜ μš”μ†ŒλŠ” 첫 번째 μš”μ†Œλ‘œ λ„ˆλΉ„λ₯Ό, 두 번째 μš”μ†Œλ‘œ 높이λ₯Ό μ§€λ‹Œλ‹€.
μˆœμ„œλŒ€λ‘œ 책을 μ„ λ°˜μ— λ†“λ˜ 각 μ„ λ°˜μ— λ†“μ΄λŠ” 책은 μž…λ ₯으둜 주어진 shelfWidth보닀 μž‘κ±°λ‚˜ κ°™μ•„μ•Ό 놓을 수 μžˆλ‹€.
κ·Έλ ‡κ²Œ ν•΄μ„œ 배치λ₯Ό ν•˜μ˜€μ„ λ•Œ μ΅œμ†Œμ˜ 높이λ₯Ό κ΅¬ν•˜λΌ.

μ ‘κ·Ό 방식

μˆœμ„œλŒ€λ‘œ 차곑차곑 dpλ°°μ—΄ heightArr에 λ„£μ–΄μ£Όλ˜ ν˜„μž¬ κ°’μ—μ„œ 이전 κ°’λ“€μ˜ dpλ°°μ—΄ μš”μ†Œμ— λ°©λ¬Έν•΄μ€€λ‹€.
즉 비ꡐ 방식은 μ•„λž˜μ™€ 같이 이루어진닀.

  1. i-1번째 μš”μ†Œμ˜ μ΅œμ†Œ 높이 값을 μ €μž₯ν•˜λŠ” heightArr[i]에닀가 μ΄μ „μ˜ μ΅œμ†Œ κ°’ heightArr[i-1]κ³Ό ν˜„μž¬ μš”μ†Œ i-1의 높이λ₯Ό λ”ν•œ κ°’μœΌλ‘œ μ΄ˆκΈ°ν™” ν•΄μ€€λ‹€.
  2. 즉, 각기 λ‹€λ₯Έ μ„ λ°˜μ— λ†“μ•˜μ„ λ•Œ κ΅¬ν•΄μ§€λŠ” 높이이닀.
  3. i-2번째 μš”μ†ŒλΆ€ν„° 0번째 μš”μ†ŒκΉŒμ§€ 비ꡐ해 쀄 것이닀.
  4. μš°μ„  ν˜„μž¬ i-1번째 λ„ˆλΉ„μ™€ j-1번째 μš”μ†Œμ˜ λ„ˆλΉ„λ₯Ό ν•©ν–ˆμ„ λ•Œ shelfWidth보닀 크면 λ°”λ‘œ 비ꡐλ₯Ό μ’…λ£Œν•΄μ€€λ‹€.
  5. 쑰건이 좩적될 경우. μΆ”κ°€ν•΄μ€€ λŒ€μƒ j-1번째 μš”μ†Œμ˜ 높이와 ν˜„μž¬ μ΅œλŒ€ 높이λ₯Ό μ§€λ‹ˆκ³  μžˆλŠ” λ³€μˆ˜ height값을 κ°±μ‹ ν•΄μ€€λ‹€. 즉, 같은 μ„ λ°˜μ— μœ„μΉ˜ν•  경우 높은 height의 κ°’μœΌλ‘œ λ°”λ€Œλ©΄μ„œ ν•΄λ‹Ή μ„ λ°˜μ΄ μ°¨μ§€ν•˜λŠ” 높이λ₯Ό κ°€λ¦¬ν‚€κ²Œ λœλ‹€.
  6. 높이 값을 κ΅¬ν–ˆμ„ λ•Œ 같은 μ„ λ°˜μ— μœ„μΉ˜ μ‹œν‚€λŠ”κ²Œ μ΅œμ†ŒμΈμ§€ (κ·Έλƒ₯ heightArr[i]), μ•„λ‹ˆλ©΄ 각기 λ‹€λ₯Έ μ„ λ°˜μœΌλ‘œ λΆ„λ₯˜ν•˜λŠ”κ²Œ μ΅œμ†ŒμΈμ§€(heightArr[j-1]+height) 비ꡐ해쀀닀.
class Solution {
public:
    int minHeightShelves(vector<vector<int>>& books, int shelfWidth) {
        int arrSize = books.size();
        vector<int> heightArr(arrSize + 1, 0);
        
        for(int i = 1; i <= arrSize; i++)
        {
            int width = books[i-1][0];
            int height = books[i-1][1];
            
            heightArr[i] = heightArr[i-1] + height; // check Next Floor
            for(int j = i-1; j > 0; j--)
            {
                if(width + books[j-1][0] > shelfWidth) break;
                width += books[j-1][0]; // check the same Floor
                height = max(height, books[j-1][1]);    // same Floor height compare with before book height
                heightArr[i] = min(heightArr[i], heightArr[j-1] + height);  // where make min
            }
        }
        
        return heightArr[arrSize];
    }
};

ν›„κΈ°

문제λ₯Ό μ΄ν•΄ν•˜λŠ”λ° 어렀움을 κ²ͺμ—ˆλ‹€...

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

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.

문제 이해에 μ‹œκ°„μ΄ 많이 κ±Έλ ΈμŠ΅λ‹ˆλ‹€... 처음 봀을 λ•ŒλŠ” 2차원 λ°°μ—΄λ‘œ λΉ„κ΅ν•΄μ•Όν•˜μ§€ μ•Šλ‚˜? μ‹Άμ—ˆλŠ”λ° μˆœμ„œλŒ€λ‘œ λ‘μ–΄μ•Όν•œλ‹€λŠ” 쑰건이 μžˆμ—ˆλ„€μš”

λ¬Έμ œκ°€ λ³΅μž‘ν•œ 것 치곀 μ»΄νŒ©νŠΈν•œ μ½”λ“œλΌ μ½λŠ” 맛이 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ γ…Žγ…Ž

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.

shelfwidth 와 λΉ„κ΅ν•΄μ£Όλ©΄μ„œ 차곑차곑 μŒ“μ•„λ‚˜κ°€λŠ” 풀이 잘 λ΄€μŠ΅λ‹ˆλ‹€. μ‰¬μš΄ 것 κ°™μœΌλ©΄μ„œλ„ ν—·κ°ˆλ¦¬λŠ” λ¬Έμ œμ˜€λ„€μš”.

@InSange InSange merged commit 9541186 into main Aug 9, 2024
1 check passed
@InSange InSange deleted the 22-InSange branch August 9, 2024 17:02
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