문제링크
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
답안 1)
/**
candyType / 2를 하여 먹을 수 있는 사탕 갯수를 구한다.
candyType을 Set으로 바꿔 중복을 제거한다.
반복문을 돌며 사탕 타입을 확인했거나 먹을 수 있는 사탕의 갯수가 없으면 종료한다.
*/
import java.lang.Math
class Solution {
fun distributeCandies(candyType: IntArray): Int {
var edibleCount = candyType.size / 2
var eatenCount = 0
val candySetSize = candyType.toSet().size
for (i in 0 until candySetSize) {
if (edibleCount <= 0) break
edibleCount -= 1
eatenCount += 1
}
return eatenCount
}
}
답안 2)
import java.lang.Math
class Solution {
fun distributeCandies(candyType: IntArray): Int {
var edibleCount = candyType.size / 2
val candySetSize = candyType.toSet().size
return Math.min(candySetSize, edibleCount)
}
}
'알고리즘 > Leetcode' 카테고리의 다른 글
[leetcode/python3] 1. Two Sum (0) | 2021.08.03 |
---|---|
[leetcode/python3] 1768.Merge Strings Alternately (0) | 2021.04.10 |
[leetcode/kotlin] 104. Maximum Depth of Binary Tree (0) | 2020.12.05 |
[leetcode/kotlin] 1492. The kth Factor of n (0) | 2020.12.05 |
[leetcode/kotlin] 1446. Consecutive Characters (0) | 2020.11.03 |