从长度为 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 =
`, arr)
log(`test =
`, 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 发布文章使用:只允许注册用户才可以访问!