본문 바로가기

전체 글236

[Kotlin][Collection] sorted / sortedDescending 기본 정렬 순서에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public fun > Iterable.sorted(): List { if (this is Collection) { if (size >() as Array).apply { sort() }.asList() } return toMutableList().apply { sort() }}val list = listOf(4, 3, 2, 1)print(list.sorted()) // [1, 2, 3, 4] 자연 정렬 순서에 따라 내림차순으로 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 .. 2024. 8. 23.
[Kotlin][Collection] reversed 요소가 역순으로 포함된 목록을 반환합니다.public fun Iterable.reversed(): List { if (this is Collection && size  reversed - Kotlin Programming Language kotlinlang.org 2024. 8. 23.
[Algorithm][Kotlin] 정렬 알고리즘 (선택, 삽입, 버블, 퀵, 병합) 정렬 (sorting)어떤 데이터들이 주어졌을 때 이를 정해진 순서대로 나열하는 것val dataList = (0..99).shuffled().take(10).toIntArray()println("Before sorting: ${dataList.joinToString(", ")}") 선택 정렬 (Selection Sort)1. 주어진 리스트 중에 최소값을 찾는다.2. 해당 최소값을 데이터 맨 앞에 위치한 값과 교체한다.3. 맨 처음 위치를 뺀 나머지 데이터를 같은 방법으로 교체한다.시간 복잡도average, worst : O(N²) (구현이 간단하지만 비효율적)공간 복잡도제자리(In-place) 알고리즘 (기존 배열 외에 추가적인 메모리를 거의 사용하지 않는다)안전성불안전정렬 (정렬을 수행할 때 동일한 .. 2024. 8. 22.
[Kotlin][Collection] unzip 목록 쌍을 반환합니다. 여기서 첫 번째 목록은 이 컬렉션의 각 쌍의 첫 번째 값에서 작성되고, 두 번째 목록은 이 컬렉션의 각 쌍의 두 번째 값에서 작성됩니다.public fun Iterable>.unzip(): Pair, List> { val expectedSize = collectionSizeOrDefault(10) val listT = ArrayList(expectedSize) val listR = ArrayList(expectedSize) for (pair in this) { listT.add(pair.first) listR.add(pair.second) } return listT to listR}val list = listOf(1 to .. 2024. 8. 22.
[Kotlin][Collection] zip 이 배열과 동일한 인덱스를 가진 다른 배열의 요소로 구성된 쌍 목록을 반환합니다. 반환된 목록에는 가장 짧은 컬렉션의 길이가 있습니다.public inline fun Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List { val first = iterator() val second = other.iterator() val list = ArrayList(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10))) while (first.hasNext() && second.hasNext()) { list.add(transform(first.n.. 2024. 8. 22.
[Kotlin][Collection] minus 주어진 요소가 처음으로 나타나는 경우 없이 원본 컬렉션의 모든 요소를 ​​포함하는 목록을 반환합니다.public operator fun Iterable.minus(elements: Iterable): List { val other = elements.convertToListIfNotCollection() if (other.isEmpty()) return this.toList() return this.filterNot { it in other }}val string = "HelloWorld"val intArray = intArrayOf(0, 1, 2, 3)print(string.indices - intArray.toSet()) // [4, 5, 6, 7, 8, 9] minus.. 2024. 8. 22.
[LeetCode][Kotlin] 2073. Time Needed to Buy Tickets 2073. Time Needed to Buy TicketsThere are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line. You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i]. Each person takes exactly 1 second to buy a ticket. A person can only .. 2024. 8. 21.