简体   繁体   English

我在网上发现的该函数的时间复杂度为O(n)吗?

[英]Is the time complexity of this function I found online O(n)?

I am trying to do this problem on LeetCode , and the following is the solution I found online: 我正在尝试在LeetCode上解决此问题 ,以下是我在网上找到的解决方案:

 // use quick select var findKthLargest = function(nums, k) { var smaller = []; var larger = []; var pivot = nums[parseInt(nums.length/2)]; var pivotCnt = 0; for (var i = 0; i < nums.length; i++) { var num = nums[i]; if(num > pivot) { larger.push(num); } else if(num < pivot) { smaller.push(num); } else { pivotCnt++; } } if (k <= larger.length) { // if larger includes k return findKthLargest(larger, k); } else if (k - larger.length - pivotCnt <= 0) { // k is part of pivot return pivot; } else { // if smaller inclues k return findKthLargest(smaller, k - larger.length - pivotCnt); } }; 

Now I believe this is an O(n) solution, because worst case is that we iterate through the entire array, but I'm not certain. 现在,我相信这是一个O(n)解决方案,因为最坏的情况是我们遍历整个数组,但是我不确定。

Your function appears to be using some sort of divide and conquer approach. 您的功能似乎正在使用某种分而治之的方法。 For each call, it makes a single O(n) pass over the input array, bucketing values larger than, and smaller than, a certain pivot, into two separate arrays. 对于每个调用,它都会使单个O(n)在输入数组上传递,将大于和小于某个特定轴的值存储到两个单独的数组中。 Then, it makes a recursive call on the appropriate subarray. 然后,它对适当的子数组进行递归调用。 In an average case, it will divide the size of the input array by two, until a recursive call is made on just an array of size one, which is the base case. 一般情况下,它将输入数组的大小除以2,直到仅对大小为1的数组进行递归调用为止,这是基本情况。

I would estimate that the running time of this function is O(n*lgn) , which is typical for a divide and conquer algorithms. 我估计此函数的运行时间为O(n*lgn) ,这是分而治之算法的典型值。 Each call does O(n) work, and there would typically be O(lgn) number of recursive calls. 每个调用都执行O(n)工作,并且通常会有O(lgn)个递归调用。

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

相关问题 该函数的时间复杂度是O(N)还是O(N ^ 2)? - Is the time complexity for this function O(N) or O(N^2)? 这个函数的时间复杂度是否为O(log n)? - Is the time complexity of this function O(log n)? 这个算法的时间和空间复杂度是 O(n) 还是 O(1)? - Does this algorithm have time and space complexity O(n) or O(1)? 简化O(nm)和O(n + m)时间复杂度 - Simplifying O(nm) and O(n + m) time complexity 此解决方案O(N)或O(LogN)的时间复杂度是多少? - What is time complexity of this solution O(N) or O(LogN)? 您将如何使用for循环实现O(C ^ n)时间复杂度函数? - How would you implement a O(C^n) time complexity function using for loops? 如果我使用两个变量遍历2D数组,那时间是否仍然是O(n ^ 2)? - If I use two variables to iterate through a 2D array is that still O(n^2) time complexity? 如果我调用一个 function,它在一个 for 循环中包含一个 for 循环,那是 O(n^2) 时间还是 O(n)? - If I call a function that contains a for loop inside a for loop, is that considered O(n^2) time or O(n)? 如何在javascript中返回O(n)时间复杂度相同的字母元素? - how to return same letters elements with O(n) time complexity in javascript? JavaScript中时间复杂度为O(n)的好友推荐算法 - Friends recommendation algorithm with O(n) time complexity in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM