본문 바로가기

Kotlin148

[Algorithm] 동적 계획법 (Dynamic Programming, DP) 동적 계획법 (Dynamic Programming, DP)복잡한 문제를 간단한 여러 개의 문제로 나누어 푸는 방법한 가지 문제에 대해서, 단 한 번만 풀도록 만들어주는 알고리즘즉, 똑같은 연산을 반복하지 않도록 만들어준다.실행 시간을 줄이기 위해 많이 이용되는 수학적 접근 방식의 알고리즘주어진 문제를 풀기 위해서, 문제를 여러 개의 하위 문제로 나누어 푼 다음, 그것을 결합하여 최종적인 목적에 도달하는 것하나의 큰 문제를 여러 개의 작은 문제로 나누고, 각 문제의 결과를 저장하여 다시 큰 문제를 해결하는 데 사용한다.일반적인 재귀를 단순히 사용 시 동일한 작은 문제들이 여러 번 반복되어 비효율적인 계산될 수 있다.예를 들어 피보나치 수를 구하고 싶을 때 재귀로 함수를 구성하면 O(2ⁿ) 으로, 동적 계획.. 2024. 8. 20.
[LeetCode][Kotlin] 1758. Minimum Changes To Make Alternating Binary String 1758. Minimum Changes To Make Alternating Binary StringYou are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not. Return the minimum number of operations needed to make s.. 2024. 8. 20.
[Kotlin][Collection] last / lastOrNull 마지막 요소를 반환합니다.주어진 조건자와 일치하는 마지막 요소를 반환합니다.NoSuchElementException - 배열이 비어 있는 경우public fun List.last(): T { if (isEmpty()) throw NoSuchElementException("List is empty.") return this[lastIndex]}public inline fun List.last(predicate: (T) -> Boolean): T { val iterator = this.listIterator(size) while (iterator.hasPrevious()) { val element = iterator.previous() if (p.. 2024. 8. 20.
[Kotlin][Collection] indexOf / indexOfFirst / indexOfLast 요소의 첫 번째 인덱스를 반환하거나 컬렉션에 요소가 없으면 -1 을 반환합니다.public fun Iterable.indexOf(element: T): Int { if (this is List) return this.indexOf(element) var index = 0 for (item in this) { checkIndexOverflow(index) if (element == item) return index index++ } return -1}val list = listOf(12, 21, 33, 42)print(list.indexOf(21)) // 1 주어진 술어와 일치하는 첫 번째 요소의 인덱스를 반환하거나, 컬.. 2024. 8. 20.
[Kotlin][Collection] first / firstOrNull 첫 번째 요소를 반환합니다. 주어진 조건자와 일치하는 첫 번째 요소를 반환합니다.NoSuchElementException - 컬렉션이 비어 있는 경우public fun Iterable.first(): T { when (this) { is List -> return this.first() else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty.") return iterator.next() } }}public inline fun I.. 2024. 8. 20.
[Kotlin][Collection] elementAt / elementAtOrElse / elementAtOrNull 지정된 인덱스에 있는 요소를 반환하거나 인덱스가 이 컬렉션의 범위를 벗어나면 IndexOutOfBoundsException을 발생시킵니다.public fun Iterable.elementAt(index: Int): T { if (this is List) return get(index) return elementAtOrElse(index) { throw IndexOutOfBoundsException("Collection doesn't contain element at index $index.") }}val list = listOf(1, 2, 3)println(list.elementAt(0)) // 1println(list.elementAt(2)) // 3// list.element.. 2024. 8. 19.
[Kotlin][Collection] contains 컬렉션에서 요소가 발견되면 true 를 반환합니다.public operator fun Iterable.contains(element: T): Boolean { if (this is Collection) return contains(element) return indexOf(element) >= 0}val map: Map = mapOf("x" to 1)println("map.contains(\"x\") is ${map.contains("x")}") // trueprintln("\"x\" in map is ${"x" in map}") // trueprintln("map.contains(\"y\") is ${map.contains("y")}") // falseprintln("\"y\".. 2024. 8. 19.