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

16-InSange #63

Merged
merged 4 commits into from
Jul 10, 2024
Merged

16-InSange #63

merged 4 commits into from
Jul 10, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Jun 28, 2024

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

Miaximum Total Importance of Roads

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

1์‹œ๊ฐ„

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

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

๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ์ง€์  roads๋ฅผ ํ†ตํ•ด์„œ ๊ฐ ๋…ธ๋“œ n๊ฐœ์— ํ•ด๋‹นํ•˜๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์œผ๋ฉด ๋†’์€ ์ ์ˆ˜๋ฅผ ์ฐจ๋ก€๋กœ ๋ถ€์—ฌํ•œ๋‹ค. n, n-1, n-2 ... 0 ์ด๋Ÿฐ์‹์œผ๋กœ..
image
๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ฐ„์„ ์˜ ํ•ฉ์„ answer์— ์ฐจ๊ทผ์ฐจ๊ทผ ์ €์žฅํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ

๊ณ„์‚ฐํ•œ ๋…ธ๋“œ๋ฅผ unordered_map์—๋‹ค๊ฐ€ ๋„ฃ์–ด์„œ ๋‘ ๋…ธ๋“œ๊ฐ€ ์ด๋ฏธ ๊ณ„์‚ฐ๋œ ๊ฐ’์ด๋ผ๋ฉด ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ•˜๋ ค๋‹ค๋ณด๋‹ˆ ํ•„์š” ๋ฉ”๋ชจ๋ฆฌ์™€ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ์ง€์ €๋ถ„ํ•ด์กŒ๋‹ค.

์‚ฌ์‹ค ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ๊ฐ๊ฐ ๋”ฐ๋กœ ๊ณ„์‚ฐํ•  ํ•„์š” ์—†์ด ์—ฐ๊ฒฐ๋œ degree๋งŒํผ ๋ถ€์—ฌ๋œ ์ ์ˆ˜๋ฅผ ์—ฐ์‚ฐํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

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

์œ„์—์„œ ๋งํ•œ๋Œ€๋กœ ์ฐจ์ˆ˜์™€ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ†ตํ•ด ์ •๋ ฌ ์‹œ์ผœ์ฃผ๊ณ  ๋†’์€ ์ฐจ์ˆ˜(degree)๋ฅผ ์ง€๋‹Œ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด ์ ์ˆ˜๋ฅผ ๋งค๊ธฐ๊ณ  degree๋งŒํผ ๊ณฑํ•ด์„œ ๋ฐ”๋กœ ํ•ด๊ฒฐํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.

class Solution {
public:
    long long maximumImportance(int n, vector<vector<int>>& roads) {
        vector<vector<int>> conn;   // road
        priority_queue<pair<long long, int>> pq;  // connect degree first: degree, second: index
        long long answer = 0;
        
        conn.assign(n, vector<int>());
        
        for(auto road : roads)
        {
            int v1 = road[0];
            int v2 = road[1];
            
            conn[v1].push_back(v2);
            conn[v2].push_back(v1);
        }
        
        for(int i = 0; i < n; i++)
        {
            pq.push({conn[i].size(), i});
        }
        
        while(!pq.empty())
        {
            pair<long long, int> cur;
            cur = pq.top();
            pq.pop();

            answer += (n-- * cur.first);
        }
        
        return answer;
    }
};

๊ฒฐ๊ณผ

image

ํ›„๊ธฐ

class Solution {
public:
    long long maximumImportance(int n, vector<vector<int>>& roads) {
        vector<int> degree(n, 0); // Array to store the degree of each city
        
        // Calculate the degree of each city
        for (const auto& road : roads) {
            degree[road[0]]++;
            degree[road[1]]++;
        }
        
        // Create a list of cities and sort by degree
        vector<int> cities(n);
        for (int i = 0; i < n; i++) {
            cities[i] = i;
        }
        sort(cities.begin(), cities.end(), [&](int a, int b) {
            return degree[a] > degree[b];
        });
        
        // Assign values to cities starting from the highest degree
        long long totalImportance = 0;
        for (int i = 0; i < n; i++) {
            totalImportance += (long long)(n - i) * degree[cities[i]];
        }
        
        return totalImportance;
    }
};

์œ„์™€ ๊ฐ™์ด ๊ตณ์ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ์–ผ๋งˆ๋‚˜ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ง€ ์ฐจ์ˆ˜๋งŒ ์•Œ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ!

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

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.

์ฒซ ๋ฒˆ์งธ ํ’€์ด์˜ ๋ฐฉ๋ฒ•, ๋‘ ๋ฒˆ์งธ ํ’€์ด์˜ ์ฝ”๋“œ์™€ ํ›„๊ธฐ์˜ ์ฝ”๋“œ๊นŒ์ง€ ์ ์  ์ตœ์ ํ™” ๋˜์–ด๊ฐ€๋Š” ๋ถ€๋ถ„์„ ๋ณด๋ฉด ์—ญ์‹œ ๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด์„œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์ฐพ๊ธฐ ๋ณด๋‹ค ๋ฌธ์ œ๋ฅผ ์™„์ „ ํŒŒ์•…ํ•œ ๋’ค์— ์†”๋ฃจ์…˜์„ ์ฐพ์•„์•ผ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ ๊ฐ™์•„์š”!

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.

์ฐจ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฐ”๋กœ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•œ๊ฒŒ ์ข‹์•˜์–ด์š”. ๋ฐฑ์ค€์€ ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋ฆฌํŠธ์ฝ”๋“œ๋Š” ์ƒ๊ด€์—†๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค์Œ์— ํ•œ ๋ฒˆ ์จ ๋ด์•ผ ๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

@InSange InSange merged commit 75fb02d into main Jul 10, 2024
@InSange InSange deleted the 16-InSange branch July 10, 2024 07:34
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