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

9-InSange #34

Merged
merged 4 commits into from
May 9, 2024
Merged

9-InSange #34

merged 4 commits into from
May 9, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Apr 12, 2024

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

Top K Frequent Words

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

30๋ถ„

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

์ ‘๊ทผ ๋ฐฉ์‹

hash์ธ map์„ ์‚ฌ์šฉํ•ด์„œ words์—์„œ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ intํ˜•์œผ๋กœ ์ €์žฅํ•ด์ค€๋‹ค.
์ž„์˜์˜ i์— ๋Œ€ํ•ด์„œ map์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” words[i]์ผ ๊ฒฝ์šฐ ์ฆ๊ฐ€์‹œ์ผœ์ฃผ๋Š” ํ˜•์‹์ด๋‹ค.
๋”ฐ๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ์•ˆ์‹œ์ผœ์ค˜๋„ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋กœ ์ธํ•ด map์˜ key๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ value๊ฐ’์ด 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ ์ƒํƒœ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. ์ฆ‰ {key, value} ์— ๋Œ€ํ•ด์„œ {words[i], 0}์œผ๋กœ ๋””ํดํŠธ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” words[i]์— ๋Œ€ํ•ด์„œ ์ฆ๊ฐ€ ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•ด์ค„ ๊ฒฝ์šฐ 0์—์„œ ์ฆ๊ฐ€๊ฐ€ ๋˜์–ด 1๋กœ ์ €์žฅ์ด ๋œ๋‹ค.
if words[i] not in map : {words[i], 1}

๋ฌธ์ œ์˜ ๋ชฉํ‘œ๋Š” K๊ฐœ์ˆ˜ ๋งŒํผ ๋งŽ์ด ์‚ฌ์šฉ๋œ ๋‹จ์–ด๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
์ด ๋ถ€๋ถ„์€ ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•˜์—ฌ {key:value}์—์„œ value์˜ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค!

struct compare{
        bool operator()(pair<int, string>& a, pair<int, string>& b)
        {
            if(a.first == b.first) return a.second > b.second;
            return a.first < b.first;
        }
    };

์šฐ์„  ์ˆœ์œ„ ํ์—์„œ ์šฐ๋ฆฌ๋Š” ๊ฐ™์€ ํฌ๊ธฐ๋ฅผ ์ง€๋‹Œ value๊ฐ’์— ๋Œ€ํ•ด์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์œ„์˜ compare ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.
value์˜ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๋ฌธ์ž์—ด์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค!

ํ›„๊ธฐ

ํ•ด๋‹น ๋ฌธ์ œ๋Š” leetcode๋ผ๋Š” ์‚ฌ์ดํŠธ๋กœ ์šฐ๋ฆฌ๊ฐ€ ๋ฐฑ์ค€ ๋ฐ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋“ฏ์ด ์™ธ๊ตญ์—์„œ๋Š” ํ•ด๋‹น ์‚ฌ์ดํŠธ๋ฅผ ์ฃผ๋กœ ์ฆ๊ฒจํ‘ผ๋‹ค.
๋‚œ์ด๋„๊ฐ€ middle ์ž„์—๋„ ์‰ฌ์›Œ๋ณด์ด์ง€๋งŒ ๊ทธ๋งŒํผ ์–ผ๋งˆ๋‚˜ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ์ดํ•ด๋ฅผ ํ•˜๊ณ  ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์š”๊ตฌํ•˜๋Š” ์ •๋„๋กœ ์ž๋ฃŒ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ์‹ค๋ ฅ์„ ํ‚ค์šฐ๊ณ  ์‹ถ๋‹ค๋ฉด ํ•ด๋‹น ์‚ฌ์ดํŠธ๋ฅผ ์ถ”์ฒœํ•œ๋‹ค.

๋‘ ๋ฒˆ์งธ ํ’€์ด๋กœ ํŠธ๋ผ์ด๋„ ์ƒ๊ฐ์„ ํ•ด๋ดค๋‹ค.
ํŠธ๋ผ์ด๋Š” string ๋ฌธ์ž์—ด์„ ๋…ธ๋“œ ํŠธ๋ฆฌํ˜•์‹์œผ๋กœ ํผ์ ธ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์ด๋‹ค.
๊ฐ ๋…ธ๋“œ๋Š” charํ˜• alphabet[26] ๋ฐฐ์—ด์„ ๋‘ ์œผ๋กœ์จ a~z๊นŒ์ง€์˜ ์•ŒํŒŒ๋ฒณ์„ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ์ด์–ด ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.
like๋‹จ์–ด๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค๋ฉด l์ด ๋“ค์–ด๊ฐ€๋Š” index๋ฒˆํ˜ธ์˜ alphabet ๋ฐฐ์—ด์— ์กด์žฌํ•œ๋‹ค๊ณ  true๋กœ ๋ฐ”๊พธ๊ณ  ๊ทธ ๋‹ค์Œ i ๋‹จ์–ด๋ฅผ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์—ฐ๊ฒฐ์‹œ์ผœ ์ค€๋‹ค.

