fun solution(citations: IntArray): Int {
val descendingArray = citations.sortedArrayDescending()
val size = descendingArray.size
for (h in descendingArray.first() downTo 0){
val up = descendingArray.filter { it>=h }.size
val down = size - up
if( h <= up && h>= down){
return h
}
}
return 0
}
나는 이렇게 품
이거 풀면서 downTo 를 사용해봤는데 이러니까 효율이 좋아지더라 그냥 .. 으로 하는것 보다 (이문제에 한해서)
다른 사람 코드를 보면서 찾은건데 내 코드가 문제가 있음에도 문제 케이스가 다 맞더라.. 문제 자체를 이해를 못했었다.
이 문제는 그냥 h 번 이상 인용된 논문이 h편 이상인 최대 h를 구하기만 하면되는데 이상하게 나는 나머지 논문의 수도 h편 이하여야 된다는 생각을 해버림 쉬운 문제였네..
역시 다른 사람들의 코드를 봐보겠음..
1. list vs array
[비교]배열(Array)과 리스트(List)의 차이
목표 l 배열(Array)과 리스트(List)의 차이를 알자. l 장단점 배열과 리스트의 기본 개념은 차후에 업로드 하겠습니다. 배열( Array )의 특징 - 같은 자료형을 가진 변수를 하나로 나타낸 것. - 연속
changun516.tistory.com
생각보다 큰 차이가 있어서 놀람....
2.
class Solution {
fun solution(citations: IntArray): Int {
val result = citations.sortedArrayDescending()
for (i in 0 until result.size) {
if (result[i] < i + 1) {
return i
}
}
return citations.size
}
}
3.
fun solution3(citations: IntArray) = citations.sortedDescending().mapIndexed { idx, item ->
(idx + 1).coerceAtMost(
item
)
}.maxOrNull()
coerceAtMost 는
Ensures that this value is not greater than the specified maximumValue.
Returns:
this value if it's less than or equal to the maximumValue or the maximumValue otherwise.
2번 코드랑 같은 논리임
'코딩테스트(코틀린) 기초부터 연습' 카테고리의 다른 글
프로그래머스 디스크컨트롤러 힙(Heap) (0) | 2021.04.08 |
---|---|
프로그래머스 디스크컨트롤러 힙(Heap) (0) | 2021.04.06 |
프로그래머스 가장큰수 (0) | 2021.04.05 |
코틀린에서 .map 사용.. 유용함 (0) | 2021.04.05 |
프로그래머스 다리를지나는 트럭 (0) | 2021.04.04 |