Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- telegraf
- typescript
- InfluxDB
- 프로그래머스
- React
- Two Pointer
- Server monitoring
- 점화식
- configfile
- pnpm
- 스택/큐
- pymodbus
- Stack
- algorighm
- queue
- algorhtim
- 코딩테스트 연습
- Jest
- 파이썬
- Algorithm
- PCCP
- modbus
- Vite
- python
- javascript
- monorepo
- 완전탐색
- 알고리즘
- 슬라이딩 윈도우
- frontend
Archives
- Today
- Total
DM Log
[ Summer/Winter Coding(~2018)] 영어 끝말잇기 - Python / JavaScript 본문
알고리즘/프로그래머스
[ Summer/Winter Coding(~2018)] 영어 끝말잇기 - Python / JavaScript
Dev. Dong 2025. 3. 4. 15:51문제 링크 - https://school.programmers.co.kr/learn/courses/30/lessons/12981
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
[문제 간단 요약]
1. 영어 끝말잇기를 하는 중 탈락자의 번호와 차례를 구하는 문제
2. 끝말잇기 규칙은 기본 규칙을 따름
[문제 해결 방안]
1. Queue를 통해 순차적으로 앞에서 부터 확인하여 문제 해
[문제 해결 코드 - python]
from collections import deque
def solution(n, words):
pre_li = set()
q = deque(words)
pre_word = q.popleft()
pre_li.add(pre_word)
cnt=1
while q:
cnt+=1
word = q.popleft()
if (word[0] != pre_word[-1]) or (word in pre_li):
num = n if cnt % n == 0 else cnt % n
table = cnt // n if cnt % n == 0 else cnt // n + 1
return [num , table]
pre_word = word
pre_li.add(word)
return [0,0]
[문제 해결 코드 - javascript]
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.head = null;
this.tail = null;
this.size = 0;
}
enqueueAll(values) {
for (let value of values) {
this.enqueue(value);
}
}
enqueue(value) {
let newNode = new Node(value);
if (this.head === null) {
this.head = this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
this.size +=1;
}
dequeue() {
if (this.head === null) {
return null;
}
let nowNode = this.head;
this.head = this.head.next;
this.size -= 1;
return nowNode.value;
}
isEmpty() {
return this.size === 0;
}
}
function solution(n, words) {
let usedWords = new Set();
let queue = new Queue();
queue.enqueueAll(words);
let preWord = queue.dequeue()
usedWords.add(preWord);
let cnt = 1
while (!queue.isEmpty()) {
cnt += 1
let word = queue.dequeue()
if ((word[0] !== preWord[preWord.length - 1]) || usedWords.has(word)) {
let person = (cnt - 1) % n + 1;
let turn = Math.ceil(cnt / n);
return [person, turn]
}
preWord = word
usedWords.add(word)
}
return [0, 0];
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[연습문제] 연속 부분 수열 합의 개수 - Python / JavaScript (0) | 2025.03.05 |
---|---|
[2017 팁스타운] 예상 대진 - Python / JavaScript (0) | 2025.03.04 |
[연습문제] N개의 최소공배수 - Python / JavaScript (0) | 2025.02.26 |
[연습문제] 멀리 뛰기 - Python / JavaScript (1) | 2025.02.24 |
[Summer/Winter Coding(~2018)] 점프와 순간 이동 - Python / JavaScript (1) | 2025.02.23 |