“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Hello everyone, I am quick-frozen fish 🐟, a front end of water system 💦, like colorful whistle 💐, continuous sand sculpture 🌲, I am a good brother of the next door Cold grass Whale, I just started to write an article. If you like my article, you can follow ➕ to like it, inject energy into me, and grow with me

Preface 🌧 ️

Algorithms are unfamiliar and familiar to the front-end people, and often we don’t value them as much as the back-end engineers do. But in fact, algorithms have an unshakable position for every programmer.

Because the development process is to convert the actual problem into the computer can recognize the instructions, that is, “data structure” said, “design the data structure, in the application of algorithms on the line”.

The quality of writing instructions will directly affect the performance of the program, and instructions are composed of data structure and algorithm, so the design of data structure and algorithm basically determines the quality of the final program.

In addition, when reading the source code, the lack of understanding of algorithms and data structures makes it difficult to understand why the author wrote the way he did.

In today’s environment, algorithms have become an indispensable skill for front-end engineers. If we want to move beyond being application engineers writing business code, we need to understand algorithms and data structures.

Of course, learning is also focused, as the front end we do not need to fully grasp the algorithm like back-end development, some of the more partial, not practical type and solution method, as long as a little understanding.

The title 🦀

215. The KTH largest element in an array

The difficulty of medium

Given an integer array nums and an integer k, return the KTH largest element in the array.

Note that you need to find the KTH largest element of the sorted array, not the KTH distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2 output: 5Copy the code

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4 output: 4Copy the code

Tip:

  • 1 <= k <= nums.length <= 104
  • -104 <= nums[i] <= 104

🌵

  • See “KTH largest element”
  • Consider choosing the minimum heap

How to solve the problem 🐂

  • Build a minimal heap and insert the values of the array in turn into the heap.
  • When the heap size exceeds K, the heap top is removed.
  • After insertion, the top of the heap is the KTH largest element.

Source 🔥

class MinHeap{
    constructor(){
        this.heap=[];
    }

    swap(i1,i2){
        const temp=this.heap[i1];
        this.heap[i1]=this.heap[i2];
        this.heap[i2]=temp;
    }

    getLeftIndex(i){
        return i*2+1;
    }

    getRightIndex(i){
        return i*2+2;
    }

    getParentIndex(i){
        // Move the binary number one bit to the right
        return (i-1) > >1;
    }

    shiftUp(index){
        if(index == 0) {return;
        }
        const parentIndex=this.getParentIndex(index);
        if(this.heap[parentIndex]>this.heap[index]){
            this.swap(parentIndex,index);
            this.shiftUp(parentIndex); }}shiftDown(index){
        const leftIndex=this.getLeftIndex(index);
        const rightIndex=this.getRightIndex(index);
        if(this.heap[leftIndex]<this.heap[index]){
            this.swap(leftIndex,index);
            this.shiftDown(leftIndex);
        }
        if(this.heap[rightIndex]<this.heap[index]){
            this.swap(rightIndex,index);
            this.shiftDown(rightIndex); }}insert(value){
        this.heap.push(value)
        this.shiftUp(this.heap.length-1)}pop(){
        this.heap[0] =this.heap.pop();
        this.shiftDown(0);
    }

    peek(){
        return this.heap[0];
    }
    size(){
        return this.heap.length; }}/ * * *@param {number[]} nums
 * @param {number} k
 * @return {number}* /
var findKthLargest = function(nums, k) {
    const h=new MinHeap()
    nums.forEach(item= >{
        h.insert(item)
        if(h.size()>k){
            h.pop()
        }
    })
    return h.heap[0]};Copy the code

O(n*log(K))

Space complexity :O(k)

Conclusion 🌞

So the “LeetCode” 215- the KTH largest element in the array ⚡️ is over. There is no shortcut to the algorithm, so we can only write and practice more and summarize more. The purpose of this article is actually very simple, which is to urge myself to complete the algorithm practice and summarize and output. I hope everyone can like my essay, and I also hope to know more like-minded friends through the article. If you also like to toss, welcome to add my friend, sand sculpture together, together progress.

Making 🤖 : sudongyu

Personal blog 👨💻: Frozen fish blog

Vx 👦 : sudongyuer

Write in the last

Guys, if you like my words, give 🐟🐟 a thumbs up 👍 or follow ➕ to support me the most.

Add me on wechat: Sudongyuer, invite you into the group and learning the front together, become a better engineer ~ (group of qr code here – > front to go to bed early, qr code has expired, see the links to the boiling point in the comments, I will put the latest qr code in the comments section, of course, can also add me WeChat I pull you into the group, after all, I also am interesting front end, I also not bad 🌟 ~