selection problem-divide and conquer
思路:
随机选取列表中的一个值v,然后将列表分为小于v的,等于v的,大于v的三组。对于k<=left.size()时,
在left中执行selection;落在中间的,返回v;k>left.size()+mid.size()时,在right中执行selection。
需要注意rand()函数的使用。
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- class Solution {
- public:
- int selection(vector<int>& s, int k) {
- // for rand()
- srand((unsigned)time(0));
- // [0,size-1]
- int v = s[rand() % (s.size())];
- vector<int> left, mid, right;
- // assign values to left, mid and right
- for (int i = 0; i < s.size(); i++) {
- if (s[i] < v)
- left.push_back(s[i]);
- else if (s[i] == v)
- mid.push_back(s[i]);
- else
- right.push_back(s[i]);
- }
- // k < v
- if (k <= left.size())
- return selection(left, k);
- else if (k > left.size() && k <= left.size()+mid.size())
- return v;
- else
- // k > v
- return selection(right, k-left.size()-mid.size());
- }
- };
- int main() {
- Solution s;
- int arr[11] = {2,36,5,21,8,13,11,20,5,4,1};
- vector<int> v(arr, arr+11);
- cout << s.selection(v,6) << endl;
- return 0;
- }
selection problem-divide and conquer的更多相关文章
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- the steps that may be taken to solve a feature selection problem:特征选择的步骤
參考:JMLR的paper<an introduction to variable and feature selection> we summarize the steps that m ...
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- [算法]分治算法(Divide and Conquer)
转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
随机推荐
- python中cursor操作数据库(转)
原文出处:http://doudouclever.blog.163.com/blog/static/175112310201284115340663/ python 操作数据库,要安装一个Python ...
- ssis-oracle 数据流任务
[OLE DB 源 1 [16]] 错误: SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER.对连接管理器“F360DB”的 A ...
- Linux之shell命令实现-批量去掉文件名中空格,以及批量修改文件名为数字序号文件名
1 shell下批量出去文件名中的空格 执行看现象: 上面的是执行for循环以后看到的: 然而源目录下的文件如下: 这样的话想要cat某个具体文件是拿不到的,所以需要去空格处理: 处理方式有很多:如 ...
- shell里的IFS内置环境变量
IFS 的全称是 Interal Field Separator ,即“内部区域分隔符”,它也是一个内置环境变量,存储着默认的文本分隔符,默认下这分隔符是空格符(space character),制表 ...
- return void ajax
public class UserInfo { private String name; private Integer age; public String getName() { return n ...
- 数据库操作----找了MySQL和SQL Sever两个的基础语句
这是MySQL的基本操作: 1 登入数据库:mysql -uroot -p+密码 (SQL Sever登入: osql -U 用户名 -P 密码) 显示已存在的数据库:show databases; ...
- BaseAdapter.notifyDataSetChanged()之观察者设计模式及源码分析
BaseAdapter.notifyDataSetChanged()的实现涉及到设计模式-观察者模式,详情请参考我之前的博文设计模式之观察者模式 Ok,回到notifyDataSetChanged进行 ...
- CentOS 7安装Docker服务详细过程
---恢复内容开始--- Docker 简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...
- UVA 562 Dividing coins 分硬币(01背包,简单变形)
题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...
- 洛谷 P2002 消息扩散
题目背景 本场比赛第一题,给个简单的吧,这 100 分先拿着. 题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n ...