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

20-InSange #74

Merged
merged 2 commits into from
Aug 4, 2024
Merged

20-InSange #74

merged 2 commits into from
Aug 4, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Jul 17, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

Robot Collisions

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

5์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํ’€์ด ๋ฐฉ๋ฒ•

position๊ณผ healths ๊ทธ๋ฆฌ๊ณ  directions๊ฐ€ ์ค€๋‹ค. ๊ฐ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋Š” ํ•˜๋‚˜์˜ ๋กœ๋ด‡์˜ ์œ„์น˜, ์ฒด๋ ฅ, ๋ฐฉํ–ฅ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
image
๋กœ๋ด‡์€ directions์— ์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ€๊ฒŒ๋˜๋ฉฐ ๋ถ€๋”ซํžˆ๋Š” ๊ฒฝ์šฐ. ์ฒด๋ ฅ์ด ์ ์€ ๋กœ๋ด‡์€ healths๊ฐ€ 0์ด ๋˜๊ณ  ์ฒด๋ ฅ์ด ๋งŽ์€ ๋กœ๋ด‡์€ 1 ๊นŽ์ธ๋‹ค.
๊ฐ™์„ ๊ฒฝ์šฐ์—๋Š” ๋‘˜๋‹ค healths๊ฐ€ 0์ด ๋œ๋‹ค.

์ฒซ๋ฒˆ์งธ ํ’€์ด๋ฐฉ์‹

์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋กœ๋ด‡๋“ค์„ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋„ฃ์–ด์ค€๋‹ค.
์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋กœ๋ด‡์€ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ,
์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๋กœ๋ด‡์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค.
R = { 5, 4, 3, 2, 1 }
L = { 6, 7, 9, 10}
๊ณผ ๊ฐ™์€ ํ˜•์‹.

  1. R ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๊ฐ€ L ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๋ณด๋‹ค ํฌ๋ฉด ์Šคํƒ์—๋‹ค๊ฐ€ ์Œ“์•„๋‘”๋‹ค. ( R ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๊ฐ€ ํด ๊ฒฝ์šฐ L ๋กœ๋ด‡๊ณผ ๋ถ€๋”ซํž ๊ฒฝ์šฐ๊ฐ€ ์—†์Œ. )
  2. ๋งŒ์•ฝ R ๋กœ๋ด‡ ์šฐ์„ ์ˆœ์œ„ํ๊ฐ€ ๋น„์–ด์ง„๋‹ค๋ฉด? L ๋กœ๋ด‡๋ณด๋‹ค ๋ชจ๋‘ ์ธ๋ฑ์Šค๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ๋ถ€๋”ซํž ์ผ์ด ์—†์–ด์ง„๋‹ค!
  3. ๊ทธ๊ฒŒ ์•„๋‹ ๊ฒฝ์šฐ. R ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๊ฐ€ L ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๋ณด๋‹ค ์ž‘์•„์„œ ์„œ๋กœ ๋ถ€๋”ซํžˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ„์‚ฐํ•ด์ค€๋‹ค.
  4. ์ฒด๋ ฅ์ด ๋†’์€ ์ชฝ์€ -1, ๋‚ฎ์€ ์ชฝ์€ 0์œผ๋กœ healths๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.
  5. ์ฒด๋ ฅ์ด ๊ฐ™์„ ๊ฒฝ์šฐ ๋‘˜ ๋‹ค 0์œผ๋กœ ๋ณ€๊ฒฝ
  6. 3 ~ 5๊ฐ€ ๋๋‚˜๋ฉด ์Šคํƒ์— ๋„ฃ์–ด์ ธ์žˆ๋Š” R ๋กœ๋ด‡๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ๋‹ค์‹œ ์ง‘์–ด ๋„ฃ์–ด์ค€๋‹ค.
  7. healths์˜ ๊ฐ’์ด 0์ด ์•„๋‹Œ ๊ฐ’๋“ค์„ ans ๋ฐฐ์—ด์—๋‹ค๊ฐ€ ์ง‘์–ด๋„ฃ์–ด์ค€๋‹ค.
class Solution {
public:
    vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
        vector<int> ans;
        
        priority_queue<pair<int, int>> RIndexArr;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> LIndexArr;
        
        for(int i = 0; i < directions.length(); i++)
        {
            if(directions[i] == 'R') RIndexArr.push({positions[i], i});
            else LIndexArr.push({positions[i], i});
        }
        
        stack<pair<int, int>> temp;
        while(!RIndexArr.empty() && !LIndexArr.empty())
        {
            cout << RIndexArr.top().first << ", " << LIndexArr.top().first << "\n";
            
            while(!RIndexArr.empty() && RIndexArr.top().first > LIndexArr.top().first)
            {
                temp.push(RIndexArr.top());
                RIndexArr.pop();
            }
            // ๋น„์–ด์žˆ์„ ๋•Œ -> ๋น„๊ตํ• ๊ฒŒ ์—†์œผ๋ฉด? Lํ†ต๊ณผ
            if(RIndexArr.empty())
            {
                LIndexArr.pop();
            }
            else
            {
                int LIndex = LIndexArr.top().second;
                int RIndex = RIndexArr.top().second;

                // R์˜ HP๊ฐ’์ด ํฌ๋‹ค๋ฉด?
                if(healths[LIndex] < healths[RIndex])
                {
                    healths[RIndex]--;
                    healths[LIndex] = 0;
                    LIndexArr.pop();
                }
                else if(healths[LIndex] == healths[RIndex])
                {   // ๊ฐ™๋‹ค๋ฉด?
                    healths[RIndex] = 0;
                    healths[LIndex] = 0;
                    LIndexArr.pop();
                    RIndexArr.pop();
                }
                else
                {   // L์ด ํฌ๋‹ค๋ฉด?
                    healths[RIndex] = 0;
                    healths[LIndex]--;
                    RIndexArr.pop();
                }
            }
            
            // R ๋‹ค์‹œ ์ง‘์–ด๋„ฃ๊ธฐ.
            while(!temp.empty())
            {
                RIndexArr.push(temp.top());
                temp.pop();
            }
        }
        
