简体   繁体   English

在给定的一组数字中找到时间最少的第三小数字

[英]Finding third smallest number in a given set of numbers with least time complexity

Here's a working algorithm that finds the third smallest number in a given set of numbers. 这是一个有效的算法,可以在给定的一组数字中找到第三小的数字。

I was looking for another solutions to the given requirement with less time complexity. 我一直在寻找另一种解决方案,以减少时间复杂度来满足给定的要求。

Here's the working code: 这是工作代码:

 Numbers = [3,2,55,-10,-55,5,3,2,1,-5,33,9,-1,4,5]; var x = 0; var y = 0; function FindThirdSmallestNumber() { for(var i=0;i<Numbers.length;i++) { if (Numbers[i] > Numbers[i+1]) { x = Numbers[i]; y = Numbers[i+1]; Numbers[i] = y; Numbers[i+1] = x; i=-1; } else { // } } console.log(Numbers[2]); } FindThirdSmallestNumber(); 

This one should be a lot simpler. 这应该简单得多。 Also not sure about this being any faster but in the most simple/obvious cases less code = better performance. 同样也不确定这会更快,但是在最简单/最明显的情况下,更少的代码=更好的性能。

I just sort the array ascending and get the value based on index. 我只是对数组进行升序排序并根据索引获取值。 So with this code you can get any place; 因此,使用此代码,您可以到达任何地方。 lowest, second lowest, third lowest, etc as long as your index does not go out of range. 最低,第二最低,第三最低等,只要您的索引不超出范围即可。

 const input = [3,2,55,-10,-55,5,3,2,1,-5,33,9,-1,4,5]; function getLowestByRank(data, rank) { data.sort(function(a, b){ return a - b }); return data[rank - 1]; } console.log(getLowestByRank(input, 3)) console.log(getLowestByRank(input, 2)) console.log(getLowestByRank(input, 4)) 

Not sure if this is any faster but it's shorter: 不知道这是否更快,但会更短:

//Use a custom sort function and pass it to the .sort() method
Numbers = Numbers.sort(function(x, y){ return x - y; });
if(Numbers.length > 2){
    //At this point, the 3rd item in the array should be the 3rd lowest
    console.log(Numbers[2]);
}else {
    console.log("There are not 3 numbers in the array.");
}

One option would be to have a separate sorted array of the three smallest numbers so far. 一种选择是拥有到目前为止三个最小数字的单独排序数组。 Whenever you run across a number smaller than the 3rd smallest (the last in the sorted array), reassign that 3rd index to the new number, and sort it again: 每当遇到小于第3个最小数字(排序数组中的最后一个)的数字时,请将该第3个索引重新分配给新数字,然后再次对其进行排序:

 const numbers = [3, 2, 55, -10, -55, 5, 3, 2, 1, -5, 33, 9, -1, 4, 5]; const sortNumeric = (a, b) => a - b; function FindThirdSmallestNumber(input) { const [smallestSoFar, numbers] = [input.slice(0, 3), input.slice(3)]; smallestSoFar.sort(sortNumeric); numbers.forEach((num) => { if (num < smallestSoFar[2]) { smallestSoFar[2] = num; smallestSoFar.sort(sortNumeric); } }); return smallestSoFar[2]; } console.log(FindThirdSmallestNumber(numbers)); 

Note that implementations that sort the whole array (as other answers do) is O(N log N) , while sort ing here is only ever done on an array of size 3, which is significantly less complex ( O(N log 3) I think, which is equivalent to O(N) ) 请注意,对整个数组进行 sort的实现(如其他答案一样)为O(N log N) ,而此处的 sort仅对大小为3的数组执行,而数组的大小则大大简化了( O(N log 3) I认为,相当于O(N)

You can use Math.min function to determine the minimum until you find your X smallest number: 您可以使用Math.min函数来确定最小值,直到找到X最小数字:

Numbers = [3,2,55,-10,-55,5,3,2,1,-5,33,9,-1,4,5];

function FindSmallestNumber(arr, limit) {
   var min = '';
   for(var counter = 1; counter <= limit; counter ++) {
      min = Math.min.apply(Math, arr);
      arr.splice(arr.indexOf(min), 1);
   }   
   console.log(min);
}

FindSmallestNumber(Numbers, 3); //3rd smallest number

I think sorting the array is likely the fastest method, but perhaps you want avoid the built–in sort. 我认为对数组进行排序可能是最快的方法,但也许您想避免使用内置排序。 An alternative is as Chris Li suggests: iterate over the values and just keep the 3 lowest, then return the highest of the three. 正如克里斯·李(Chris Li)所建议的那样:对这些值进行迭代,将三个值保持最低,然后返回三个值中的最高值。

I assumed you want to avoid built-in methods, so this only uses some basic array methods and does everything else manually. 我以为您要避免使用内置方法,因此这仅使用一些基本的数组方法,而其他操作则手动进行。

 // Avoid Math.max function getMax(arr) { var max = -Infinity; for (var i=0, iLen=arr.length; i<iLen; i++) { if (arr[i] > max) max = arr[i]; } return max; } // If data.length < 4, just returns largest value // Iterates over data once function get3rdSmallest(data) { // Helper to insert value in order // Expects arr to be length 3 or smaller function insertNum(arr, num) { if (!arr.length || num < arr[0]) { nums.unshift(num); } else if (num > arr[arr.length-1]) { arr.push(num); } else { arr[2] = arr[1]; arr[1] = num; } } var num, nums = []; if (data.length < 4) { return getMax(data); } for (var i=0, iLen=data.length; i<iLen; i++) { num = data[i]; // Insert first 3 values sorted if (nums.length < 3) { insertNum(nums, num); // If num is smaller than largest value in nums, // remove move largest and insert num } else if (num < nums[2]){ nums.splice(-1); insertNum(nums, num); } } // Return highest (last) value in nums return nums[2]; } var data = [3,2,55,-10,-55,5,3,2,1,-5,33,9,-1,4,5]; console.log(get3rdSmallest([-4,-22])); // -4 console.log(get3rdSmallest([4,0,1,7,6])); // 4 console.log(get3rdSmallest(data)); // -5 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM