문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
알고리즘
- 반복문을 통해 크레인의 위치를 이동한다.
- 반복문을 통해 크레인으로 뽑을 수 있는 인형을 확인 후 뽑는다.
- 뽑은 인형의 개수가 2개 이상이라면 맨 위에 2개의 인형을 비교한다.
- 비교 후 같다면 pop() 한 후 인형의 개수만큼 카운트한다.
코드
import java.util.*
class Solution {
fun solution(board: Array<IntArray>, moves: IntArray): Int {
var answer = 0
val pickUp = Stack<Int>()
for (move in moves){
for (i in board.indices){
if (board[i][move-1] != 0){
if (pickUp.isNotEmpty() && pickUp.peek() == board[i][move-1]){
pickUp.pop()
answer += 2
}else{
pickUp.push(board[i][move-1])
}
board[i][move-1] = 0
break
}
}
}
return answer
}
}
github
GitHub - junjange/KotlinAlgorithm: 내가 푼 코딩 테스트 문제와 해결법(Kotlin)
내가 푼 코딩 테스트 문제와 해결법(Kotlin). Contribute to junjange/KotlinAlgorithm development by creating an account on GitHub.
github.com
'CodingTest > Programers' 카테고리의 다른 글
[programers] 프로그래머스(파이썬) : 성격 유형 검사하기 (1) | 2022.09.04 |
---|---|
[programers] 프로그래머스(파이썬) : 가장 큰 수 (1) | 2022.08.31 |
[programers] 프로그래머스(파이썬) : 크레인 인형뽑기 게임 (0) | 2022.08.29 |
[programers] 프로그래머스(파이썬) : 가장 먼 노드 (0) | 2022.08.19 |
[programers] 프로그래머스(파이썬) : 기능개발 (0) | 2022.05.21 |