思路:

随机选取列表中的一个值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. maven settings.xml windows

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  2. LWIP学习之一些细节

    一 绑定端口后,开启监听,为何监听还要返回一个新的连接?:监听状态的连接只需要很小的内存,于是tcp_listen()就会收回原始连接的内存,而重新分配一个较小内存块供处于监听状态的连接使用. 二 t ...

  3. canvas前端压缩图片

    参考网上的用法,下面是利用canvas进行的图片压缩 <!DOCTYPE html> <html> <head> <meta charset="ut ...

  4. 【Java】 hashcode()和System.identityHashCode()

    hashcode()和System.identityHashCode() openjdk8: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5b86f ...

  5. 使用 h5 新特性,轻松监听任何 App 自带返回键

    var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : ...

  6. css继承性

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  7. apple-touch-icon-precomposed

    <link rel="apple-touch-icon-precomposed" href=""> apple-touch-icon-precomp ...

  8. Linux基础环境_安装配置教程(CentOS7.2 64、JDK1.8、Tomcat8)

    Linux基础环境_安装配置教程 (CentOS7.2 64.JDK1.8.Tomcat8) 安装包版本 1)     VMawre-workstation版本包 地址: https://my.vmw ...

  9. 如何处理VirtualBox启动错误消息:The vboxdrv kernel module is not loaded

    我在启动minikube时,遇到如下错误消息: Starting local Kubernetes v1.10.0 cluster... Starting VM... E1010 03:27:37.9 ...

  10. MovieReview—Wile Hunter(荒野猎人)

     Faith is Power         Faith is power, this sentence is not wrong. Find your own beliefs, and strug ...