LeetCodin
1.4K subscribers
160 photos
3 videos
2 files
303 links
LeetCode 1700+
πŸ‡ΊπŸ‡ΏπŸ‡ΊπŸ‡Έ
Motto: ПобСда Π»ΡŽΠ±ΠΈΡ‚ ΠΏΠΎΠ΄Π³ΠΎΡ‚ΠΎΠ²ΠΊΡƒ

- Getting ready for FAANG | FAANGga tayyorlanamiz
- Data Structures and Algorithms | Ma'lumotlar tuzilmalari va Algoritmlar
- SWE at Deloitte πŸ‡ΊπŸ‡Έ
- LeetCode: https://leetcode.com/tbekpro/
Download Telegram
πŸ‡¬πŸ‡§ 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! βœŒοΈπŸ€“
πŸ‘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! βœŒοΈπŸ€“
πŸ‘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. 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! βœŒοΈπŸ€“
πŸ‘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. 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! βœŒοΈπŸ€“
πŸ‘8
πŸ‘7
πŸ‡¬πŸ‡§ 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! βœŒοΈπŸ€“
πŸ‘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! βœŒοΈπŸ€“
πŸ‘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
πŸ‘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 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
πŸ‘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 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
πŸ‘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! βœŒοΈπŸ€“
πŸ‘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! βœŒοΈπŸ€“
πŸ‘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 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
πŸ‘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. 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
πŸ‘4
πŸ‘5πŸ‘1
πŸ‡¬πŸ‡§ A little bit tired today, so I'll just leave this here :)

πŸ‡ΊπŸ‡Ώ Bugun ozgina charchadim va shuning uchun bu meme-ni shu yerda qoldiraman :)
πŸ‘13😁3🀨1πŸ‘€1
πŸ‡¬πŸ‡§ 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! βœŒοΈπŸ€“
πŸ‘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! βœŒοΈπŸ€“
πŸ‘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 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! βœŒοΈπŸ€“
πŸ‘1