๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][ํ•ด์‹œ] ํฌ์ผ“๋ชฌ

ibelieveinme 2023. 5. 15. 22:34
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/1845

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

ํ•ด๊ฒฐ๋ฒ•

ํฌ์ผ“๋ชฌ ๋ฒˆํ˜ธ์™€ ๊ฐœ์ˆ˜๋ฅผ ์ค‘๋ณต์—†์ด ์ €์žฅํ•ด์„œ ๋ฝ‘๋Š” ๊ฐœ์ˆ˜๋Œ€๋น„ ์ตœ๋Œ€ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

์ค‘๋ณต์—†์ด ์ €์žฅํ•ด์„œ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š”๊ฑฐ๋ผ Hash์˜ ์ „ํ˜•์ ์ธ ๋ฌธ์ œ๋‹ค.

Hash Map์œผ๋กœ ๊ฐ’์„ ์ค‘๋ณต์—†์ด ์ €์žฅํ•˜๊ณ  ํ•ด์‰ฌ๊ธธ์ด์™€ ๋ฝ‘์„ ๊ฐœ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ด์„œ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์•Œ์•„๋‘˜ ๊ฒƒ

1. C++์˜ foreach๋ฌธ

for (auto i : ๋ฐฐ์—ด/๋ฒกํ„ฐ์ด๋ฆ„)

C++11๋ถ€ํ„ฐ ์ง€์›๊ฐ€๋Šฅ !

 

2. unordered_map

- key - value ์กฐํ•ฉ์œผ๋กœ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ.

- ํ•ด์‹œ ํ…Œ์ด๋ธ” ํ˜•ํƒœ์ด๋ฏ€๋กœ ์ž๋™ ์ •๋ ฌ๋˜์ง€ ์•Š์•„์„œ ์ •๋ ฌ์ด ํ•„์š”์—†์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

- O(1)์˜ ๊ฒ€์ƒ‰, ์‚ญ์ œ, ์ˆ˜์ •, ์‚ฝ์ž…์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

#include <iostream>
#include <unordered_map>//include
using namespace std;

int main(){
    unordered_map<string, int> map; //์„ ์–ธ

    //๋ฐ์ดํ„ฐ ์‚ฝ์ž…
    map.insert(make_pair("a", 1));
    map.insert(make_pair("b", 2));
    map.insert(make_pair("c", 3));

    //๋ฐ์ดํ„ฐ ํƒ์ƒ‰
    map.find("a");
    cout << "map["a"].first: " << map["a"].first; // a
    cout << "map["a"].second: " << map["a"].second; // 1
    map.count("a");//key๊ฐ€ "a"์ธ ์›์†Œ์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
    
    //๋ฐ์ดํ„ฐ ์ˆœํšŒ
    for(auto& i: map){
    	if(i.first == "b") i.second ++; // map["b"].second = 2;
    }
    
    //๋ฐ์ดํ„ฐ ์ˆ˜์ •
    map["a"] = 2;
    cout << "map["a"].second: " << map["a"].second; // 2

    //๋ฐ์ดํ„ฐ ์‚ญ์ œ
    map.erase("a");
    
    //๊ธฐํƒ€
    map.empty(); // ๋น„์–ด์žˆ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
    map.size(); // map์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜

    return 0;
}

 

3. unordered_set

- Key ๋ฐ์ดํ„ฐ๊ฐ€ Set์— ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ. ์ง‘ํ•ฉ๊ฐ™์€ ๋Š๋‚Œ !

- Map์€ Key๊ฐ€ ํŠน์ • Value์— ๋Œ€์‘๋˜์ง€๋งŒ, Set์€ Key๋งŒ ๊ฐ€์ง€๋ฏ€๋กœ ์ค‘๋ณต์ €์žฅ์ด ํ•„์š”์—†๋Š” Key๋ฅผ ์ €์žฅํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

- ๊ฐ™์€ ์›์†Œ๊ฐ€ ์ด๋ฏธ ๋“ค์–ด์žˆ๋‹ค๋ฉด ๋‚˜์ค‘์— ๋“ค์–ด์˜ค๋Š” insert๋Š” ๋ฌด์‹œํ•œ๋‹ค.

#include <bits/stdc++.h>
using namespace std;

int solution(vector<int> nums) {
    unordered_set<int> s(nums.begin(), nums.end());

    return min(nums.size() / 2, s.size());
}

- ๋‹ค๋ฅธ๋ถ„์ด ์œ„์ฒ˜๋Ÿผ unordered_set์œผ๋กœ 2์ค„๋งŒ์— ๊ตฌํ˜„ํ–ˆ๊ธธ๋ž˜ unordered_set๋„ ์•Œ์•„๋‘์ž.

 

์ฝ”๋“œ

#include <vector>
#include <unordered_map>
using namespace std;

int solution(vector<int> nums){
    int answer = 0;
    unordered_map<int, int> pocketmons;
    
    //ํฌ์ผ“๋ชฌ ์ข…๋ฅ˜ ์ถ”๊ฐ€
    for (auto num : nums){
        pocketmons.insert(make_pair(num, 0));
    }
    
    //ํฌ์ผ“๋ชฌ ๊ฐœ์ˆ˜ ์ถ”๊ฐ€
    for (auto pocketmon : pocketmons){
        pocketmon.second++;
    }
    
    //์ตœ๋Œ€ ํฌ์ผ“๋ชฌ ์„ ํƒ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ
    if(pocketmons.size() > nums.size()/2){
        answer = nums.size()/2;
    }
    else if(pocketmons.size() < nums.size()/2){
        answer = pocketmons.size();
    }
    else answer = nums.size()/2;
    
    return answer;
}

์ง๊ด€์ ์œผ๋กœ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ์งœ๊ณ  ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ๋ณด๊ณ  ์ตœ์ ํ™” ํ–ˆ๋‹ค.

#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

int solution(vector<int> nums){
    int answer = 0;
    unordered_map<int, int> pocketmons;
    
    //ํฌ์ผ“๋ชฌ ์ข…๋ฅ˜&๊ฐœ์ˆ˜ ์ถ”๊ฐ€
    for (auto num : nums){
        pocketmons[num]++;
    }
    
    //์ตœ๋Œ€ ํฌ์ผ“๋ชฌ ์„ ํƒ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ
    answer = min(pocketmons.size(), nums.size()/2);
    
    return answer;
}

์ด๊ฑด ์ตœ์ ํ™” ์ฝ”๋“œ. unordered_map์„ ๊ตฌ์ง€ insert ๋ฌธ์„ ์•ˆ์“ฐ๊ณ ๋„ ๊ฐ’ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š”๊ฑธ ๊ธฐ์–ตํ•ด๋‘์ž.

728x90