๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

IN DEPTH CAKE/Supercoder

[c++/LeetCode-Hash Table] 692. Top K Frequent Words

 

 

๋‚œ์ด๋„Medium

ํ‚ค์›Œ๋“œ: Hash Table

 

๐ŸŽฒ ๋ฌธ์ œ

 

๋ฌธ์ž์—ด ๋ฐฐ์—ด words์™€ ์ •์ˆ˜ k๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, k๊ฐœ์˜ ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋นˆ๋„์ˆ˜๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ ๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ๋นˆ๋„๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์—ด์€ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

 

 

๐ŸŽฒ ์˜ˆ์‹œ

Example 1:

Input: words = ["i","love","leetcode","i","love","coding"], k = 2
Output: ["i","love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.

 

Example 2:

Input: words = ["the","day","is","sunny","the","the","the","sunny","is","is"], k = 4
Output: ["the","is","sunny","day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words, with the number of occurrence being 4, 3, 2 and 1 respectively.

 

 

๐ŸŽฒ ๋ฌธ์ œ ํ’€์ด

hash๋ฅผ ์‚ฌ์šฉํ•ด์„œ count๋ฅผ ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋‹จ์ˆœํ•œ ๋ฌธ์ œ์ด๋‹ค.

hash๋ฅผ ์‚ฌ์šฉํ•œ count -> count ๊ฐ’ ๊ธฐ์ค€์œผ๋กœ sort -> ๊ทธ ์ค‘ top-k๊ฐœ๋งŒ ์ถœ๋ ฅ ํ•˜๋ฉด ๋๋‚˜๋Š” ๋ฌธ์ œ์ด๋‹ค. (์‰ฝ์ฃ ?)

 

๋ฌธ์ œ ํ’€์ด ํ•  ๋•Œ, ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์จ๋ณด์ž ์‹ถ์–ด์„œ (python์œผ๋กœ ์ฝ”๋”ฉ๋ฌธ์ œ ํ’€์–ด๋ณด๋˜๊ฑฐ ์š”์ฆ˜ cpp๋กœ ์˜ฎ๊ฒจ๊ฐ€๋Š” ์ค‘์ด๋ผ STL ์ตํž ๊ฒธ) ๊ตณ์ด ์•ˆ์จ๋„ ๋˜๋Š” heap์„ ์จ๋ดค๋‹ค. ์ด ์ฝ”๋“œ์—์„œ heap (priority_queue)๋Š” ์•ˆ์จ๋„ ๋˜๊ณ  sorting ํ•˜๋Š” ๊ฒŒ ์˜คํžˆ๋ ค ๋” ๋งž๋Š” ์ฝ”๋“œ์ด๋‹ค. heap์€ ๊ณ„์†ํ•ด์„œ sorting์ด ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์€๋ฐ,  ์ด ์ฝ”๋“œ์—์„œ๋Š” ์‚ฌ์‹ค์ƒ ํ•œ๋ฒˆ์— sortingํ•˜๋Š” ๊ฑฐ๋ผ... ๊ทธ๋‹ค์ง€ ํšจ์šฉ์„ฑ์€ ์—†๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

compare ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ค„ ๋•Œ ์กฐ์‹ฌํ•  ์ ์€, sort์— ๋„ฃ์–ด์ฃผ๋Š” compare๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ, heap ์— ๋“ค์–ด๊ฐ€๋Š” compare ์šฉ ํ•จ์ˆ˜๋Š” compare class ๋‚ด์— operator() ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•ด์ฃผ๋Š” ํ˜•ํƒœ๋กœ ์ •์˜ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

struct cmp{
    bool operator()(pair<int, string> a, pair<int, string> b) const{
        if(a.first > b.first)
            return !(a.first > b.first);
        if(a.first == b.first)
            return !(a.second < b.second);
        else
            return !(false);
        
    }
};

class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {

        unordered_map<string, int> hash;
        vector<string> ans;

        // count
        for(const auto word : words){
            const auto ptr = hash.find(word);
            if(ptr == hash.end())
                hash[word] = 1;
            else
                hash[word] += 1;
        }

        // build heap
        priority_queue<pair<int, string>, vector<pair<int, string>>, cmp> heap;
        for(auto const& it: hash){
            cout << it.first << " " << it.second << endl;
            heap.push({it.second, it.first});
        }

        // find top k elements
        for(auto _ = 0; _ < k; ++_){
            ans.emplace_back(heap.top().second);
            heap.pop();
        }

        return ans;
    }
};

 

 

๋ฐ˜์‘ํ˜•

'IN DEPTH CAKE > Supercoder' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[c++/LeetCode-DP] 70. Climbing Stairs  (1) 2024.05.06
[c++/LeetCode-Hash Table] 939. Minimum Area Rectangle  (1) 2024.05.05
[c++/LeetCode-Hash Table] 387. First Unique Character in a String  (1) 2024.05.05
[c++/LeetCode-Hash Table] 1. Two Sum  (1) 2024.05.05
[c++/LeetCode-Stack] 394. Decode String  (1) 2024.05.05