[Algorithm] Find Nth smallest value from Array
Q1: Find the smallest value from array:
function findMin(arr) {
let min = arr[0]; for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
} return min; //
}
O(n), cannot be improved anymore, because we have to loop though the array once.
Q2: Find 2nd smallest value, naive solution:
function findSndMin(arr) {
let min = arr[0];
let min2 = arr[1]; for (let i = 0; i < arr.length; i++) {
if (arr[i] < min) {
min2 = min;
min = arr[i];
} else if (arr[i] !== min && arr[i] < min2) {
min2 = arr[i];
}
} return min2; //
}
Q3: Find nth smallest value:
1. Sorting cannot be the best solution, because it do more works, we only care Nth, means I don't care 0...n-1 is sorted or not or n +1....arr.length is sorted or not.
2. Patition: avg can achieve n + n/2 + n/4 + n/8 .... = 2n ~~ O(n), worse: O(n^2)
function findNthMin(arr, m) {
const pivot = arr[0];
let smaller = [];
let larger = []; for (let i = 1; i < arr.length; i++) {
if (arr[i] < pivot) {
smaller.push(arr[i]);
} else {
larger.push(arr[i]);
}
} smaller.push(pivot); arr = [...smaller, ...larger]; if (m > smaller.length) {
return findNthMin(larger, m - smaller.length);
} else if (m < smaller.length) {
return findNthMin(smaller, m);
} else {
return arr[m - 1];
}
} const data = [3, 1, 5, 7, 2, 8];
const res = findNthMin(data, 4);
console.log(res);
Partition vs Heap:
Why here Heap is not a good solution, because we only need Nth samllest value, we don't care 0..n-1, whether those need to be sorted or not. Heap doing more stuff then necessary.
But if the question change to "Find first K smallest value", then Heap is a good solution.
Mean while we need to be carefull that since we just need K samllest, not all N, then we don't need to add everything into the Heap.
if (h,size() >= K) {
if (h.peek() > val(i)) {
h.pop()
h.add(val(i))
}
} else {
h.add(val(i))
}
[Algorithm] Find Nth smallest value from Array的更多相关文章
- algorithm@ find kth smallest element in two sorted arrays (O(log n time)
The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...
- Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队
题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...
- Fast Algorithm To Find Unique Items in JavaScript Array
When I had the requirement to remove duplicate items from a very large array, I found out that the c ...
- Algorithm | Sort
Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algo ...
- 【pat】algorithm常用函数整理
reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...
- [工作积累] 32bit to 64bit: array index underflow
先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression ...
- 全排列算法(字典序法、SJT Algorithm 、Heap's Algorithm)
一.字典序法 1) 从序列P的右端开始向左扫描,直至找到第一个比其右边数字小的数字,即. 2) 从右边找出所有比大的数中最小的数字,即. 3) 交换与. 4) 将右边的序列翻转,即可得到字典序的下一个 ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- B. A Leapfrog in the Array
http://codeforces.com/problemset/problem/949/B Dima is a beginner programmer. During his working pro ...
随机推荐
- Windows+QT+Eclipse+MinGW搭建QT开发环境详细教程
Windows+QT+Eclipse+MinGW搭建QT开发环境详细教程 一.准备工具: QT-SDK for Windows:http://get.qt.nokia.com/qtsdk/qt-sd ...
- [thinkphp] 无限极分类
<?php /* * 无限极分类 类 */ header("Content-Type: text/html; charset=UTF-8"); Class Category ...
- POJ2912 Rochambeau [扩展域并查集]
题目传送门 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4463 Accepted: 1545 ...
- cookies和session区别
session原理:1.session是保存在服务器端,理论上是没有是没有限制,只要你的内存够大 2.浏览器第一次访问服务器时会创建一个session对象并返回一个JSESSIONID=ID的值, ...
- Flask实战第47天:首页导航条首先和代码抽离
新建一个前台页面的父模板front_base.html 导航条是总boostrap v3中文站拷贝过来的,然后根据自己的需求做一些修改 <!DOCTYPE html> <html l ...
- HDU 6336 Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- [CF600E]Dsu on tree
题意:树上每个点都有颜色,称一个颜色占领一棵子树,当且仅当没有别的颜色在这棵子树内的数量比它多.求所有子树的占领颜色之和.题解:最显然的是DFS序+主席树或莫队,这里使用Dsu on tree. 每次 ...
- [BZOJ2007][NOI2010]海拔(对偶图最短路)
首先确定所有点的海拔非0即1,问题转化成裸的平面图最小割问题,进而转化成对偶图最短路(同BZOJ1002). 这题的边是有向的,所以所有边顺时针旋转90度即可. 如下图(S和T的位置是反的). #in ...
- 【构造】AtCoder Regular Contest 079 F - Namori Grundy
对每个点的取值都取最小的可能值. 那个图最多一个环,非环的点的取值很容易唯一确定. 对于环上的点v,其最小可能取值要么是mex{c1,c2,...,ck}(ci这些是v直接相连的非环点)(mex是). ...
- 【带权并查集】Gym - 100923H - Por Costel and the Match
裸题. 看之前的模版讲解吧,这里不再赘述了. #include<cstdio> #include<cstring> using namespace std; int fa[10 ...