Algorithm/프로그래머스 알고리즘
[프로그래머스] 영어끝말잇기
p8labs
2021. 10. 15. 16:08
728x90
반응형
문제
분석
2단계라 그렇게 어렵진 않았다.
입출력 예제를 보면 인덱스를 주어 쉽게 풀수 있다.
현재 몇회차 끝말잇기가 진행되었는지
탈락자는 몇번 인지.
구현
import java.util.*;
class Solution {
public int[] solution(int n, String[] words) {
int[] answer = new int[2];
ArrayList<String> checkWordList = new ArrayList<>();
int rotateCount = 1;
int wordCount = 1;
int currentCount = 1 ;
boolean isWrongWord = false;
for(int i = 0; i < words.length-1; i++) {
wordCount++;
currentCount++;
String currentWord = words[i];
String nextWord = words[i+1];
char curLastWord = currentWord.charAt(currentWord.length()-1);
char nextLastWord = nextWord.charAt(0);
checkWordList.add(currentWord);
if (curLastWord != nextLastWord || checkWordList.contains(nextWord) ) {
isWrongWord = true;
break;
}
if ((wordCount % n) == 0) {
currentCount = 0;
rotateCount ++;
}
}
if (isWrongWord) {
answer[0] = currentCount;
answer[1] = rotateCount;
} else {
answer[0] = 0;
answer[1] = 0;
}
return answer;
}
}
결과
문제:
https://programmers.co.kr/learn/courses/30/lessons/12981?language=java
코딩테스트 연습 - 영어 끝말잇기
3 ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"] [3,3] 5 ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"] [0,0]
programmers.co.kr
728x90
반응형