알고리즘 문제가 하나 있어서 풀어 보았습니다.
배열에 여러개의 수들이 있고요.
그 안에 없는 양수 중에 가장 작은 것을 구하는 것입니다.
방금 문제를 풀었고,
코드는 아래와 같습니다.
fun solution(A: IntArray): Int {
if (A.size == 0) {
return 1
}
else if (A.size == 1) {
return when {
(A[0] == 1) -> 2
else -> 1
}
}
else {
A.sort()
var minAbsentNumber = when {
(A[0] <= 0) -> 1
else -> A[0] + 1
}
for (i in 1 until A.size) {
if (A[i] <= 0) {
continue
}
else if ((A[i] - A[i-1]) == 0) {
minAbsentNumber = A[i] + 1
}
else if ((A[i] - A[i-1]) == 1) {
minAbsentNumber = A[i] + 1
}
else if ((A[i] - A[i-1]) > 1) {
minAbsentNumber = A[i-1] + 1
return minAbsentNumber
}
}
return when {
(minAbsentNumber <= 0) -> 1
else -> minAbsentNumber
}
}
}
결과
Compilation successful.
Example test: [1, 3, 6, 4, 1, 2]
OK
Example test: [1, 2, 3]
OK
Example test: [-1, -3]
OK
Your code is syntactically correct and works properly on the example test.
Note that the example tests are not part of your score. On submission at least 8 test cases not shown here will assess your solution.
Posted through the AVLE Dapp (https://avle.io)
[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Upvoted! Thank you for supporting witness @jswit.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
아쉽게도 점수는 저조하네요. 시간내서 문제 푸는 연습을 많이 해야 합니다.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit