(Swift) 백준 10818 최소, 최대

SteadySlower·2022년 5월 8일
0

Coding Test

목록 보기
16/298

[Swift] 10818 최소, 최대 - 백준 B3

시간 초과

let N = Int(readLine()!)!
let nums = readLine()!.components(separatedBy: " ").map { Int(String($0))! }

print(nums.min()!, nums.max()!)
  • 시간 초과가 난 이유: components를 사용했기 때문 → .split을 사용하자❗️

정답

let N = Int(readLine()!)!
let nums = readLine()!.split(separator: " ").map { Int(String($0))! }

print(nums.min()!, nums.max()!)
  • split은 Foundation을 불러올 필요가 없고 속도도 더 빠르다.
  • min()과 max()는 시간복잡도가 O(n)인데 sort하면 O(nlogn)이므로 굳이 sort할 필요가 없다.
profile
백과사전 보다 항해일지(혹은 표류일지)를 지향합니다.

0개의 댓글