思路:

随机选取列表中的一个值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的更多相关文章

  1. [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 ...

  2. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  3. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  4. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  5. [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 ...

  6. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  7. 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 ...

  8. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  9. Divide and Conquer.(Merge Sort) by sixleaves

    algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...

  10. [算法]分治算法(Divide and Conquer)

    转载请注明:http://www.cnblogs.com/StartoverX/p/4575744.html 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...

随机推荐

  1. Hive进阶_Hive的客户端操作

    启动远程客户端 # hive --service hiveserver2获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源 工具类 package demo.utils; import j ...

  2. netty~引用对象引用

    从InBound里读取的ByteBuf要手动释放,还有自己创建的ByteBuf要自己负责释放.这两处要调用这个release方法. write Bytebuf到OutBound时由netty负责释放, ...

  3. Spark Mllib里的向量标签概念、构成(图文详解)

    不多说,直接上干货! Labeled point: 向量标签 向量标签用于对Spark Mllib中机器学习算法的不同值做标记. 例如分类问题中,可以将不同的数据集分成若干份,以整数0.1.2,... ...

  4. plsql过期注册

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca 打开plsql工具 点击注册即可

  5. JAVA基础之File类

    个人理解: File是个文件类,可以用其增加.删除.查找某种类型的文件或者文件夹,同时根据其成员变量的特点可以综合利用,避免出现跨系统的时候出现错误,并且查找时最好输入绝对路径,以免出现不存在的文件. ...

  6. 观察者模式和php实现

    观察者模式: 观察者模式(Observer Pattern):定义对象间的一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新.观察者模式又叫做发布-订阅(Publ ...

  7. Kendo MVVM 数据绑定(三) Click

    Kendo MVVM 数据绑定(三) Click Click 绑定可以把由 ViewModel 定义的方法不绑定到目标 DOM 的 click 事件.当点击目标 DOM 元素时触发 ViewModel ...

  8. php分页代码及总结

    代码部分: <?PHPheader("Content-type:text/html;charset=utf-8");$pageSize = 10;//接收传入的分页码$pag ...

  9. 访问权限修饰符-static-final-this-super-匿名对象

    1.this关键字的作用     1)调用本类中的属性;     2)调用本类中的构造方法;且只能放首行,且必须留一个构造方法作为出口,即不能递归调用     3)表示当前对象; 2.匿名对象     ...

  10. 织梦DeDeCMS友情链接文字显示不全

    文件:/include/taglib/flink.lib.php 把下面代码中的24改为合适的值 $attlist=”type|textall,row|24,titlelen|24,linktype| ...