leetcode 561. Array Partition I(C语言)10
来源:互联网 发布:电阻串联和并联的算法 编辑:程序博客网 时间:2024/06/05 11:08
贴原题:
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Example 1: Input: [1,4,3,2]
Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 =
min(1, 2) + min(3, 4). Note: n is a positive integer, which is in the
range of [1, 10000]. All the integers in the array will be in the
range of [-10000, 10000].
解析
本题我用了刚刚做另一道题时http://blog.csdn.net/m0_37454852/article/details/78062299 写的快速排序算法,就很简单了,排好序之后每隔一个相加就行了。
贴代码:
void my_qsort(int* nums, int l, int r){ if(l<r) { int i=l+1;//左端点,从基准值的下一个开始比较 int j=r;//右端点 while(i<j) { if(*(nums+i)>*(nums+l))//若比关键值大,则把该值放到右端点(交换该值与右端点值) { int temp=*(nums+j); *(nums+j)=*(nums+i); *(nums+i)=temp; j--;//右端点右移 } else//否则就选择下一个继续比较 { i++; } } if(*(nums+i)>=*(nums+l)) { i--; } int temp=*(nums+i); *(nums+i)=*(nums+l); *(nums+l)=temp; my_qsort(nums, l, i-1);//递归调用 my_qsort(nums, i+1, r); }}int arrayPairSum(int* nums, int numsSize) { my_qsort(nums, 0, numsSize-1); int sum=0; for(int i=0; i<numsSize; i+=2) { sum+=*(nums+i); } return sum;}
阅读全文
0 0
- leetcode 561. Array Partition I(C语言)10
- 561. Array Partition I(C语言)
- LeetCode——561. Array Partition I(贪心,C++)
- LeetCode 561. Array Partition I
- LeetCode 561. Array Partition I
- [LeetCode]561. Array Partition I
- leetcode 561. Array Partition I
- LeetCode 561. Array Partition I
- Leetcode 561. Array Partition I
- [leetcode]561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode ** 561. Array Partition I
- leetcode.561.Array Partition I
- 【leetcode】561. Array Partition I
- HBase学习笔记——避免热点Region的一些技巧
- Phoenix对QL的支持
- php常见的六种设计模式
- Laravel多条件查询
- Git错误提示之:fatal: Not a git repository (or any of the parent directories): .git
- leetcode 561. Array Partition I(C语言)10
- 流量监控工具iftop
- SD卡用arduino操作的各种方式
- Shell 命令教程(二) 进程,磁盘等操作
- 添加和删除外键约束
- 七参数计算工具教程
- 截取字符串的三种方法
- Team them up! UVA
- 无线端接口调试--重写console.log