-
728x90
class Solution { fun solution(operations: Array<String>): IntArray { var answer = intArrayOf() var tempList = mutableListOf<Int>() operations.forEach{ if(it.split(" ")[0] == "I"){ tempList.add(it.split(" ")[1].toInt()) } else if(it.split(" ")[0] == "D" && it.split(" ")[1] == "1" && tempList.isNotEmpty()){ tempList.remove(tempList.maxOrNull()) } else if(it.split(" ")[0] == "D" && it.split(" ")[1] == "-1" && tempList.isNotEmpty()) { tempList.remove(tempList.minOrNull()) } } if(tempList.isEmpty()){ answer += 0 answer += 0 } else { answer += tempList.maxOrNull()!! answer += tempList.minOrNull()!! } return answer } }
상기 코드는 해당 문제가 원하는 풀이가 아님을 말씀드립니다.
큐를 이용하여 풀어야 하지만 현재 실력이 아직 못미쳐 일단 리스트를 이용하여 작성하였습니다.
추후 해당 문제는 큐를 작성하여 추가로 코드를 작성 할 수 있도록 하겠습니다.
'프로그래머스' 카테고리의 다른 글
[프로그래머스][Kotlin]가장 가까운 같은 글자 (0) 2022.12.12 문자열 나누기 (0) 2022.12.07 명예의 전당(1) (0) 2022.11.28 [프로그래머스][Kotlin]기사단원의 무기 (0) 2022.11.21 과일장수 (0) 2022.11.11