思路:

随机选取列表中的一个值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. CQRS之旅——前言(翻译)

    探索CQRS和Event Sourcing 本项目聚焦在使用命令和查询分离模式和事件溯源(CQRS+Event Sourcing)构建一个具有高扩展,高可用和高维护性的应用程序. 本项目定位为一个学习 ...

  2. 现阶段github上的emysql编译无法通过的问题

    最近在写db引擎,今天用到了emysql,找到https://github.com/Eonblast/Emysql,拽下来,然后发现竟然编译不通过~~去网上找了下资料,在 http://erlang. ...

  3. Spring Cloud--Feign服务调用组件的使用实例

    引入依赖: 启动类上添加@EnableFeignClients注解: 写调用接口: 直接@Autowired注入服务调用接口: 底层使用了动态代理,对接口进行了实现. 并且封装了RestTemplat ...

  4. 【Unity3D】3D游戏学习

    1.理解游戏元素,简单回答以下问题: 1.1简单介绍一款上世纪(19XX)出品的计算机游戏,然后按课件的方法描述游戏的五个基本元素.(讲目标.玩法.规则) 1.1.1仙剑奇侠传 <仙剑奇侠传&g ...

  5. JavaScript笔记3--标识符和保留字

    1.标识符 javaScript标识符必须以字母,下划线(_)或美元符($)开始.后续的字符可以是字母/数字/下划线/美元符.也可以使用非英语语言或数学符号来书写标识符; 2.保留字 break/de ...

  6. python3.6 配置COCO API出错解决方案

    使用Anaconda Prompt进行安装 问题出现的背景:在尝试使用mask-rcnn时,遇到了这个问题,最终解决掉了

  7. CF Gym 100637K Microcircuits (DP)

    题意:给你n个点,将这些点放在一个环上,问你不相交的连k条线的方案数.(没有重点) 题解:dp[i][j]表示i个点连j条线的方案数,那么新加一个点i, 情况1,i没有和之前的点相连,方案数为dp[i ...

  8. [手势识别] CNN + OpenCV 手势识别记录

    这几天没事,想着再学点一些视觉识别方向的东西,因为之前做了验证码识别,有了机器学习的信心,因此这次打算做个手势识别,参考了很多网上的图像处理方式,中间也遇到了很多问题,最终算是做出来了吧. 1.训练集 ...

  9. python_90_hashlib模块

    #用于加密相关的操作,3.x里代替了2.x中的md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法 import hashlib ...

  10. VS code 豆沙绿护眼主题

    一.下载亮色主题Atom One Light 二.找到settings.JSON,粘贴JSON 快捷键输入  Ctrl+Shift+p   ,输入settings,选择open settings (J ...