从长度为 M 的无序数组中,找出N个最小的数
从长度为 M 的无序数组中,找出 N个最小的数
在一组长度为 n 的无序的数组中,取最小的 m个数(m < n), 要求时间复杂度 O(m * n)

网易有道面试题
const minTopK = (m, n) => {
const obj = {};
for (let i = 0; i < m.length; i++) {
if(!obj.hasOwnProperty(m[i])) {
obj[m[i]] = 1;
} else {
obj[m[i]] += 1;
}
}
const arr = Object.entries(obj).sort((a, b) => a[1] - b[1] > 0 ? 1 : -1);
return arr.slice(0, n).map(item => parseInt(item[0]));
};
demos
如何在 10 亿数中找出前 1000 大的数?
分治法
快速排序 partition
快速排序
O(N*logN)
基本思想:(分治)
先从数列中取出一个数作为key值;
将比这个数小的数全部放在它的左边,大于或等于它的数全部放在它的右边;
对左右两个小数列重复第二步,直至各区间只有1个数。
middle, left, right
const QuickSort = (arr = []) => {
let m = arr[0];
let left = [];
let right = [];
for(let i of arr) {
if(i < m ) {
left.push(i);
} else {
right.push(i);
}
}
if(left.length > 1) {
left = QuickSort(left)
}
if(right.length > 1) {
right = QuickSort(right)
}
return left.concat(right);
}
bug

OK
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-01
* @modified
*
* @description 快速排序 quicksort
* @difficulty Medium
* @complexity O(n*log(n))
* @augments
* @example
* @link https://github.com/xgqfrms/leetcode/issues/7#issuecomment-669991209
* @solutions
*
*/
const log = console.log;
function quickSort(arr) {
// 终止条件
if (arr.length <= 1) {
return arr;
}
// 中间index
var pivotIndex = Math.floor(arr.length / 2);
// 中间值,参考值
var pivot = arr.splice(pivotIndex, 1)[0];
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// 递归
return quickSort(left).concat([pivot], quickSort(right));
};
const arr = [12, 7, 5, 23, 18, 37, 1, 9, 17];
const test = quickSort(arr);
log(`arr =\n`, arr)
log(`test =\n`, test)
/*
arr =
[
12, 7, 5, 23,
37, 1, 9, 17
]
test =
[
1, 5, 7, 9, 12,
17, 18, 23, 37
]
*/
leetcode
TopK
https://leetcode.com/problems/top-k-frequent-elements/
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-089-15
* @modified
*
* @description 347. Top K Frequent Elements
* @difficulty Medium
* @complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/top-k-frequent-elements/
* @link https://leetcode-cn.com/problems/top-k-frequent-elements/
* @solutions
*
*/
const log = console.log;
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
const obj = {};
const result = [];
for (let i = 0; i < nums.length; i++) {
if(!obj.hasOwnProperty(nums[i])) {
obj[nums[i]] = 1;
} else {
obj[nums[i]] += 1;
}
}
const arr = Object.entries(obj).sort((a, b) => a[1] - b[1] > 0 ? -1 : 1);
return arr.slice(0, k).map(item => parseInt(item[0]));
// return Object.entries(obj).sort((a, b) => a[1] - b[1] > 0 ? -1 : 1).slice(0, k).map(item => parseInt(item[0]));
};
refs
https://www.zhihu.com/question/28874340
https://blog.csdn.net/mashuangwe/article/details/76944143
array sort
https://www.cnblogs.com/xgqfrms/p/12440091.html
facebook hard
https://leetcode.com/problems/alien-dictionary/
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
从长度为 M 的无序数组中,找出N个最小的数的更多相关文章
- 从数组中找出第K大的数
利用改进的快排方法 public class QuickFindMaxKValue { public static void main(String[] args) { int[] a = {8, 3 ...
- 数组中找出最小的K个数
题目 给出一个数组,找出K个最小的值 例如给出数组{5,2,4,3,1},给定K值3,则输出结果为{2,3,1} 程序 先给出第一个版本的程序 public static void printKNum ...
- 用C#写一个函数,在一个数组中找出随意几个值相加等于一个值 与迭代器对比
算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} 要找出那些数相加等 ...
- Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)
题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...
- 刷题之给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数
今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的 ...
- C语言 选择排序算法原理和实现 从数组中 找出最小的元素然后交换位置
#include <stdio.h> int main(void) { /* 选择排序算法 原理:从数组中 找出最小的元素然后交换位置: */ int a[10] = {9,5,10,7, ...
- 从数组中找出所有组合为s的数
java版本 package numCombine; /** * 从数组中找出所有组合为s的数 * @author root * */ public class NumComberAll { publ ...
- C语言:对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中。-在数组中找出最小值,并与第一个元素交换位置。
//对传入sp的字符进行统计,三组两个相连字母“ea”"ou""iu"出现的次数,并将统计结果存入ct所指的数组中. #include <stdio.h& ...
- 3sum(从数组中找出三个数的和为0)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
随机推荐
- missing required library sqlite.dll最终解决办法
missing required library sqlite.dll最终解决办法 昨天电脑还是好的,今天早晨打开navicat连接Mysql无缘无故报错"missing required ...
- 为了更好的多线程性能,在对象创建或者更新时,若数据大于2047字节则 Python 的 GIL 会被释放。 执行计算密集型任务如压缩或哈希时释放 GIL
hashlib - Secure hashes and message digests - Python 3.8.3 documentation https://docs.python.org/3.8 ...
- Python基础(列表中变量与内存关系)
在Python中,copy的是内存地址,引用的是列表的引用地址,列表里存的是各个元素的地址 例如: name = [1,2,3,4,['xfxing','summer',6]] n2 = name.c ...
- OIer 生涯绊脚石
字符串 哈希进制搞质数 \({\color{OrangeRed}{KMP}}\) 数组别开太大,否则 \({\color{Gold}{TLE}}\) 没有必要 \({\color{Cyan}{strl ...
- Tomcat 核心组件 Container容器相关
前言 Engine容器 Host容器 前言 Connector把封装了Request对象以及Response对象的Socket传递给了Container容器,那么在Contianer容器中又是怎么样的 ...
- labuladong 算法小抄
<labuladong的算法小抄官方完整版> 本书目前可以手把手带你解决 110 道 LeetCode 算法问题,而且在不断更 新,全部基于 LeetCode 的题目,涵盖了所有题型和技巧 ...
- B - Play on Words
如果这个图是欧拉路,则每个顶点的出度等于入度.即out[i] = in[i] 如果这个图是半欧拉图,则起点的出度比入度大1,终点的入度比出度大1.其余顶点的出度等于入度.如果满足上述条件,就可以将所有 ...
- Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)
This is the hard version of the problem. The difference is the constraint on the sum of lengths of s ...
- POJ - 2553 tarjan算法+缩点
题意: 给你n个点,和m条单向边,问你有多少点满足(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}关系,并把这些点输出(要注意的是这个关系中是蕴含关系而不是且(&&)关系) 题解: ...
- 单源最短路问题 Dijkstra 算法(朴素+堆)
选择某一个点开始,每次去找这个点的最短边,然后再从这个开始不断迭代,更新距离. 代码: 朴素(vector存图) #include <iostream> #include <cstd ...