문제
https://school.programmers.co.kr/learn/courses/30/lessons/12981
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
파이썬
- math 함수 사용하여 올림
import math
def solution(n, words):
arr = []
cnt = 0
for i in range(0, len(words)):
if words[i] in arr:
return (math.ceil(cnt % n) +1, cnt // n + 1)
elif (i > 0 and i < len(words)) and words[i-1][-1] != words[i][0]:
return (math.ceil(cnt % n) +1, cnt // n + 1)
else:
cnt += 1
arr.append(words[i])
return (0, 0)
자바스크립트
function solution(n, words) {
let answer = [0, 0];
const setWords = new Set(words)
for (let i = 0; i < words.length - 1; i++) {
if (i === 0) {
// 첫 단어는 틀릴 수 없으므로 지나감
setWords.delete(words[i])
} else if (setWords.has(words[i]) && words[i - 1].slice(-1) === words[i][0]) {
// 단어가 중복체크 & 끝말잇기가 정상이면 지나감
setWords.delete(words[i])
} else {
return answer = [i % n + 1, parseInt(i / n) + 1]
}
}
// words.length - 1 까지만 확인 했으므로 마지막 단어도 확인
if (!setWords.has(words[words.length - 1])) {
return answer = [(words.length - 1) % n + 1, parseInt(words.length / n)]
}
return answer;
}
'PS (Problem Solving) > Programmers' 카테고리의 다른 글
[프로그래머스] H-index (0) | 2022.11.03 |
---|---|
[프로그래머스] N개의 최소공배수 (0) | 2022.11.01 |
[프로그래머스] 최소직사각형 (0) | 2022.10.27 |
[프로그래머스] 짝지어 제거하기 - 파이썬, 자바스크립트 (0) | 2022.10.26 |
[프로그래머스] 다음 큰 숫자 (0) | 2022.10.25 |