leetcode.575. Distribute Candies
来源:互联网 发布:sql server 2017下载 编辑:程序博客网 时间:2024/05/22 14:19
Question
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: 3Explanation: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: 2Explanation: 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.
Note:
- The length of the given array is in range [2, 10,000], and will be even.
- The number in given array is in range [-100,000, 100,000].
Solution
#define min(x,y) ( x>y?y:x )int compare( const void* a, const void* b){ int int_a = * ( (int*) a ); int int_b = * ( (int*) b ); if ( int_a == int_b ) return 0; else if ( int_a < int_b ) return -1; else return 1;}int distributeCandies(int* candies, int candiesSize) { int i, j, kinds = 1; // sort qsort( candies, candiesSize, sizeof(int), compare ); // count total kinds of number for (i = 0, j = 1; i < candiesSize - 1, j < candiesSize; i++, j++) { kinds += candies[i]!=candies[j]; } return min(kinds, candiesSize/2);}
阅读全文
0 0
- LeetCode | 575. Distribute Candies
- leetcode 575. Distribute Candies
- [LeetCode]575. Distribute Candies
- [leetcode]: 575. Distribute Candies
- [leetcode]575. Distribute Candies
- LeetCode 575. Distribute Candies
- leetcode.575. Distribute Candies
- LeetCode 575. Distribute Candies
- [LeetCode]575.Distribute Candies
- leetcode 575. Distribute Candies
- LeetCode: 575. Distribute Candies
- LeetCode#575. Distribute Candies
- LeetCode 575. Distribute Candies
- 【LeetCode】575. Distribute Candies
- [LeetCode]575. Distribute Candies
- [LeetCode]575. Distribute Candies
- <LeetCode>575. Distribute Candies
- leetcode 575. Distribute Candies
- readonly与disabled的区别
- 使用SPRING提供远程和WEB服务
- 状态栏使用
- C#FileStream 读写文件,StreamRead StreamWrite
- JVM内存模型和垃圾收集
- leetcode.575. Distribute Candies
- 在Centos 7中安装 Mysql
- 算法----五大算法之回溯法
- 粗略分析Python中的内存泄漏
- 《用Python写网络爬虫》示例网站访问不了导致的系列问题解决办法
- 000050:编写java程序,使用while循环语句计算1+1/2!+1/3!...1/20!之和
- 自定义 QTreeView
- 前端规范(3)--JavaScript规范
- 利用runTime实现UIWebView 与 JS 随意交互,JS跳转原生页面,亦可实现推送界面万能动态跳转