๊ฐ ๋…ธ๋“œ์—๋Š” finish๋ผ๋Š” boolํ˜• ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ๋Š”๋ฐ ํ•ด๋‹น ์ž๋ฃŒํ˜•์€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ผ๋Š” ๋œป, ์ฆ‰ ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

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

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์จ๋„ ์ข‹์ง€๋งŒ, vector๋กœ ์˜ฎ๊ธด ๋’ค ์ •๋ ฌํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ธ ๊ฒƒ ๊ฐ™๋„ค์š” :)

#include <unordered_map>

class Solution {
public:
    vector<string> topKFrequent(vector<string> words, int k) {
        unordered_map<string, int> map;
        for(const string& word : words) {
            map[word] += 1;
        }

        vector<pair<string, int>> v(map.begin(), map.end());
        sort(v.begin(), v.end(), [](const pair<string, int>& lhs, const pair<string, int>& rhs) {
            if(lhs.second == rhs.second) {
                return lhs.first < rhs.first;
            }
            return lhs.second > rhs.second;
        });
        
        vector<string> ans;
        for(auto& [word, cnt] : v) {
            if(k == 0) {
                break;
            }

            ans.emplace_back(word);
            k--;
        }
        return ans;
    }
};

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.

hash ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”๊ฐ€ ์‹ถ์—ˆ๋Š”๋ฐ map๋ฅผ ์“ฐ๋ฉด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋˜๋Š”๊ตฐ์š”! ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜ ๋˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค. ๋ฐฑ์ค€ ํ‹ฐ์–ด ์‹œ์Šคํ…œ์ด ์ต์ˆ™ํ•˜๋‹ค๋ณด๋‹ˆ ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ๋Š” ๋ฌธ์ œ ๋‚œ์ด๋„ ํŒŒ์•…์ด ์–ด๋ ต๋„ค์š”..

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.

ํŒŒ์ด์ฌ์„ ์“ฐ๋‹ค๋ณด๋‹ˆ c++์— hash๊ฐ€ ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋Š”๋ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ ๊นŒ์š”? ์š”์ƒˆ ๊ฐ„๋‹จํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ชฐ๋ผ์„œ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ์˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ํ•œ๊ณ„๊ฐ€ ์žˆ์Œ์„ ๋Š๋ผ๋Š”๋ฐ ์ข‹์€ ์‚ฌ์ดํŠธ ์ถ”์ฒœ ๋ฐ›๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

map์˜ key์™€ value๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹จ์–ด์˜ ํŠน์ •์•ŒํŒŒ๋ฒณ์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๊ธฐ๋กํ•ด์ค„์ˆ˜์žˆ๊ตฐ์š”.. map์˜ ํ™œ์šฉ๋ฒ•์—๋Œ€ํ•ด ์ž˜๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค

@InSange
Copy link
Collaborator Author

InSange commented May 9, 2024

ํŒŒ์ด์ฌ์„ ์“ฐ๋‹ค๋ณด๋‹ˆ c++์— hash๊ฐ€ ์žˆ๋Š”์ง€ ๋ชฐ๋ž๋Š”๋ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ ๊นŒ์š”? ์š”์ƒˆ ๊ฐ„๋‹จํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋ชฐ๋ผ์„œ ์ผ์ • ์ˆ˜์ค€ ์ด์ƒ์˜ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๋ฐ ํ•œ๊ณ„๊ฐ€ ์žˆ์Œ์„ ๋Š๋ผ๋Š”๋ฐ ์ข‹์€ ์‚ฌ์ดํŠธ ์ถ”์ฒœ ๋ฐ›๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ใ…Žใ…Ž

์˜ˆ์•„ hash๋Š” 1:1๋กœ ์–ด์ฐŒ๋ณด๋ฉด key : value ๋”•์…”๋„ˆ๋ฆฌ์™€ ์œ ์‚ฌํ•˜๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!
hash๋Š” ๋ฆฌ์ŠคํŠธ์™€ ๋‹ฌ๋ฆฌ ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์„ ๋•Œ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(1)์ด๊ธฐ์— ๋น ๋ฅธ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!
ํ•œ ๋ฒˆ ์•Œ์•„๋‘๋ฉด ์ข‹์€ ์ž๋ฃŒ๊ตฌ์กฐ ์ผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

@InSange InSange merged commit bf32bd4 into main May 9, 2024
@InSange InSange deleted the 9-InSange branch May 9, 2024 04:35
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.

5 participants