문제
풀이과정
원래는 vector만을 이용해서 풀었는데 map을 사용해서 key로 영단어들을 처리하는 게 더 효율적임을 깨닫고 한 번 코드를 엎었다.
풀이 코드
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<int> solution(int n, vector<string> words) {
map<string, int> word;
word[words[0]] = 1;
for (int i = 1; i < words.size(); i++) {
if (word[words[i]] || words[i - 1].back() != words[i].front())
return {(i % n) + 1, (i / n) + 1};
word[words[i]] = 1;
}
return {0, 0};
}
실행 결과
'coding test' 카테고리의 다른 글
[프로그래머스/level2] JadenCase 문자열 만들기 (0) | 2024.01.22 |
---|---|
[프로그래머스/level2] 최댓값과 최솟값 (0) | 2024.01.17 |
[programmers/level3] 야근 지수 (0) | 2024.01.12 |