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的更多相关文章

  1. 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 ...

  2. Yandex.Algorithm 2011 Round 2 D. Powerful array 莫队

    题目链接:点击传送 D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input ...

  3. 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 ...

  4. Algorithm | Sort

    Bubble sort Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algo ...

  5. 【pat】algorithm常用函数整理

    reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...

  6. [工作积累] 32bit to 64bit: array index underflow

    先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression ...

  7. 全排列算法(字典序法、SJT Algorithm 、Heap's Algorithm)

    一.字典序法 1) 从序列P的右端开始向左扫描,直至找到第一个比其右边数字小的数字,即. 2) 从右边找出所有比大的数中最小的数字,即. 3) 交换与. 4) 将右边的序列翻转,即可得到字典序的下一个 ...

  8. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  9. B. A Leapfrog in the Array

    http://codeforces.com/problemset/problem/949/B Dima is a beginner programmer. During his working pro ...

随机推荐

  1. Docker 常用命令收集

    查看Docker版本 docker version 查看 Image docker images 打包 Image docker save -o ‘packageName.tar’ ‘imageNam ...

  2. shadowssock启动服务

    启动服务:ssserver -c /var/ss/server.json

  3. 0821Servlet基础

    什么是servlet    jsp页面的前身是servlet, 但是servlet和jsp是两个不同概念    servlet是运行在服务器端的一段程序, 是可以直接运行一段java后台代码      ...

  4. NetCore项目的部署

    NetCore项目和以前的AspNet项目在部署上有很大的不同,因为NetCore是跨平台的 NetCore支持Kestrel和IIS两种方式,看代码的11行和14行 using System.IO; ...

  5. 安装SQL2008时显示必须重启计算机才可以继续安装的错误—解决办法

    数据库学习已经有一段时间了,前几天进入了SQL视频的学习,在安装数据库的时候,出现问题,总显示重启失败 ,无法继续安装,如图所示 出现这种问题 ,应该如何解决呢? 解决方法如下: (1).开始菜单-搜 ...

  6. 【计算几何】【bitset】Gym - 101412G - Let There Be Light

    三维空间中有一些(<=2000)气球,一些光源(<=15),给定一个目标点,问你在移除不超过K个气球的前提下,目标点所能接受到的最大光照. 枚举每个光源,预处理其若要照射到光源,需要移走哪 ...

  7. Problem D: 程序填充(递归函数):数列2项和

    Problem D: 程序填充(递归函数):数列2项和 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 2601  Solved: 2117 Descrip ...

  8. Problem Z: 百鸡问题

    #include <stdio.h> int main() { int i, j, k; ; i <= ; i++ ) ; j <= ; j++ ) ; k <= ; k ...

  9. Phpstorm-svn配置

    参考网站; http://blog.csdn.net/Knight_quan/article/details/51889476 1 打开PhpStorm,找到工具  VCS—>Checkout ...

  10. 基于tiny4412的Linux内核移植(支持device tree)(三)

    作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...