561. Array Partition I
来源:互联网 发布:阿里云服务器屏蔽ip 编辑:程序博客网 时间:2024/06/15 12:57
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: 4Explanation: 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].
vactor c++ 用法详情: http://blog.csdn.net/hancunai0017/article/details/7032383
vactor.at(i) 向量第i位上的值
vactor.size() 向量长度
sort(vector.begin,vector.end) c++ STL(标准模板库)自带的排序函数
八大排序: http://blog.csdn.net/hguisu/article/details/7776068
1. 直接插入排序:
每次增加一个数,插入到队列中,插入的地方之后的所有数顺次后移。
时间复杂度:O(n^2)
2.希尔排序:
先将整个序列划分为若干子序列,分别进行直接插入排序,待序列基本有序时,在对全体进行直接插入排序。
- 选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
- 按增量序列个数k,对序列进行k 趟排序;
- 每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。
不稳定排序。
3.简单选择排序:
第i次操作,选出第i位及以后的数中最小的,与第i位上的数交换,共进行n-1次
(简单改进——二元选择排序:每次选两个交换,减少循环次数(哦))
4.堆排序:
首先,堆:父节点比子节点都大(或小),一个数组存储成一个堆,是一颗完全二叉树。
排序过程:
1) 初始建堆
2) 出堆顶节点, 将堆底节点移至堆顶, 调整堆
3) 重复2, 直至只剩一个节点
分析:
2) 出堆顶节点, 将堆底节点移至堆顶, 调整堆
3) 重复2, 直至只剩一个节点
分析:
设树深度为k,。从根到叶的筛选,元素比较次数至多2(k-1)次,交换记录至多k 次。所以,在建好堆后,排序过程中的筛选次数不超过下式:
而建堆时的比较次数不超过4n 次,因此堆排序最坏情况下,时间复杂度也为:O(nlogn )。
5. 冒泡排序:
每次比较两个数, 将较大(小)的数依次交换后移.
6. 快速排序:
选取基准点(假设为左端点), 从两端处理数据, 先比较右端点的值, 若大于基准值, 则继续比较当前点左侧的值, 直到小于, 交换. 再从左侧出发, 同理比较.
当左右侧比较指针重合时, 则将数据划分为两端, 递归再次进行比较.
O(nlogn)
不稳定排序, 效率与基准点选取有关.
7. 归并排序:
阅读全文
0 0
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- java集合
- tomcat启动startup.bat闪退
- 使用Python进行AES加密和解密
- MVP模式初探
- docker 基本使用
- 561. Array Partition I
- 动态规划——背包问题
- HttpImage(网络请求图片)
- 51Nod 1445(考建图的最短路)
- 【QT】QObject简介
- SparseArray、ArrayMap 实现原理学习
- jQuery Growl 消息提醒插件
- openstack使用openvswitch实现vxlan组网
- 10道典型的JavaScript面试题