난이도: Bronze 3
프로그래밍 언어: Kotlin
문제명: 최소, 최대
푼일자: 2021년 6월
주소: https://www.acmicpc.net/problem/10818
문제풀기
단순한 선형 시간 O(n) 문제이다.
제출하고, 생각나는 몇가지 방법이 있어 각각 백준에 제출을 해서 성능을 비교해 보았다.
MutableList<Int> 형에서 지원하는 .min() 과 .max() 를 사용해 보았다.
fun resolve1() = with(Scanner(System.`in`)) {
nextLine()
val r = nextLine().split(" ").map { it.toInt() }.toTypedArray().toList()
print("${r.minOrNull()} ${r.maxOrNull()}")
}
MutableList<Int> 에서 정렬을 한 뒤 맨 처음과 맨 마지막의 index 를 넣어 확인해 보았다.
fun resolve2() = with(Scanner(System.`in`)) {
nextLine()
val r = nextLine().split(" ").map { it.toInt() }.toTypedArray().toList().sorted()
print("${r[0]} ${r[r.size-1]}")
}
ArrayList<Int> 형에서 지원하는 .min() 과 .max() 를 사용해 보았다.
fun main() = with(Scanner(System.`in`)) {
nextLine()
val r = nextLine().split(" ").map { it.toInt() }
print("${r.minOrNull()} ${r.maxOrNull()}")
}
위 코드에서 .sorted() 를 이용하여 인덱스로 print를 하게 만들면... resolve2() 와 비슷한 성능을 낸다.
반응형