일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- 안드로이드
- Flow
- coroutinescope
- conflate
- monotone stack
- 백준2309
- Advanced LCA
- Next Challenge
- coroutine
- 백준
- ShapeableImageView
- google play console
- 릴리즈 키해시
- cancellationException
- coldStream
- TOSS 과제
- ServerDrivenUI
- Kotlin
- Android
- java
- coroutinecontext
- collectLatest
- flowon
- withContext
- Product Flavor
- app-distribution
- Algorithm
- KAKAO
- SDUI
- hotStream
- Today
- Total
루피도 코딩한다
[Kotlin] Shortening 1 sec in Problem Solving; withIndex() and associate() Functions 본문
[Kotlin] Shortening 1 sec in Problem Solving; withIndex() and associate() Functions
xiaolin219 2024. 2. 16. 01:10Thanks to Kotlin's functional programming features, we can efficiently solve algorithmic problems compared to using traditional languages. In this post, we'll explore how to transform a list into a map under specific conditions in just one line using withIndex()
and associate()
.
1. Basic Notion
withIndex()
withIndex()
is an extension function for collections in Kotlin. It enables iteration over a collection while accessing both index and value. This feature is useful for operations based on index during iteration.
associate()
associate()
transforms a collection into a map in Kotlin. It takes a lambda function, defining how to convert each element into a key-value pair. This function is handy for converting collections into maps based on specific criteria.
Comparison with mapIndexed()
While withIndex()
and mapIndexed()
both provide index access during iteration, withIndex()
offers a cleaner syntax and is more efficient for large datasets. On the other hand, mapIndexed()
requires explicit index handling, which may introduce performance overhead.
2. Code Using mapIndexed() vs withIndex() and associate()
val list = br.readLine().split(" ").map { it.toInt() }.toIntArray()
val sortedUniqueList = list.toHashSet().sorted().toList()
// This can be change like..
val hashMap = linkedMapOf<Int, Int>()
sortedUniqueList.mapIndexed { idx, value ->
hashMap[value] = idx
}
// this!
val indexMap = sortedUniqueList.withIndex().associate { (index, value) -> value to index }
3. Performance Considerations
One of the most important thing to consider on PS is the performance.
mapIndexed()
operates by executing a lambda for each element and creating a new map, while associate()
creates a new map in one go. Therefore, for very large datasets, associate()
may be slightly more efficient.
For real, when I submit both code one with the mapIndexed()
against the one with the withIndex()
and associate()
, the latter one shows slightly higher time efficiency.
'Algorithm' 카테고리의 다른 글
[카카오 기출/Kotlin] 후보키 (0) | 2024.04.02 |
---|---|
[카카오 기출/kotlin] k진수에서 소수 개수 구하기 (2) | 2024.02.28 |
[백준 11726: 2XN 타일링] kotlin 풀이 (피보나치, 팩토리얼) (1) | 2024.02.15 |
[Graph] Advanced LCA Algorithm (0) | 2023.02.04 |
[백준 6198번: 옥상 정원 꾸미기](Java) (6) | 2022.05.18 |