프로그래머스 - 영어 끝말잇기

well-life-gm·2021년 12월 19일
0

프로그래머스

목록 보기
90/125

프로그래머스 - 영어 끝말잇기

해쉬로 이미 쓰여진 단어들 체크만 해주면서 풀면 된다.

코드는 다음과 같다.

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

using namespace std;

vector<int> solution(int n, vector<string> words) {
    vector<int> answer;
    int last = 0;
    map<string, int> m;       
    m.insert( { words[0], 0 } );
    for(int i=1;i<words.size();i++) {
        if(words[i][0] != words[i-1][words[i-1].size()-1]) {
            last = i + 1;
            break;
        }
        if(m.find(words[i]) != m.end()) {
            last = i + 1;
            break;
        }
        m.insert( { words[i], 0 } );
    }
    int a1 = last % n;
    a1 = a1 == 0 && last != 0 ? n : a1;
    int a2 = (int)ceil((double)last / n);
    answer.push_back(a1);
    answer.push_back(a2);
    
    return answer;
}

결과

profile
내가 보려고 만든 블로그

0개의 댓글