“This is the 7th 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 🦀
Merge K ascending linked lists
Difficult difficult
You are given an array of linked lists, each of which has been sorted in ascending order.
Please merge all lists into one ascending list and return the merged list.
Example 1:
Input: lists = [].enlighened by [1 4], [2, 6]] output:,1,2,3,4,4,5,6 [1] : list array is as follows: [1 - > > 5, 4-1 - > 3 - > 4, 6] 2 - > merge them into an ordered list. 1 - > 1 - > 2 - > 3 - > 4 - > 4 - > 5 - > 6Copy the code
Example 2:
Input: Lists = [] Output: []Copy the code
Example 3:
Input: Lists = [[]] Output: []Copy the code
Tip:
k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i]
按 ascendingarrangementlists[i].length
The sum of does not exceed10 ^ 4
🌵
- The next node in a new list must be the smallest node in K lists
- Consider choosing the minimum heap
How to solve the problem 🐂
- Build a minimal heap and insert the list heads into the heap in turn.
- Pop the top of the heap to the output list and insert the new list head of the list with the top of the heap into the heap.
- When all the heap elements pop up, the merge is complete.
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]? .val>this.heap[index]? .val){this.swap(parentIndex,index);
this.shiftUp(parentIndex); }}shiftDown(index){
const leftIndex=this.getLeftIndex(index);
const rightIndex=this.getRightIndex(index);
if(this.heap[leftIndex]? .val<this.heap[index]? .val){this.swap(leftIndex,index);
this.shiftDown(leftIndex);
}
if(this.heap[rightIndex]? .val<this.heap[index]? .val){this.swap(rightIndex,index);
this.shiftDown(rightIndex); }}insert(value){
this.heap.push(value)
this.shiftUp(this.heap.length-1)}pop(){
if(this.size()===1) {return this.heap.shift()};
const top=this.heap[0]
this.heap[0] =this.heap.pop();
this.shiftDown(0);
return top;
}
peek(){
return this.heap[0];
}
size(){
return this.heap.length; }}/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */
/ * * *@param {ListNode[]} lists
* @return {ListNode}* /
var mergeKLists = function(lists) {
const res=new ListNode(0)
const h = new MinHeap()
let p = res
lists.forEach(l= >{
if(l)h.insert(l);
})
while(h.size()){
const n = h.pop()
p.next=n;
p=p.next
if(n.next){
h.insert(n.next)
}
}
return res.next
};
Copy the code
O(n*log(K))
Space complexity :O(k)
Conclusion 🌞
So the “LeetCode” 23- merge K ascending linked list ⚡️ is over. There is no shortcut to 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 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 🌟 ~