        for(int val : healths)
        {
            if(val) ans.push_back(val);
        }
        
        return ans;
    }
};

image
๊ฒฐ๊ณผ๋Š” ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ ๊ฐ’์ด ์กด์žฌํ•  ๊ฒฝ์šฐ. ๋‹ค์‹œ ์ง‘์–ด๋„ฃ๊ณ  ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์€ ๊ฒฐ๊ตญ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•œ๋‹ค.

๋‘๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•

positions์™€ ์ธ๋ฑ์Šค๋ฅผ ํ•จ๊ป˜ ๋ฌถ์–ด์„œ positions์„ ๊ธฐ์ค€์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์ค€๋‹ค.
R์€ ์Šคํƒ์—๋‹ค๊ฐ€ ์ง‘์–ด๋„ฃ๊ณ  L์„ ๋งŒ๋‚ฌ์„ ๊ฒฝ์šฐ์— ์Šคํƒ์—์„œ R๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๊บผ๋‚ด ๋น„๊ตํ•œ๋‹ค.

class Solution {
public:
    vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
        vector<int> ans;

        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> IndexArr;
        stack<pair<int, int>> Rst;
        
        for(int i = 0; i < positions.size(); i++)
        {
            IndexArr.push({positions[i], i});
        }
        
        while(!IndexArr.empty())
        {
            int val = IndexArr.top().first;
            int index = IndexArr.top().second;
            
            cout << val << ", " << index << "\n";
            IndexArr.pop();
            
            if(directions[index] == 'L')
            {
                while(!Rst.empty())
                {
                    int stVal = Rst.top().first;
                    int stIndex = Rst.top().second;
                    
                    if(healths[stIndex] == healths[index])
                    {
                        healths[stIndex] = 0;
                        healths[index] = 0;
                        Rst.pop();
                        break;
                    }
                    else if(healths[stIndex] > healths[index])
                    {
                        healths[index] = 0;
                        healths[stIndex]--;
                        break;
                    }
                    else
                    {
                        healths[stIndex] = 0;
                        healths[index]--;
                        Rst.pop();
                    }
                }
            }
            else
            {
                Rst.push({val, index});
            }
        }
        
        for(int health : healths)
        {
            if(health) ans.push_back(health);
        }
        
        return ans;
    }
};

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.

์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์˜€๋„ค์š”! ์–‘ ์ชฝ์„ ์ „๋ถ€ ์Šคํƒ์— ๋„ฃ์ง€ ์•Š๊ณ  ํ•œ ์ชฝ๋งŒ ์Šคํƒ์— ๋„ฃ๊ณ  ํ•œ ์ชฝ์€ ๋ฐ”๋กœ๋ฐ”๋กœ ๋น„๊ตํ•˜๋Š” ๋ถ€๋ถ„์ด๋ž‘ ์–ด์ฐจํ”ผ ์ •๋‹ต๋„ ์‹œ์ž‘์— ์ฃผ์–ด์ง„ ์ˆœ์„œ๋กœ ์ •๋ ฌ๋˜์–ด์žˆ์–ด์•ผํ•˜๋‹ˆ๊นŒ ํฌ์ง€์…˜ ๋ณ„๋กœ ์ •๋ฆฌํ•˜๋˜ ๊ฒฐ๊ตญ healths vector์—์„œ ๊ฐ’ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„๊นŒ์ง€ ๊ณ ๋ฏผ ๋งŽ์ดํ•œ ๊ฒƒ ๊ฐ™์€ ํ”์ ์ด ์žˆ๋„ค์š” ใ…Žใ…Ž

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌ ์ˆœ์„œ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ •๋ฆฌ๋˜๋Š”๊ฐ€ ์กฐ๊ธˆ ์–ด๋ ค์› ๋Š”๋ฐ pair<int, int> ๋ฅผ ์ด์šฉํ•ด์„œ ํฌ์ธํ„ฐ? ์ฒ˜๋Ÿผ ์ ‘๊ทผํ•ด์„œ ์›๋ณธ ์ˆœ์„œ๋Š” ๊ฑด๋“ค์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊นŒ์ง€ ์žฌ๋ฐŒ๋Š” ๋ฌธ์ œ์— ์žฌ๋ฐŒ๋Š” ์ฝ”๋“œ์˜€์Šต๋‹ˆ๋‹ค!

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.

์˜ค๋ฅธ์ชฝ์œผ๋กœ ๊ฐ€๋Š” ๋กœ๋ด‡๋“ค๋งŒ ์Šคํƒ์— ์ง‘์–ด๋„ฃ๊ณ  ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ ์™ผ์ชฝ์œผ๋กœ ๊ฐ€๋Š” ๋กœ๋ด‡์˜ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•ด ์ฃผ๋Š”๊ฒŒ ์ฒซ๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•๋ณด๋‹ค ํ™•์‹คํžˆ ํšจ์œจ์ ์œผ๋กœ ๋ณด์ด๋„ค์š”. ์ฝ”๋“œ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค!

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