Queue
개념
문제 1. 기능 개발
설명
제약 조건
입출력의 예
progresses
speeds
return
코드
문제 2. 카드 뭉치
설명
제약 조건
입출력의 예
cards1
cards2
goal
result
코드
참고
Last updated

Last updated
fun solution(progresses: IntArray, speeds: IntArray): IntArray {
val result = mutableListOf<Int>()
// 1. progresses와 speeds를 각 인덱스의 값을 key와 value로 만든 객체를 Queue에 입력함
val queue: Queue<Pair<Int, Int>> = LinkedList()
for (i in progresses.indices) {
queue.offer(Pair(progresses[i], speeds[i]))
}
// 2. Queue가 비어질 때까지 무한 반복함
while (queue.isNotEmpty()) {
// 2.1. Queue에서 첫 번째 원소의 key가 100이 넘을 때까지 계속 순회함
while (queue.peek().first < 100) {
// 2.1.1. 각 원소의 key에 value를 더함
val size = queue.size
repeat(size) {
val (progress, speed) = queue.poll()
queue.offer(Pair(progress + speed, speed))
}
}
// 2.2. Queue에서 key가 100이 넘지 않은 원소까지 추출하면서 개수를 셈
var count = 0
while (queue.isNotEmpty() && queue.peek().first >= 100) {
queue.poll()
count++
}
// 2.3. 개수를 결과로 추가함
result.add(count)
}
return result.toIntArray()
}fun solution(
cards1: Array<String>,
cards2: Array<String>,
goal: Array<String>,
): String {
// 첫 번째 카드 뭉치의 문자열 위치를 저장할 card1Index 변수 선언 후 0 저장
var card1Index = 0
// 두 번째 카드 뭉치의 문자열 위치를 저장할 card2Index 변수 선언 후 0 저장
var card2Index = 0
// 일치 횟수를 저장할 변수 선언 후 0 저장
var count = 0
// goal 배열 순회
for (string in goal) {
// goal 문자열과 첫 번째 카드 뭉치의 card1Index 위치 문자열과 일치하는지 확인
// 일치할 경우
// card1Index 1 증가, count 1 증가
if (card1Index < cards1.size && string == cards1[card1Index]) {
card1Index++
count++
}
// goal 문자열과 두 번째 카드 뭉치의 card2Index 위치 문자열과 일치하는지 확인
// 일치할 경우
// card2Index 1 증가, count 1 증가
else if (card2Index < cards2.size && string == cards2[card2Index]) {
card2Index++
count++
}
}
// count와 goal 크기와 같을 경우 Yes 아닐 경우 No 반환
return if (goal.size == count) "Yes" else "No"
}