λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Algorithm πŸ§‘πŸ»‍πŸ’»/ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€(Programmers)

[c++] ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ - 둜또의 졜고 μˆœμœ„μ™€ μ΅œμ € μˆœμœ„( Level 1)

by μ•ˆμ£Όν˜• 2021. 10. 20.

문제

 

μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ - 둜또의 졜고 μˆœμœ„μ™€ μ΅œμ € μˆœμœ„

둜또 6/45(μ΄ν•˜ '둜또'둜 ν‘œκΈ°)λŠ” 1λΆ€ν„° 45κΉŒμ§€μ˜ 숫자 쀑 6개λ₯Ό μ°μ–΄μ„œ λ§žνžˆλŠ” λŒ€ν‘œμ μΈ λ³΅κΆŒμž…λ‹ˆλ‹€. μ•„λž˜λŠ” 둜또의 μˆœμœ„λ₯Ό μ •ν•˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€. 1 μˆœμœ„ 당첨 λ‚΄μš© 1 6개 λ²ˆν˜Έκ°€ λͺ¨λ‘ 일치 2 5개 번호

programmers.co.kr

 

μ½”λ“œ

#include <string>
#include <vector>
#include <map>
#include <iostream>

using namespace std;

int check(int n){
    if(n<=1) return 6;
    return 7-n;
}
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
    vector<int> answer;
    map<int,int>m;
    int best = 0, worst = 0;
    
    for(int lotto: lottos) m[lotto]++;
    for(int win: win_nums){
        if(m.find(win)!=m.end()) m[win]--;
    }
    
    for(auto it=m.begin(); it!=m.end(); it++){
        if(it->first==0){
            best+=it->second;
            worst+=it->second;
        } 
        else if(it->first!=0){
            if(it->second==0) best++;
            else worst++;
        }
    }
    
    answer.push_back(check(best));
    answer.push_back(check(6-worst));
    return answer;
}

 

풀이

졜고둜 λ§žλŠ” κ°œμˆ˜μ™€ μ΅œμ €λ‘œ λ§žλŠ” 개수λ₯Ό κ΅¬ν•΄κ°€λŠ” μ‹μœΌλ‘œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€. 

  • 0일 λ•ŒλŠ” worst와 best μ „λΆ€ ++
  • 0이 μ•„λ‹λ•ŒλŠ” μˆ˜κ°€ 같을 λ•Œ best++, μ•„λ‹ˆλΌλ©΄ worst--
  • μ΅œμ’…μ μœΌλ‘œ worstλŠ” ν‹€λ¦° 개수이기 λ•Œλ¬Έμ— λ§žλŠ” 개수둜 λ°”κΏ”μ£ΌκΈ° μœ„ν•΄ 6-worstλ₯Ό 톡해 μˆœμœ„ 계산

λŒ“κΈ€