当前位置:网站首页>Day575: divide candy
Day575: divide candy
2022-06-23 01:22:00 【Shallow look】
List of articles
problem : Divide candy
Given an even length array , Different numbers represent different kinds of candy , Each number represents a candy . You need to divide the candy equally between a younger brother and a younger sister . Return the maximum number of sweets sister can get .
Example 1:
Input : candies = [1,1,2,2,3,3]
Output : 3
analysis : There are three kinds of candy , Each has two .
Optimal allocation scheme : Sister get [1,2,3], My brother also got [1,2,3]. In this way, my sister can get the most kinds of candy .
Example 2 :
Input : candies = [1,1,2,3]
Output : 2
analysis : My sister gets candy [2,3], My brother gets candy [1,1], Sister has two different kinds of candy , There is only one kind of brother . In this way, my sister can get the most kinds of candy .
source : Power button (LeetCode)
Train of thought : aggregate (set)
- If the type of candy is greater than n/2,n Is the total number of sweets , Sister can get at most n/2 Grow candy .
- If the candy type is m,m<n/2, Sister can get at most m Grow candy .
- It can be obtained. , The maximum number of sweets my sister can get is min(m,n/2).
class Solution {
public int distributeCandies(int[] candyType) {
Set<Integer> candyTypeSet = new HashSet<Integer>();
for(int candy : candyType){
candyTypeSet.add(candy);
}
return Math.min(candyTypeSet.size(),candyType.length/2);
}
}
Train of thought two : Sort
- Sort the array ;
- Find out how many kinds of candy there are in the array ;
- Returns half the length of the array and the minimum value in the candy category .
class Solution {
public int distributeCandies(int[] candyType) {
Arrays.sort(candyType);
int count = 1;
for(int i=1; i<candyType.length; i++){
if(candyType[i]>candyType[i-1])
count += 1;
}
return Math.min(count,candyType.length/2);
}
}
边栏推荐
- SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
- Pat class A - 1015 reversible primes
- SAP mm me27 create intra company sto order
- OSPF experiment in mGRE environment
- The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection
- The road of architects starts from "storage selection"
- 基于深度学习的视觉目标检测技术综述
- Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc
- E-R diagram
- LeetCode 206. Reverse linked list (iteration + recursion)
猜你喜欢

SAP mm transaction code vl04 create outbound delivery for sto

07 project cost management

cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧

魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择

Overview of visual object detection technology based on deep learning

three. JS simulated driving tour art exhibition hall - creating super camera controller

Cadence spb17.4 - Chinese UI settings

Cadence spb17.4 - Allegro - optimiser la spécification de l'angle de connexion de la polyligne pour une seule ligne électrique - polyligne à arc

Cadence spb17.4 - Allegro - optimize and specify the polyline connection angle of a single electrical line - polyline to arc

SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications
随机推荐
3D printing microstructure
Steps to implement a container global component
Shell logs and printouts
LeetCode 206. 反转链表(迭代+递归)
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection
Pat class A - 1014 waiting in line (bank queuing problem | queue+ simulation)
Pat a - 1010 radical (thinking + two points)
Node fetch download file
Flink synchronizes MySQL data to es
Analysis on the wallet system architecture of Baidu trading platform
Is it safe to open a new bond? How
[22 summer reconstruction 1] codeworks round 791 (Div. 2)
Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
Random decoding NLP
What is the storage structure and mode of data in the database?
Phantomjs Usage Summary
What financial product does the new bond belong to?
cadence SPB17.4 - allegro - 優化指定單條電氣線折線連接角度 - 折線轉圓弧
人民币的单位的大写