思路:

随机选取列表中的一个值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. High waits on control file sequential read

    High waits on control file sequential read (文档 ID 2277867.1) In case we run into an issue where cont ...

  2. 070 Climbing Stairs

    你正在爬楼梯.需要 n 步你才能到达顶部.每次你可以爬 1 或 2 个台阶.你有多少种不同的方式可以爬到楼顶呢?注意:给定 n 将是一个正整数.示例 1:输入: 2输出: 2说明: 有两种方法可以爬到 ...

  3. 4. 把一幅彩色图像的R、G、B分量单独显示。

    #include <cv.h> #include <highgui.h> int main(void) { IplImage* oo = cvLoadImage(); IplI ...

  4. java分为 三类 ME,SE,EE

    java分为 三类  ME,SE,EE Java SE=Java Standard EditionJava EE=Java Enterprise Edition Java ME=Java Mobile ...

  5. nio aio netty区别

    传统io就是bio     同步阻塞         但可以采用伪同步 nio  jdk1.7以前     同步非阻塞io     1.7以后     同步异步非阻塞                  ...

  6. 01SpringBase

    Spring (容器) 概述: 01.Java EE开发者的春天 02.主要目的是 降低业务逻辑层和其他层的耦合度 IOC 03.spring容器是用来创建(new)和管理(对象之间的关系)程序中所有 ...

  7. Mysql系列常见面试题(三)

    1.Mysql数据表在什么情况下容易损坏? 服务器突然断电导致数据文件损坏,强制关机,没有先关闭Mysql服务等. 2.Mysql有关全校的表都有那几个? Mysql服务器通过权限表老控制用户对数据库 ...

  8. IO流----File,递归,字节流,字符流

    要把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化设备(硬盘.光盘.U盘等)上. 当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作. 当把持久设备上的数据读 ...

  9. 【Unity3D】实现太阳系

    实践要求:写一个程序,实现一个完整的太阳系,其他星球围绕太阳的转速必须不一样,并且不再一个法平面内. 法平面是指过空间曲线的切点,且与切线垂直的平面.要求不在一个法平面内,则在保证所有行星以及太阳在一 ...

  10. 织梦ckeditor编辑器 通过修改js去除img标签内的width和height样式

    1. 文件\include\ckeditor\plugins\image\dialogs\image.js 2. 使用工具美化js代码 3. 搜索 setStyle('width', CKEDITOR ...