美文网首页
215. Kth Largest Element in an A

215. Kth Largest Element in an A

作者: jluemmmm | 来源:发表于2020-08-27 13:54 被阅读0次

    堆排序

    • Runtime: 84 ms, faster than 69.47%
    • Memory Usage: 37.7 MB, less than 47.67%

    时间复杂度O(nlog_2n)
    空间复杂度O(log_2n)

    var findKthLargest = function(nums, k) {
        let len = nums.length
        let mid = Math.floor(len/2)
        for(let i = mid; i-- ; i >= 0) {
            heapSort(nums, i, len)
        }
        for(let i = len - 1; i > len - k; i--) {
            swap(nums, 0, i)
            heapSort(nums, 0, i)
        }
        return nums[0]
    };
    
    var swap = function(nums, a, b) {
        let temp = nums[a]
        nums[a] = nums[b]
        nums[b] = temp
    }
    
    var heapSort = function(nums, index, boundary) {
        let max = index
        let left = 2 * index + 1
        let right = 2 * index + 2
        if(left < boundary && nums[left] > nums[max]) max = left
        if(right < boundary && nums[right] > nums[max]) max = right
        if(max > index) {
            swap(nums, max, index)
            heapSort(nums, max, boundary)
        }
    }
    

    部分排序实现方法

    期望为线性时间的选择算法,将输入数组进行递归划分,快速排序会处理递归的两边,randomized-select只处理划分的一边,这一差异会在性能分析中体现出来,快排 O(nlogn),随机化排序O(n)

    • Runtime: 104 ms, faster than 43.58%
    • Memory Usage: 37.8 MB, less than 45.16%

    时间复杂度O(n)
    空间复杂度O(log_2n)

    var findKthLargest = function(nums, k) {
        return partQuickSort(nums, 0, nums.length - 1, k - 1)
    };
    
    var partition = function(nums, start, end) {
        let flag = nums[end]
        let index = start
        for(let i = start; i < end; i++) {
            if(nums[i] > flag) {
                swap(nums, index, i)
                //[nums[index], nums[i]] = [nums[i], nums[index]]
                index++
            }
        }
        swap(nums, index, end)
        //[nums[index], nums[end]] = [nums[end], nums[index]]
        return index
    }
    
    var partQuickSort = function(nums, start, end, k) {
        let p = partition(nums, start, end)
        if(p === k) return nums[p]
        return p < k ? partQuickSort(nums, p + 1, end, k) : partQuickSort(nums, start, p - 1, k) 
    }
    
    var swap = function(nums, m, n){ 
        let temp = nums[m]
        nums[m] = nums[n]
        nums[n] = temp
    
    }
    

    部分快排

    • Runtime: 76 ms, faster than 94.62%
    • Memory Usage: 39.2 MB, less than 80.84%
    /**
     * @param {number[]} nums
     * @param {number} k
     * @return {number}
     */
    var findKthLargest = function(nums, k) {
        let len = nums.length
        return partQuickSort(nums, 0, len - 1, len - k)
    };
    
    var partQuickSort = function(nums, i, j, k) {
        if(j - i < 1) return nums[i]
        let flag = nums[i]
        let start = i + 1
        let end = j
        while(start <= end) {
            if(nums[start] <= flag) {
                start++
            } else {
                swap(nums, start, end)
                end--
            }
        }
        swap(nums, i, start - 1)
        if(k === start - 1) return nums[k]
        return k > start - 1 ? partQuickSort(nums, start, j, k) : partQuickSort(nums, i, start - 2, k)
    }
    
    var swap = function(nums, i, j) {
        let temp = nums[i]
        nums[i] = nums[j]
        nums[j] = temp
    }
    

    奇奇怪怪的方法

    • Runtime: 176 ms, faster than 12.23%
    • Memory Usage: 37.8 MB, less than 43.73%
    var findKthLargest = function(nums, k) {
        let len = nums.length
        for(let i = 0; i < k - 1; i++) {
            let max = Math.max.apply(this, nums)
            let index = nums.indexOf(max)
            nums.splice(index, 1)
        }
        return Math.max.apply(this, nums)
    };
    

    快速排序实现

    • Runtime: 1076 ms, faster than 5.09%
    • Memory Usage: 172.1 MB, less than 5.04%
    var findKthLargest = function(nums, k) {
        nums = quickSort(nums)
        return nums[k - 1]
    };
    
    var quickSort = function(nums) {
        if(nums.length < 2) return nums
        let flag = nums.pop()
        let less = quickSort(nums.filter(item => item < flag))
        let more = quickSort(nums.filter(item => item >= flag))
        return more.concat([flag], less)
    }
    

    基于快速排序进行改进

    • Runtime: 104 ms, faster than 34.25%
    • Memory Usage: 39.5 MB, less than 54.93%
    /**
     * @param {number[]} nums
     * @param {number} k
     * @return {number}
     */
    var findKthLargest = function(nums, k) {
        let len = nums.length
        let res = partSort(nums, 0, len - 1, len - k)
        if(res === undefined) res = nums[len - k]
        return res
    };
    
    function partSort(nums, i, j, index) {
        if(j - i < 1) return
        let flag = nums[i]
        let start = i + 1
        let end = j
        while(start <= end ) {
            if(nums[start] <= flag) {
                start++
            } else {
                swap(nums, start, end)
                end--
            }
        }
        swap(nums, i, start - 1)
        if(start - 1 === index) {
          return nums[start - 1]
        }
        // debugger
        if(start - 2 < j && start - 2 > i) partSort(nums, i, start - 2, index)
        if (start < j)partSort(nums, start, j, index)
        
    }
    
    var swap = function(nums, i, j) {
        let temp = nums[i]
        nums[i] = nums[j]
        nums[j] = temp
    }
    

    相关文章

      网友评论

          本文标题:215. Kth Largest Element in an A

          本文链接:https://www.haomeiwen.com/subject/fcscsktx.html