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 分治算法 在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式.字面上的解释是 ...
随机推荐
- SpringBoot---Web开发---WebSocket
[广播式] 1. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- <Android 应用 之路> 聚合数据SDK
聚合数据介绍 聚合数据是一个为智能手机开发者,网站站长,移动设备开发人员及图商提供原始数据API服务的综合性云数据平台.包含手机聚合,网站聚合,LBS聚合三部分,其功能类似于Google APIS.[ ...
- Yii2 的快速配置 api 服务 yii2-fast-api
yii2-fast-api yii2-fast-api是一个Yii2框架的扩展,用于配置完善Yii2,以实现api的快速开发. 此扩展默认的场景是APP的后端接口开发,因此偏向于实用主义,并未完全采用 ...
- sshd_config配置注释
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-wide c ...
- HDU 4734 F(x) (数位DP,基础)
题意: 一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. ...
- HDU 2089 不要62 (数位DP,入门)
题意: 只要含连续的62,或者含4的车牌号码都是不吉利的,其他都是吉利的组合.问区间[L,R]中有多少个数是吉利的? 思路: 依然是利用树(10进制是十叉树)的思想,统计左边所有子树有多少个数是吉利的 ...
- HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博弈)
思路:若能给对方留下m+1,就可以胜.否则败. #include <iostream> using namespace std; int main() { int t,n,m;cin> ...
- python_91_正则表达式
常用的正则表达式: '.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r& ...
- 树莓派 - 修改pi账号密码,开启root账号
1.修改PI账号的密码 password pi 2.开启root账号 树莓派使用的Linux是debian系统,所以树莓派启用root和debian是相同的. debian里root账户默认没有密码, ...
- Centos7.3 安装devstack stein版本
1. 系统准备 # 关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭selinux setenforce 0 sed -i ...