-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
There was a problem hiding this 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;
}
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hash ์ฒ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ํ๋๊ฐ ์ถ์๋๋ฐ map๋ฅผ ์ฐ๋ฉด ์์ฐ์ค๋ฝ๊ฒ ๋๋๊ตฐ์! ์ด๋ ๊ฒ ํ๋ ๋ ๋ฐฐ์๊ฐ๋๋ค. ๋ฐฑ์ค ํฐ์ด ์์คํ ์ด ์ต์ํ๋ค๋ณด๋ ๋ค๋ฅธ ์ฌ์ดํธ๋ ๋ฌธ์ ๋์ด๋ ํ์ ์ด ์ด๋ ต๋ค์..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ด์ฌ์ ์ฐ๋ค๋ณด๋ c++์ hash๊ฐ ์๋์ง ๋ชฐ๋๋๋ฐ ๋์ ๋๋ฆฌ๋ผ๊ณ ์๊ฐํ๋ฉด ๋ ๊น์? ์์ ๊ฐ๋จํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ชฐ๋ผ์ ์ผ์ ์์ค ์ด์์ ๋ฌธ์ ๋ฅผ ํธ๋๋ฐ ํ๊ณ๊ฐ ์์์ ๋๋ผ๋๋ฐ ์ข์ ์ฌ์ดํธ ์ถ์ฒ ๋ฐ๊ณ ๊ฐ๋๋ค ใ ใ
There was a problem hiding this 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์ ํ์ฉ๋ฒ์๋ํด ์๋ฐฐ์๊ฐ๋๋ค
์์ hash๋ 1:1๋ก ์ด์ฐ๋ณด๋ฉด key : value ๋์
๋๋ฆฌ์ ์ ์ฌํ๋ค๊ณ ๋ณด์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค!! |
๐ ๋ฌธ์ ๋งํฌ
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
์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ์ ํด์ฃผ๋ฉด ๋๋ค!์ฐ์ ์์ ํ์์ ์ฐ๋ฆฌ๋ ๊ฐ์ ํฌ๊ธฐ๋ฅผ ์ง๋
value
๊ฐ์ ๋ํด์ ์์ธ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๊ธฐ ์ํด ์์ compare ์กฐ๊ฑด์ ์ถ๊ฐํด์ฃผ์๋ค.value
์ ๊ฐ์ด ๊ฐ์ผ๋ฉด ๋ฌธ์์ด์ ์ฌ์ ์์ผ๋ก ์ ๋ ฌํด์ค๋ค!ํ๊ธฐ
ํด๋น ๋ฌธ์ ๋ leetcode๋ผ๋ ์ฌ์ดํธ๋ก ์ฐ๋ฆฌ๊ฐ ๋ฐฑ์ค ๋ฐ ํ๋ก๊ทธ๋๋จธ์ค๋ฅผ ์ฌ์ฉํ๋ฏ์ด ์ธ๊ตญ์์๋ ํด๋น ์ฌ์ดํธ๋ฅผ ์ฃผ๋ก ์ฆ๊ฒจํผ๋ค.
๋์ด๋๊ฐ
middle
์์๋ ์ฌ์๋ณด์ด์ง๋ง ๊ทธ๋งํผ ์ผ๋ง๋ ์๋ฃ๊ตฌ์กฐ์ ๋ํด์ ์ดํด๋ฅผ ํ๊ณ ํ์ฉํ ์ ์๋์ง๋ฅผ ์๊ตฌํ๋ ์ ๋๋ก ์๋ฃ๊ตฌ์กฐ์ ๋ํด์ ์ค๋ ฅ์ ํค์ฐ๊ณ ์ถ๋ค๋ฉด ํด๋น ์ฌ์ดํธ๋ฅผ ์ถ์ฒํ๋ค.๋ ๋ฒ์งธ ํ์ด๋ก ํธ๋ผ์ด๋ ์๊ฐ์ ํด๋ดค๋ค.
ํธ๋ผ์ด๋ string ๋ฌธ์์ด์ ๋ ธ๋ ํธ๋ฆฌํ์์ผ๋ก ํผ์ ธ๋๊ฐ๋ ๋ฐฉ์์ด๋ค.
๊ฐ ๋ ธ๋๋ charํ alphabet[26] ๋ฐฐ์ด์ ๋ ์ผ๋ก์จ a~z๊น์ง์ ์ํ๋ฒณ์ ๋ฐฉ๋ฌธํ๋ฉฐ ์ด์ด ๋๊ฐ ์ ์๋ค.
like
๋จ์ด๋ฅผ ์์๋ก ๋ค๋ฉดl
์ด ๋ค์ด๊ฐ๋index
๋ฒํธ์ alphabet ๋ฐฐ์ด์ ์กด์ฌํ๋ค๊ณtrue
๋ก ๋ฐ๊พธ๊ณ ๊ทธ ๋ค์i
๋จ์ด๋ฅผ ๋๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฐ๊ฒฐ์์ผ ์ค๋ค.๊ฐ ๋ ธ๋์๋ finish๋ผ๋ boolํ ์กฐ๊ฑด์ด ์ถ๊ฐ๋์ด ์๋๋ฐ ํด๋น ์๋ฃํ์ ๋ง์ง๋ง ๋ ธ๋๋ผ๋ ๋ป, ์ฆ ํด๋น ๋จ์ด๊ฐ ์กด์ฌํ๋ค๋ ๋ป์ด๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