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. gulp配合vue压缩代码格式化

    实际项目就是一个单页面.因此,我觉得用gulp足够,并且不需要webpack和vue-cli因为没有必要使用组件. 先来说一下项目结构 1. 然后来看看我的包管理package.json都用了啥,你也 ...

  2. lvm笔记

    安装LVMyum -y install lvm* 创建PV# pvcreate /dev/md5 /dev/sdf1 /dev/sdg 查看PV# pvdisplay 还可以使用命令pvs 和pvsc ...

  3. Djangp2.x版本报错找不到模版目录下的文件

    1.报错内容:django.template.exceptions.TemplateDoesNotExist: index.html 2.解决办法,在settings.py文件中,找到TEMPLATE ...

  4. CentOS7用yum安装MySQL与启动

    首先CentOS7 已经不支持mysql,因为收费了你懂得,所以内部集成了mariadb,而安装mysql的话会和mariadb的文件冲突,所以需要先卸载掉mariadb,以下为卸载mariadb,安 ...

  5. 删除DOM节点应用

    <!-- HTML结构 --> <ul id="test-list"> <li>JavaScript</li> <li> ...

  6. 解决Mysql 服务无法启动 服务没有报告任何错误

    MySQL数据库在升级到5.7版本后,和之前的版本有些不一样,没有data文件夹,我们都知道MySQL数据库文件是保存在data文件夹中的,网上有人说把5.6版本的data文件夹拷贝一个,这种说法听听 ...

  7. C# CreateProcess的测试

    很奇怪的一个现象,在C#中使用Process来启动进程,启动文件名必须是系统指定的扩展名.EXE,而我使用原生的Win32API ::CreateProcess ()并没有这个限制,以后遇到类似的问题 ...

  8. 【Bzoj3527】【Luogu3338】[Zjoi2014]力(FFT)

    题面 Bzoj Luogu 题解 先来颓柿子 $$ F_i=\sum_{j<i}\frac{q_iq_j}{(i-j)^2}-\sum_{j>i}\frac{q_iq_j}{(i-j)^2 ...

  9. 第1天:Ansible安装部署

    Ansible介绍 Ansible是一个简单的自动化引擎,可完成配置管理.应用部署.服务编排以及各种IT需求.它是一款使用Python语言开发实现的开源软件,其依赖Jinjia2.paramiko和P ...

  10. RabbitMQ (八) 队列的参数详解

    代码中,我们通常这样声明一个队列: //声明队列 channel.QueueDeclare ( queue: QueueName, //队列名称 durable: false, //队列是否持久化.f ...