πΊπΏ Stack va Queue.
Hammaga salom! Shu kunlar davomida o'tilgan mavzularga havolalarni qoldiraman.
Stack:
1. Nazariya. Stack.
2. Nazariya. Stack-ning metodlari.
Queue:
1. Nazariya. Queue.
2. Queue metodlari va implementatsiyasi.
2. Queue metodlari va implementatsiyasi. 2-qism.
Keyinchalik shu mavzularni takrorlash uchun qaytib kelganingizda, topish qulay bo'ladi degan umiddaman.
Omad! βοΈπ€
#stack #queue #wrapup
@leetcodin
Hammaga salom! Shu kunlar davomida o'tilgan mavzularga havolalarni qoldiraman.
Stack:
1. Nazariya. Stack.
2. Nazariya. Stack-ning metodlari.
Queue:
1. Nazariya. Queue.
2. Queue metodlari va implementatsiyasi.
2. Queue metodlari va implementatsiyasi. 2-qism.
Keyinchalik shu mavzularni takrorlash uchun qaytib kelganingizda, topish qulay bo'ladi degan umiddaman.
Omad! βοΈπ€
#stack #queue #wrapup
@leetcodin
Telegram
LeetCodin
πΊπΏ Nazariya. Stack.
Hammaga salom! Sizlar bilan Linked List mavzusini ko'rib chiqdik. Keling endi shu mavzuning mantiqiy davomi - shu ma'lumot tuzilmasi [MT] asosida qurilgan Stack va Queue MT-larini ko'rib chiqamiz. Stack-dan boshlaymiz.
Stack - bu faqatβ¦
Hammaga salom! Sizlar bilan Linked List mavzusini ko'rib chiqdik. Keling endi shu mavzuning mantiqiy davomi - shu ma'lumot tuzilmasi [MT] asosida qurilgan Stack va Queue MT-larini ko'rib chiqamiz. Stack-dan boshlaymiz.
Stack - bu faqatβ¦
π9
π¬π§ Implement Queue using Stacks
Hi, guys! Since we covered Stack and Queue, let's solve two interesting problems. We will start from building Queue using Stack(s).
π’ Easy: 232. Implement Queue using Stacks
π¨βπ» My solution: Java | 0 ms | 100% Faster | Explained
I think now you can solve this problem.
Enjoy! βοΈπ€
Hi, guys! Since we covered Stack and Queue, let's solve two interesting problems. We will start from building Queue using Stack(s).
π’ Easy: 232. Implement Queue using Stacks
π¨βπ» My solution: Java | 0 ms | 100% Faster | Explained
I think now you can solve this problem.
Enjoy! βοΈπ€
Telegram
LeetCodin
π¬π§ Stack n Queue.
Hi, guys! Let's wrap up our latest topics in this post.
Stack:
1. Theory. Stack.
2. Theory. Stack methods.
Queue:
1. Theory. Queue.
2. Queue methods and implementation.
2. Queue methods and implementation. Part 2.
These links are forβ¦
Hi, guys! Let's wrap up our latest topics in this post.
Stack:
1. Theory. Stack.
2. Theory. Stack methods.
Queue:
1. Theory. Queue.
2. Queue methods and implementation.
2. Queue methods and implementation. Part 2.
These links are forβ¦
π3
πΊπΏ Queue-ni Stack ishlatgan holda yozing
Hammaga salom! Sizlar bilan Stack va Queue mavzularini ko'rib chiqdik. Keling endi ikkita qiziqarli masalalarni yechamiz. Queue-ni Stack(-lar) ishlatgan holda yozish masalasidan boshlaymiz.
π’ Oson: 232. Implement Queue using Stacks
π¨βπ» Mening yechimim: Java | 0 ms | 100% Faster | Explained
Umid qilamanki endi bu masalani yecha olamiz.
Omad! βοΈπ€
Hammaga salom! Sizlar bilan Stack va Queue mavzularini ko'rib chiqdik. Keling endi ikkita qiziqarli masalalarni yechamiz. Queue-ni Stack(-lar) ishlatgan holda yozish masalasidan boshlaymiz.
π’ Oson: 232. Implement Queue using Stacks
π¨βπ» Mening yechimim: Java | 0 ms | 100% Faster | Explained
Umid qilamanki endi bu masalani yecha olamiz.
Omad! βοΈπ€
Telegram
LeetCodin
πΊπΏ Stack va Queue.
Hammaga salom! Shu kunlar davomida o'tilgan mavzularga havolalarni qoldiraman.
Stack:
1. Nazariya. Stack.
2. Nazariya. Stack-ning metodlari.
Queue:
1. Nazariya. Queue.
2. Queue metodlari va implementatsiyasi.
2. Queue metodlari va iβ¦
Hammaga salom! Shu kunlar davomida o'tilgan mavzularga havolalarni qoldiraman.
Stack:
1. Nazariya. Stack.
2. Nazariya. Stack-ning metodlari.
Queue:
1. Nazariya. Queue.
2. Queue metodlari va implementatsiyasi.
2. Queue metodlari va iβ¦
π5
π¬π§ Implement Queue using Stacks
π’ Easy: 232. Implement Queue using Stacks
π¨βπ» My solution: Java | 0 ms | 100% Faster | Explained
π¨βπ» Task
Our task is to build a Queue using Stack(s).
π‘ Idea
The idea is to use two Stacks and copy them to each other by popping out elements from one and pushing to another (reversing). Thus, we will have a stack with peek() showing the first inserted element.
π¨βπ» Solution
1.
2. In pop() method I reverse [
3. Inside peek() method I do the same, but instead of popping out I just call peek() on reversed stack.
4. Inside
This is it. Hope you guys liked the problem and the solution.
Good luck! βοΈπ€
π’ Easy: 232. Implement Queue using Stacks
π¨βπ» My solution: Java | 0 ms | 100% Faster | Explained
π¨βπ» Task
Our task is to build a Queue using Stack(s).
π‘ Idea
The idea is to use two Stacks and copy them to each other by popping out elements from one and pushing to another (reversing). Thus, we will have a stack with peek() showing the first inserted element.
π¨βπ» Solution
1.
copyStackFirstToTwo() inside of this method I copy elements and compose reversed stack. 2. In pop() method I reverse [
copyStackFirstToTwo()] main stack into stackReverse, then pop() element and write back [copyStackFirstToTwo()] to main stack.3. Inside peek() method I do the same, but instead of popping out I just call peek() on reversed stack.
4. Inside
push() I just push an element into main stack.This is it. Hope you guys liked the problem and the solution.
Good luck! βοΈπ€
LeetCode
Implement Queue using Stacks - LeetCode
Can you solve this real interview question? Implement Queue using Stacks - Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implementβ¦
Implementβ¦
π4
πΊπΏ Queue-ni Stack ishlatgan holda yozing
π’ Oson: 232. Implement Queue using Stacks
π¨βπ» Mening yechimim: Java | 0 ms | 100% Faster | Explained
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz - bu Queue-ni Stack(-lar)ni ishlatgan holda yozish.
π‘ G'oya
Men yozgan yechimning tagidagi g'oya - bu ikkita stack ishlatib, bitta stack-dan ikkinchi stack-ga teskari navbatda elementlarni yozish. Chunki Stack ma'lumotlar tuzilmasida oxirgi kiritilgan element birinchi navbatda keladi va birinchi kiritilgan element - oxirgi navbatda. Agar Stack bo'ylab teskarisiga yurib chiqsak, birinchi kiritilgan element birinchi keladi va oxirgi kiritilgan element - oxirgi bo'ladi. Bu esa Queue-ni ishlash qoidasi.
π¨βπ» Yechim
1.
2.
3.
4.
Shu xolos. Sizlarga shu masala yoqdi degan umiddaman.
Omad! βοΈπ€
π’ Oson: 232. Implement Queue using Stacks
π¨βπ» Mening yechimim: Java | 0 ms | 100% Faster | Explained
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz - bu Queue-ni Stack(-lar)ni ishlatgan holda yozish.
π‘ G'oya
Men yozgan yechimning tagidagi g'oya - bu ikkita stack ishlatib, bitta stack-dan ikkinchi stack-ga teskari navbatda elementlarni yozish. Chunki Stack ma'lumotlar tuzilmasida oxirgi kiritilgan element birinchi navbatda keladi va birinchi kiritilgan element - oxirgi navbatda. Agar Stack bo'ylab teskarisiga yurib chiqsak, birinchi kiritilgan element birinchi keladi va oxirgi kiritilgan element - oxirgi bo'ladi. Bu esa Queue-ni ishlash qoidasi.
π¨βπ» Yechim
1.
copyStackFirstToTwo() bu metodning ichida birinchi stack-ni ikkinchi stack-ga teskari navbatda ko'chiraman.2.
pop() ichida boshida teskari stack-ni [copyStackFirstToTwo()] stackReverse ichiga ko'chiraman. Keyin shu stack-da pop() metodini chaqirib, asosiy stack-ga qayta yozib qo'yaman.3.
peek() ichida esa huddi shu harakatlar bajariladi, faqat pop() metodi o'rniga teskari stack-ning peek() metodi chaqiriladi.4.
push() ichida asosiy stack-ga elementni qo'shaman.Shu xolos. Sizlarga shu masala yoqdi degan umiddaman.
Omad! βοΈπ€
LeetCode
Implement Queue using Stacks - LeetCode
Can you solve this real interview question? Implement Queue using Stacks - Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implementβ¦
Implementβ¦
π8
π¬π§ 225. Implement Stack using Queues.
Hi, guys! Let's solve another problem which is the logical continuation of the series. Implement Stack DS with Queue(s).
π’ Easy: 225. Implement Stack using Queues
π¨βπ» My solution: Java | Using 2 Queues
My solution is not the best out there. So, here is the tip: this can be done by using one queue. Think about it. π§
Good luck! βοΈπ€
Hi, guys! Let's solve another problem which is the logical continuation of the series. Implement Stack DS with Queue(s).
π’ Easy: 225. Implement Stack using Queues
π¨βπ» My solution: Java | Using 2 Queues
My solution is not the best out there. So, here is the tip: this can be done by using one queue. Think about it. π§
Good luck! βοΈπ€
LeetCode
Implement Stack using Queues - LeetCode
Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement theβ¦
Implement theβ¦
π2
πΊπΏ Stack-ni Queue ishlatgan holda yozing
Hammaga salom! Keling kecha yechgan masalaning mantiqiy davomi - Stack-ni Queue(-lar) ishlatgan holda yozish masalasini yechaylik.
π’ Oson: 225. Implement Stack using Queues
π¨βπ» Mening yechimim: Java | Using 2 Queues
Mening yechimimdan ham effektivroq yechim bor. Ikkita emas, bitta Queue ishlatib ham yechsangiz bo'ladi. O'ylab ko'ring.π§
Omad! βοΈπ€
Hammaga salom! Keling kecha yechgan masalaning mantiqiy davomi - Stack-ni Queue(-lar) ishlatgan holda yozish masalasini yechaylik.
π’ Oson: 225. Implement Stack using Queues
π¨βπ» Mening yechimim: Java | Using 2 Queues
Mening yechimimdan ham effektivroq yechim bor. Ikkita emas, bitta Queue ishlatib ham yechsangiz bo'ladi. O'ylab ko'ring.π§
Omad! βοΈπ€
LeetCode
Implement Stack using Queues - LeetCode
Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement theβ¦
Implement theβ¦
π5
π¬π§ LeetCode 600.
Just wanted to share that I was able to achieve another milestone - 600 solved problems on LeetCode.
What went better during last 100 problems:
- I challenged myself to try more medium and hard level problems. As a result I solved 74 medium and hard ones vs 26 easy problems. That is almost 75%!
- I'm solving medium problems faster than before.
Yay! π₯
πΊπΏ LeetCode 600.
Bugun yana bitta milestone-ga erishganimni yozmoqchiman - LeetCode platformasida 600ta masala yechibman.
Oxirgi 100ta masalalar yechish davomida:
- O'zimga ko'proq o'rta va qiyin darajali masalalarni yechish maqsad qilib qo'ygan edim. Natijada 74ta o'rta va qiyin va 26ta oson masala yechibman. Bu deyarli 75%! Yomon emas.
- O'rta darajali masalalarga yechimlarini tezroq topa olayapman.
Oldik! π₯
@leetcodin
Just wanted to share that I was able to achieve another milestone - 600 solved problems on LeetCode.
What went better during last 100 problems:
- I challenged myself to try more medium and hard level problems. As a result I solved 74 medium and hard ones vs 26 easy problems. That is almost 75%!
- I'm solving medium problems faster than before.
Yay! π₯
πΊπΏ LeetCode 600.
Bugun yana bitta milestone-ga erishganimni yozmoqchiman - LeetCode platformasida 600ta masala yechibman.
Oxirgi 100ta masalalar yechish davomida:
- O'zimga ko'proq o'rta va qiyin darajali masalalarni yechish maqsad qilib qo'ygan edim. Natijada 74ta o'rta va qiyin va 26ta oson masala yechibman. Bu deyarli 75%! Yomon emas.
- O'rta darajali masalalarga yechimlarini tezroq topa olayapman.
Oldik! π₯
@leetcodin
π19πΎ5π3π1
π¬π§ 225. Implement Stack using Queues.
π’ Easy: 225. Implement Stack using Queues
π¨βπ» My solution: Java | Using 2 Queues
Hi, guys! Let's review the solution of a user StefanPochmann on LeetCode. He has elegant and concise solution - link.
π‘ Idea
His idea is to use Queue inside
π¨βπ» Solution
His solution is simple and brilliant at the same time.
The only method we need to review is
OK.
1. He adds new element to the end of the queue.
2. Then he removes element from the queue and adds this element again to the queue. This happens
Example:
- Let's say we have a queue: 1, 2, 3.
- You need to push 4. You add 4 to the queue. Now you queue looks like this: 4,1,2,3. Here 3 is first to be removed.
- Then you remove and add one-by-one: remove 3 [4,1,2] and add 3 => 3,4,1,2.
- Then remove 2 [3,4,1] and add 2 => 2,3,4,1.
- Then remove 1 [2,3,4] and add 1 => 1,2,3,4.
So, we repeated this action
This is it. Hope you enjoyed!
Good luck! βοΈπ€
@leetcodin
π’ Easy: 225. Implement Stack using Queues
π¨βπ» My solution: Java | Using 2 Queues
Hi, guys! Let's review the solution of a user StefanPochmann on LeetCode. He has elegant and concise solution - link.
π‘ Idea
His idea is to use Queue inside
push() method. The trick here is that he adds new element to the end of the queue and then copies it and at the same time removes copied elements, except last added. This way he orders the queue such a way that the most recently added will be first to remove.π¨βπ» Solution
His solution is simple and brilliant at the same time.
The only method we need to review is
push() method, since the rest methods just do what queue's relevant methods do.OK.
1. He adds new element to the end of the queue.
2. Then he removes element from the queue and adds this element again to the queue. This happens
n - 1 times, where n is the size of the queue. What does this action give us?Example:
- Let's say we have a queue: 1, 2, 3.
- You need to push 4. You add 4 to the queue. Now you queue looks like this: 4,1,2,3. Here 3 is first to be removed.
- Then you remove and add one-by-one: remove 3 [4,1,2] and add 3 => 3,4,1,2.
- Then remove 2 [3,4,1] and add 2 => 2,3,4,1.
- Then remove 1 [2,3,4] and add 1 => 1,2,3,4.
So, we repeated this action
n - 1 times and as a result we have a queue where elements are ordered from recent to oldest [for removal].This is it. Hope you enjoyed!
Good luck! βοΈπ€
@leetcodin
LeetCode
Implement Stack using Queues - LeetCode
Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement theβ¦
Implement theβ¦
π3
πΊπΏ 255. Stack-ni Queue ishlatgan holda yozing
π’ Oson: 225. Implement Stack using Queues
π¨βπ» Mening yechimim: Java | Using 2 Queues
Hammaga salom! Keling kechagi masala uchun StefanPochmann yozgan yechimini ko'rib chiqamiz. Uning yechimi juda ham nafis va qisqa - havola.
π‘ G'oya
Shu odam
π¨βπ» Yechim
Uning yechimi oddiygini va bir paytda ajoyib.
Biz faqat
Ho'p.
1. U yangi elementni queue-ni oxiriga joylashtiradi.
2. Keying u queue-ning birinchi qo'shilgan elementini o'chirib, shu elementni queue-ga yana qo'shadi. Shu harakatlarni u
Misol:
- Tasavvur qiling, bizda quyidagi queue bor: 1, 2, 3. [Bu yerda 3 soni birinchi bo'lib olib tashlanadi.]
- Endi biz shu queue-ga 4 sonini qo'shishimiz kerak. 4 queue-ning oxiriga qo'shamiz. Endi queue-ning ko'rinishi bunday: 4,1,2,3.
- Keyin siz har bir elementni o'chirib shu paytda uni yani queue-ga qo'shasiz: 3 o'chirib [4,1,2], yana 3 qo'shamiz => 3,4,1,2.
- Keyin 2 o'chirib [3,4,1], yana 2 qo'shamiz => 2,3,4,1.
- Keyin 1 o'chirib [2,3,4], yana 1 qo'shamiz => 1,2,3,4.
Shunday qilib, biz shu harakatlarni
Bugunga shu. Sizlarga ham shu yechim yoqdi degan umiddaman!
Omad! βοΈπ€
@leetcodin
π’ Oson: 225. Implement Stack using Queues
π¨βπ» Mening yechimim: Java | Using 2 Queues
Hammaga salom! Keling kechagi masala uchun StefanPochmann yozgan yechimini ko'rib chiqamiz. Uning yechimi juda ham nafis va qisqa - havola.
π‘ G'oya
Shu odam
push() metod ichida bitta ajoyib g'oyani ishlatgan - u yangi elementni queue-ni oxiriga qo'shadi va keyin queue-dan elementlarni o'chirib, huddi shu elementlarni qo'shib chiqadi, oxirgi qo'shilgandan tashqari. Bu harakatlar orqali u queue-dagi elementlari kerakli navbatda joylashtiradi, boshqa so'z bilan aytganda eng oxirgi qo'shilgan element olib tashlash uchun eng birinchi bo'lib qoladi.π¨βπ» Yechim
Uning yechimi oddiygini va bir paytda ajoyib.
Biz faqat
push() metodini ko'rib chiqamiz, chunki qolgan metodlar queue-ning metodlarini takrorlaydi.Ho'p.
1. U yangi elementni queue-ni oxiriga joylashtiradi.
2. Keying u queue-ning birinchi qo'shilgan elementini o'chirib, shu elementni queue-ga yana qo'shadi. Shu harakatlarni u
n - 1 marta takrorlaydi, bu yerda n - bu queue-ning ichida joylashgan elementlar soni. Ho'sh, bu harakatlar nima beradi?Misol:
- Tasavvur qiling, bizda quyidagi queue bor: 1, 2, 3. [Bu yerda 3 soni birinchi bo'lib olib tashlanadi.]
- Endi biz shu queue-ga 4 sonini qo'shishimiz kerak. 4 queue-ning oxiriga qo'shamiz. Endi queue-ning ko'rinishi bunday: 4,1,2,3.
- Keyin siz har bir elementni o'chirib shu paytda uni yani queue-ga qo'shasiz: 3 o'chirib [4,1,2], yana 3 qo'shamiz => 3,4,1,2.
- Keyin 2 o'chirib [3,4,1], yana 2 qo'shamiz => 2,3,4,1.
- Keyin 1 o'chirib [2,3,4], yana 1 qo'shamiz => 1,2,3,4.
Shunday qilib, biz shu harakatlarni
n - 1 marta bajardik va natijada [olib tashlash uchun] queue ichida elementlar eng oxirida qo'shilgandan boshlab, eng birinchi qo'shilgangacha joylashdi.Bugunga shu. Sizlarga ham shu yechim yoqdi degan umiddaman!
Omad! βοΈπ€
@leetcodin
LeetCode
Implement Stack using Queues - LeetCode
Can you solve this real interview question? Implement Stack using Queues - Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement theβ¦
Implement theβ¦
π6
π¬π§ 1441. Build an Array With Stack Operations
Hi, guys! Another day - another Stack challenge. Keep it going!
π‘ Medium: 1441. Build an Array With Stack Operations
π¨βπ» My solution: Java Simple 0ms | 100 Faster Solution
Our task is to return an array which would show push() and pop() method calls in correct order depending on given array.
Good luck! βοΈπ€
Hi, guys! Another day - another Stack challenge. Keep it going!
π‘ Medium: 1441. Build an Array With Stack Operations
π¨βπ» My solution: Java Simple 0ms | 100 Faster Solution
Our task is to return an array which would show push() and pop() method calls in correct order depending on given array.
Good luck! βοΈπ€
LeetCode
Build an Array With Stack Operations - LeetCode
Can you solve this real interview question? Build an Array With Stack Operations - You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
π5
πΊπΏ 1441. Build an Array With Stack Operations
Hammaga salom! Bugun ham masala yechamiz! To'xtamaymiz!
π‘ O'rta daraja: 1441. Build an Array With Stack Operations
π¨βπ» Mening yechimim: Java Simple 0ms | 100 Faster Solution
Bizning vazifamiz bu Stack-ning metodlarini chaqirish navbatini berilgan massiv uchun to'g'ri chiqarish.
Omad! βοΈπ€
Hammaga salom! Bugun ham masala yechamiz! To'xtamaymiz!
π‘ O'rta daraja: 1441. Build an Array With Stack Operations
π¨βπ» Mening yechimim: Java Simple 0ms | 100 Faster Solution
Bizning vazifamiz bu Stack-ning metodlarini chaqirish navbatini berilgan massiv uchun to'g'ri chiqarish.
Omad! βοΈπ€
LeetCode
Build an Array With Stack Operations - LeetCode
Can you solve this real interview question? Build an Array With Stack Operations - You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
π3
π¬π§ 1441. Build an Array With Stack Operations
π‘ Medium: 1441. Build an Array With Stack Operations
π¨βπ» My solution: Java Simple 0ms | 100 Faster Solution
Hi, guys! Let's check the solution for the problem above.
π¨βπ» Task
Our task is to return an array which would show push() and pop() method calls in correct order depending on given array.
π¨βπ» Solution
1. I create new ArrayList.
2. I create
3. Inside for loop, which starts from 1, because given stream starts from 1, I check if
4. I add "Push" to the list, since all number including
5. Then I check if
- Why?
- Because I keep applying pop() until the number from stream -
- Why I decreased
- Because I keep the index of current number from array which I should reach by going thru the stream.
6. Finally, I return the list.
This is it. If you have different solution, please, don't hesitate to share in the comments below.
Peace! βοΈπ€
@leetcodin
π‘ Medium: 1441. Build an Array With Stack Operations
π¨βπ» My solution: Java Simple 0ms | 100 Faster Solution
Hi, guys! Let's check the solution for the problem above.
π¨βπ» Task
Our task is to return an array which would show push() and pop() method calls in correct order depending on given array.
π¨βπ» Solution
1. I create new ArrayList.
2. I create
count variable for index.3. Inside for loop, which starts from 1, because given stream starts from 1, I check if
count equals to the length. If yes, then I stop the loop.4. I add "Push" to the list, since all number including
n should be pushed to the stack.5. Then I check if
target[count] equals to the pushed number. If no, then I add "Pop" to the list and decrease count by 1.- Why?
- Because I keep applying pop() until the number from stream -
i is equal to the number from target.- Why I decreased
count by 1?- Because I keep the index of current number from array which I should reach by going thru the stream.
6. Finally, I return the list.
This is it. If you have different solution, please, don't hesitate to share in the comments below.
Peace! βοΈπ€
@leetcodin
LeetCode
Build an Array With Stack Operations - LeetCode
Can you solve this real interview question? Build an Array With Stack Operations - You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
π4
πΊπΏ 1441. Build an Array With Stack Operations
π‘ O'rta daraja: 1441. Build an Array With Stack Operations
π¨βπ» Mening yechimim: Java Simple 0ms | 100 Faster Solution
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz bu Stack-ning metodlarini chaqirish navbatini berilgan massiv uchun to'g'ri chiqarish.
π¨βπ» Yechim
1. ArrayList yaratamiz.
2.
3. for loop ishlatamiz. Sonlar ketma-ketligi 1dan boshlangani uchun, loop ham 1 dan boshlanadi. Loop-ning ichida birinchi o'rinda
4. Keying har bir iteratsiya uchun "Push"-ni listga qo'shaman, chunki ketma-ketlikdagi sonlarning hammasi stack-ga qo'shilishi lozim.
5. Keyin esa
- Nimaga?
- Chunki stack-ning pop() metodi
- Nimaga
- Bu massivdagi sonning indeksini saqlash uchun kerak.
6. Oxirida esa [loop to'xtaganida] list-ni qaytaramiz.
Bugunga shu. Agar siz boshqa usulda yechgan bo'lsangiz, comments-da yozing.
Omad! βοΈπ€
@leetcodin
π‘ O'rta daraja: 1441. Build an Array With Stack Operations
π¨βπ» Mening yechimim: Java Simple 0ms | 100 Faster Solution
Hammaga salom! Kechagi masalaning yechimini ko'rib chiqamiz.
π¨βπ» Vazifa
Bizning vazifamiz bu Stack-ning metodlarini chaqirish navbatini berilgan massiv uchun to'g'ri chiqarish.
π¨βπ» Yechim
1. ArrayList yaratamiz.
2.
count o'zgaruvchisini yaratamiz. Massivdagi indeksni o'zida saqlab turadi.3. for loop ishlatamiz. Sonlar ketma-ketligi 1dan boshlangani uchun, loop ham 1 dan boshlanadi. Loop-ning ichida birinchi o'rinda
count target massivining uzunligiga tengligini tekshiraman. Agar teng bo'lsa, loop-ni to'xtatamiz.4. Keying har bir iteratsiya uchun "Push"-ni listga qo'shaman, chunki ketma-ketlikdagi sonlarning hammasi stack-ga qo'shilishi lozim.
5. Keyin esa
target[count] stack-ga oxirgi qo'shilgan songa tengligini tekshiraman. Agar teng bo'lmasa, "Pop" so'zini list-ga qo'shaman va count-ning qiymatini 1ga kamaytiraman.- Nimaga?
- Chunki stack-ning pop() metodi
target massivining hozirgi soni ketma-ketlikdagi songa teng bo'lmagunicha ishlatiladi.- Nimaga
count-ning qiymati 1ga kamayadi?- Bu massivdagi sonning indeksini saqlash uchun kerak.
6. Oxirida esa [loop to'xtaganida] list-ni qaytaramiz.
Bugunga shu. Agar siz boshqa usulda yechgan bo'lsangiz, comments-da yozing.
Omad! βοΈπ€
@leetcodin
LeetCode
Build an Array With Stack Operations - LeetCode
Can you solve this real interview question? Build an Array With Stack Operations - You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
You have an empty stack with the two following operations:
* "Push": pushes an integer to the top of the stack.
β¦
π4
π¬π§ 129. Sum Root to Leaf Numbers
Hi, guys! Let's have some practice with trees.
π‘ Medium: 129. Sum Root to Leaf Numbers
π¨βπ» My solution: Java | 0ms Solution | DFS
If you are struggling with this sort of problems, then check my previous posts. For example, this one about DFS.
Good luck! βοΈπ€
Hi, guys! Let's have some practice with trees.
π‘ Medium: 129. Sum Root to Leaf Numbers
π¨βπ» My solution: Java | 0ms Solution | DFS
If you are struggling with this sort of problems, then check my previous posts. For example, this one about DFS.
Good luck! βοΈπ€
LeetCode
Sum Root to Leaf Numbers - LeetCode
Can you solve this real interview question? Sum Root to Leaf Numbers - You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
π2
πΊπΏ 129. Sum Root to Leaf Numbers
Hammaga salom! Keling boshqa mavzularga oid amaliy mashqlar bajaramiz.
π‘ O'rta daraja: 129. Sum Root to Leaf Numbers
π¨βπ» Mening yechimim: Java | 0ms Solution | DFS
Agar shu kabi masalalarni yechish qiyin bo'lsa, bundan avval yozgan postlarimni ko'rib chiqishni maslahat beraman. Masalan, bu DFS haqidagi postni.
Omad! βοΈπ€
Hammaga salom! Keling boshqa mavzularga oid amaliy mashqlar bajaramiz.
π‘ O'rta daraja: 129. Sum Root to Leaf Numbers
π¨βπ» Mening yechimim: Java | 0ms Solution | DFS
Agar shu kabi masalalarni yechish qiyin bo'lsa, bundan avval yozgan postlarimni ko'rib chiqishni maslahat beraman. Masalan, bu DFS haqidagi postni.
Omad! βοΈπ€
LeetCode
Sum Root to Leaf Numbers - LeetCode
Can you solve this real interview question? Sum Root to Leaf Numbers - You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
π3π1
π¬π§ 129. Sum Root to Leaf Numbers
π‘ Medium: 129. Sum Root to Leaf Numbers
π¨βπ» My solution: Java | 0ms Solution | DFS
Hi, guys! Let's check the solution for the problem.
π¨βπ» Task
Our task to record root-to-leaf path in a binary tree as a number and find the total sum of such numbers.
π¨βπ» Solution
1. I've declared
2. I initialize
3. Inside
- Base case check when node is null.
- Calling recursive method for left and right child nodes of current node.
Between these statements I do two actions:
- I append the digit which current node holds.
- Then check if current node is a leaf node (does not have child nodes). If it is a leaf, then I parse integer from StringBuilder and add parsed number to
In the end of the method I remove last char [digit] from StringBuilder. This helps me to keep the correct path digits for next nodes which we are going to traverse.
In the end, after recursive method stops its execution, I return the
This is it.
Good luck! βοΈπ€
π‘ Medium: 129. Sum Root to Leaf Numbers
π¨βπ» My solution: Java | 0ms Solution | DFS
Hi, guys! Let's check the solution for the problem.
π¨βπ» Task
Our task to record root-to-leaf path in a binary tree as a number and find the total sum of such numbers.
π¨βπ» Solution
1. I've declared
sum variable for accumulating sum.2. I initialize
sum and call my recursive DFS method to traverse the given tree. Note that I use StringBuilder to record digits throughout the way.3. Inside
traverseBT() I have standard recursive DFS structure for binary tree:- Base case check when node is null.
- Calling recursive method for left and right child nodes of current node.
Between these statements I do two actions:
- I append the digit which current node holds.
- Then check if current node is a leaf node (does not have child nodes). If it is a leaf, then I parse integer from StringBuilder and add parsed number to
sum.In the end of the method I remove last char [digit] from StringBuilder. This helps me to keep the correct path digits for next nodes which we are going to traverse.
In the end, after recursive method stops its execution, I return the
sum. This is it.
Good luck! βοΈπ€
LeetCode
Sum Root to Leaf Numbers - LeetCode
Can you solve this real interview question? Sum Root to Leaf Numbers - You are given the root of a binary tree containing digits from 0 to 9 only.
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
Each root-to-leaf path in the tree represents a number.
* For example, the root-to-leaf path 1 -> 2 -> 3β¦
π1