575. Distribute Candies 平均分糖果,但要求种类最多
[抄題]:
Given an integer array with?even?length, where different numbers in this array represent different?kinds?of candies. Each number means one candy of the corresponding kind. You need to distribute these candies?equally?in number to brother and sister. Return the maximum number of?kinds?of candies the sister could gain.
Example 1:
Input: candies = [1,1,2,2,3,3] Output: 3 Explanation: There are three different kinds of candies (1, 2 and 3), and two candies for each kind. Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. The sister has three different kinds of candies.?
Example 2:
Input: candies = [1,1,2,3] Output: 2 Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. The sister has two different kinds of candies, the brother has only one kind of candies.?[暴力解法]:
時間分析:
空間分析:
?[優化后]:
時間分析:
空間分析:
[奇葩輸出條件]:
[奇葩corner case]:
[思維問題]:
[一句話思路]:
[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
? [五分鐘肉眼debug的結果]:
[總結]:
hashtable hashset的key都具有單一性 不能重復
[復雜度]:Time complexity: O(n) Space complexity: O(n)
[英文數據結構或算法,為什么不用別的數據結構或算法]:
hashset的key具有單一性 不能重復,對應糖果的種類也是
[關鍵模板化代碼]:
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
?[代碼風格] :
class Solution {public int distributeCandies(int[] candies) {//ccif (candies == null || candies.length == 0) {return 0;}//iniSet kinds = new HashSet();//forfor (int candy : candies) {kinds.add(candy);}//return :return (candies.length / 2) > kinds.size() ? kinds.size() : (candies.length / 2);} } View Code?
轉載于:https://www.cnblogs.com/immiao0319/p/8931800.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的575. Distribute Candies 平均分糖果,但要求种类最多的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开炸鸡店利润怎么样 这个项目很符合大家的
- 下一篇: 个人期货开户条件