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
πŸ‡ΊπŸ‡Ώ Amaliyot. Stack.

Hammaga salom! Biz sizlar bilan Stack ma'lumotlar tuzilmasini [MT] ko'rib chiqdik. Keling endi shu MT bilan amaliy mashqlar bajaramiz.

🟒 Oson: 20. Valid Parentheses
πŸ‘¨β€πŸ’» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)

Aslida menikidan ancha chiroyliroq yechim bor. Lekin! Chiroylimi - yo'qmi, sizlar berilgan masalani Stack ishlatib yechishingiz lozim.

Omad! βœŒοΈπŸ€“
πŸ‘8
πŸ‡¬πŸ‡§ 20. Valid Parentheses.

🟒 Easy: 20. Valid Parentheses
πŸ‘¨β€πŸ’» My solution: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)

Hi, guys. Today we will see the solution of phoenix13steve user on LeetCode. Link to this solution is here: Short java solution

1. First he creates a Stack.
2. Then he goes through the String letter by letter.
3. For each char he check if it is '(' or '{', or '['.
- If yes, then he pushes closing parentheses to the relevant case.
- If no, then he checks the condition where Stack is empty or the last element of the Stack is not equal current char. If yes, then we return false.

Let's have a closer look to the logic. Here if current char is not opening parentheses, then we must have closing one for the latest until this moment.

4. Finally, if the Stack is empty, then we return true. It means that all opening brackets had closing in the right order.

Peace! βœŒοΈπŸ€“
πŸ‘3
πŸ‡ΊπŸ‡Ώ 20. Valid Parentheses.

🟒 Oson: 20. Valid Parentheses
πŸ‘¨β€πŸ’» Mening yechimim: Java 2 Solutions | Stack | Arraylist in just 1 ms (99.71% faster)

Hammaga salom! Bugun sizlar bilan phoenix13steve nickname-ga ega LeetCoder-ning yechimini ko'ramiz. Yechimga havola: Short java solution

1. Boshida Stack yaratadi.
2. Keying foreach loop orqali String-ni aylanib chiqadi.
3. Har bir char-ni '(' yoki '{' yoki '[' tengligini tekshiradi.
- Agar shulardan bittasiga teng bo'lsa, Stack-ga shu qavsning mos yopadigan qavsini push() qiladi.
- Agar teng bo'lmasa, u Stack bo'sh yoki Stack-ning oxirgi elementi hozirgi char-ga teng emasligini tekshiradi. Agar shu shart true bo'lsa, metod false qaytaradi.

Keling shu harakatning tagidagi mantiqni ko'raylik. Agar loop-dagi hozirgi char ochuvchi qavs bo'lmasa, unda hozirgi char oxirgi uchragan ochuvchi qavsning yopuvchi jufti bo'lishi shart.

4. Oxirida esa Stack-ning bo'shiligi tekshiriladi. Agar u bo'sh bo'lsa, bu hamma ochuvchi qavslar o'z yopuvchi qavslariga ega va ular to'g'ri ketma-ketlikda joylashganligini bildiradi.

Hammaga omad! βœŒοΈπŸ€“
πŸ‘7
πŸ‘13
πŸ‡¬πŸ‡§ 921. Minimum Add to Make Parentheses Valid

Hi, guys! Another day - another Stack challenge :). Let's do it.

🀨 Medium: 921. Minimum Add to Make Parentheses Valid
πŸ‘¨β€πŸ’» My solution: Java | Stack | O(N) Solution

Let's see how fast can you solve it! Share you results below.
Good luck! βœŒοΈπŸ€“
πŸ‘3
πŸ‡ΊπŸ‡Ώ 921. Minimum Add to Make Parentheses Valid

Hammaga salom! Bugun ham amaliy mashq bajaramiz. Bugun yana o'sha Stack mavzusi.

[
LeetCode-chilar: Siz o'shamu?
Stack: Ha, men o'sha.
] 😁

🀨 Medium: 921. Minimum Add to Make Parentheses Valid
πŸ‘¨β€πŸ’» Mening yechimim: Java | Stack | O(N) Solution

Qani ko'raylikchi kim qancha tez yecha olgan ekan! Natijalarni comments-da qoldiring.
Omad! βœŒοΈπŸ€“
πŸ‘6
πŸ‡¬πŸ‡§ 100 000 views of my solutions on LeetCode. Yay!πŸ₯‚

πŸ‡ΊπŸ‡Ώ 100 000 - LeetCode-chilar 100 ming marta yechimlarimni ko'rishgan ekan! Oldik!πŸ₯‚
πŸ”₯25πŸ‘5πŸŽ‰2
πŸ‡¬πŸ‡§ 921. Minimum Add to Make Parentheses Valid

🀨
Medium: 921. Minimum Add to Make Parentheses Valid
πŸ‘¨β€πŸ’» My solution: Java | Stack | O(N) Solution

Hi, guys! Let's check one of possible solutions using Stack Data Structure.

πŸ‘¨β€πŸ’» Task
Our task is to compose valid bracket string by making minimum number of insertions and return this minimum number.

πŸ’‘ Idea
The idea behind the solution is to keep invalid brackets and return their it. Thus, we will know how many insertions we have to do in order to make the string valid.

πŸ‘¨β€πŸ’» Solution
My solution is pretty short.
1. First, I create Stack.

2. Then I iterate over String char by char.
I check if the stack is empty. If it is, then I just push the char.
If it has some values then I check whether the last char is opening bracket and current char is closing bracket.
- In this case I remove the last char from the stack.
- Otherwise, I add current char to the stack since it "incorrectly placed".

3. When loop finishes I will have Stack with chars which make given String invalid. So, I just return the size of the Stack.

This is it :)
Good luck! βœŒοΈπŸ€“
πŸ‘2
πŸ‡ΊπŸ‡Ώ 921. Minimum Add to Make Parentheses Valid

🀨 Medium: 921. Minimum Add to Make Parentheses Valid
πŸ‘¨β€πŸ’» Mening yechimim: Java | Stack | O(N) Solution

Hammaga salom! Keling shu masalaning Stack ishlatgan holda yechish mumkin bo'lgan usullaridan bittasini ko'rib chiqamiz.

πŸ‘¨β€πŸ’» Vazifa
Bizning vazifamiz - bu berilgan (faqat qavslardan iborat) String-ni minimal o'zgarishlar orqali to'g'ri String qilish va shu o'zgarishlar sonini qaytarish.

πŸ’‘ G'oya
Mening yechimimning tagidagi g'oya - bu "noto'g'ri" qavslarni saqlash va ularning sonini qaytarish. Bu orqali String-ni to'g'ri qilish uchun biz nechta o'zgarishlar kiritishimiz kerakligini bilib olamiz.

πŸ‘¨β€πŸ’» Yechim
Mening yechimim qisqa chiqdi.
1. Boshida Stack yarataman.

2. Keying esa String-ni har bir harfini [char] foreach loop orqali ko'rib chiqaman. Loop-ni ichida:
Stack bo'shligini tekshiraman. Agar bo'sh bo'lsa unda char-ni stack-ga qo'shaman. Agar bo'sha bo'lmasa, men stack-dagi oxirgi char ochuvchi qavs va hozirgi char yopuvchi qavsligini tekshiraman:
- Agar shu shart true bo'lsa, stack-ning oxirgi elementini olib tashlayman, chunki bu to'g'ri holat.
- Agar shu shart false bo'lsa, stack-ga hozirgi char-ni qo'shaman, chunki bu joylashishi noto'g'ri qavsdir.

3. Loop tugaganida menda noto'g'ri joylashgan qavslardan iborat Stack hosil bo'ladi va men faqat Stack-ning hajmini qaytaraman.

Bugunga shu :)
Omad! βœŒοΈπŸ€“
πŸ‘4
πŸ‘5
πŸ‡¬πŸ‡§ Theory. Queue.

Hi, guys! After covering Stack Data Structure it's time to move on to the next one - Queue. Let's see what DS Queue is.

Basically saying, Queue is a data structure which allows to access only one data node - the first inserted node. When you remove [or poll] that item, then you can access the second added item and etc.

The approach implemented by this DS is called First-In-First-Out or FIFO. It means that oldest element added will be processed first. Imagine people standing in a queue: first come - first served. Or another example: when you come up to any shelve in Korzinka, you will always see the oldest products [and if you put your hand deeper you can find the fresher oneπŸ˜‚]

We can write our own Queue. The classes that we will need for this:
1. Node class. This is the same Node class that we utilized for Linked List - link. Basically, we build a linked list for Queue, but utilize it in a specific manner.

2. Queue class. Queue class contains a link to the first and the last nodes. If the first is null, then we don't have node in our Queue yet. Queue also has:
- constructor(s) to create our Queue with a certain state.
- methods: adding offer() or push() [depends on language], removing poll(), displaying the first element peek(), and etc.

3. There is an interesting thing: we can build a specific Linked List for our Queue. Let's call this list - FirstLast List. This name reflects the behavior of the list, i.e. we will need to keep two pointers first and last. first will be used for retrieving or removing the elements from Queue and last will be used for insertion.

That's it for today. We will review the possible implementation in the next posts. Tune up!

Sources:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.

Good luck! βœŒοΈπŸ€“
πŸ‘1😁1
πŸ‡ΊπŸ‡Ώ Nazariya. Queue.

Hammaga salom! Stack ma'lumotlar tuzilmasidan [MT] keyin Queue-ni o'tishimiz shart. Keling shu postda Queue-ni ko'rib chiqamiz.

Queue [navbat] - bu faqat bitta, birinchi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [poll()] so'ng, keyingi [ikkinchi] element bilan foydalana olasiz.

Ushbu MT tomonidan qo'llaniladigan usul First-In-First-Out yoki FIFO deb ataladi. Bu degani MT-ga birinchi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.
- Tasavvur qiling odamlar navbatda turibdilar: birinchi kelganga - birinchi navbatda xizmat ko'rsatiladi. Tanish-bilish bu ma'lumotlar tuzilmasida ishlatilmaydi! Qo'yaverasiz!
- Yana bitta misol: Korzinkada qaymoq olaman desangiz, birinchi o'rinda oldingizda eskiroq [kelgan] mahsulot turadi. Agar erinmasdan qo'lingizni sal ichkariroq cho'zsangiz - yangirog'ini topasiz.πŸ˜‚

Biz ham o'zimizning sodda Queue MT-mizni yozishimiz mumkin. Bizga quyidagi klasslar kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Queue linked list ustida qurilgan desak bo'ladi.

2. Queue klassi. Queue klassi o'zida birinchi va oxirgi tugunlarga ko'rsatuvchi ko'rsatgichlarni saqlaydi. Agar birinchisi null bo'lsa, bizning Queue-miz bo'sh degani. Queue-da yana:
- ma'lum bir holat bilan Queue-ni yaratuvchi konstruktor;
- qo'shish offer() yoki push() [tilga qarab], olib tashlash poll(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.

3. Bunday qilsak ham bo'ladi: Queue-miz uchun maxsus bir Linked List yozishimiz mumkin. Uning nomini FirstLast List qilamiz. Chunki bu nom uning behavior-ni yaxshi aks ettiradi, boshqa so'z bilan aytganda u ikkita first va last ko'rsatgichlarini saqlaydi. first-ni birinchi elementni tekshirish va olib tashlash uchun ishlatsak, last-ni esa yangi elementni qo'shish uchun ishlatamiz.

Bugunga shu. Bu MT-ning implementatsiyasini keyingi postlarda ko'rib chiqamiz.

P.S. Queue-ni o'qiyman deb qiynalmadingizlarmi?πŸ˜„ Qanday qilib o'qiganingizni comments-da yozing.πŸ˜‚πŸ˜‚πŸ˜‚
[kuye uye] deb o'qigan bo'lsangiz - sizga 5 baho!

Manbalar:
1. Data Structures and Algorithms in Java. R. Lafore.
2. Internet.

Hammaga omad! βœŒοΈπŸ€“
πŸ‘5😁1🀣1
πŸ‡¬πŸ‡§ Queue methods and implementation.

Hi, guys! Let's see how can we build our own Queue.

As we discussed yesterday, we can build it using three classes: Node, FirstLastList, and Queue itself. Since we already saw the code for Node, I'll review only two latter. Today we will see FirstLastList.

FirstLastList:
1. It will contain two pointers first and last.

2. In constructor I just assign null to them.

3. We will need isEmpty() method, which returns boolean value. If first is null, then the list is empty. Make a pause here and think about it.

4. Method insertLast(int val). We need this method to insert a value to the end of the list. Inside of this method we:
- First create a new Node with given value.
- Then we check if the list is empty [see point 3]. If it is, then we assign new node to the first variable. Why? Because it is the first element in the queue. We need it for removing elements from queue. If it is not empty, then we assign new node to the next pointer of last node.
- In the end of the method we just assign the new node to last variable. Thus we keep a link the the last node of a queue.

5. Method deleteFirst(). We remove the first element from the list.
- First we save data of the first node to a temporary variable.
- Then we check if we have an element after the first. If no, the we assign null to last. Thus we remove an element if there is only one inside of the queue.
- Then we assign next pointer's value of first to first itself. It means that now first points to the second element in the queue.
- Finally we return temporary value to show which value has been deleted.

Good! This is it for today.
Peace! βœŒοΈπŸ€“
πŸ‡ΊπŸ‡Ώ Queue metodlari va implementatsiyasi.

Hammaga salom! Bugun Queue mavzusini davom etamiz.

Kecha aytib o'tganimdek, Queue-ni uchta klass orqali tuzamiz. Bular Node, FirstLastList va Queue klasslari. Node klassini bungacha ko'rib chiqqanmiz, shuning uchun faqat qolgan ikkitasini implementatsiyasini ko'ramiz. Keling bugun FirstLastList-da nimalar bo'lishini yozaman.

FirstLastList:
1. Ikkita first va last Node type-dagi ko'rsatgichlar bo'ladi.

2. Parametrsiz konstruktor ham bo'ladi. Uning ichida tepadagi ikkita ko'rsatgichlarning qiymatini null qilamiz.

3. Bizga yana isEmpty() [bo'sh] boolean qiymat qaytaruvchi metod kerak bo'ladi. Agar first null-ga teng bo'lsa, [queue-ning tagida bo'lgan] list bo'shligini anglatadi. Bu yerda sal to'xtab, shu oxirgi gapni yana bir marta o'qib, nimaga bunday bo'lishini o'ylab ko'ring.

4. insertLast(int val). Bu metod orqali biz list-ning oxiriga yangi elementni qo'shib qo'yamiz. Uning ichida:
- Yangi berilgan val qiymatga ega Node yaratamiz.
- Keying biz list-imiz bo'shligini tekshiramiz [punkt 3]. Agar bo'sh bo'lsa biz yangi elementni unga yozib qo'yamiz. Nimaga? Chunki queue bo'sh va bu u uchun birinchi element. Agar queue bo'sh bo'lmasa, biz oxirgi elementning keyingi elementga ko'rsatuvchi ko'rsatgichiga yangi Node-ni yozib qo'yamiz.
- Metodni yakunida yangi Node-ni last ko'rsatgichiga yozamiz.

5. deleteFirst(). Queue-dan birinchi elementni olib tashlash.
- Metodning boshida birinchi elementning ichidagi qiymatni yozib olish uchun vaqtincha temp o'zgaruvchisini yaratamiz.
- Keyin first [birinchi] elementdan keyin yana element borligini tekshiramiz. Agar bo'lmasa, last [oxirgi] ko'rsatgichimizga null yozib qo'yamiz. Buning sababi - queue-da bitta element bo'lsa, uni olib tashlaymiz. Bu degani oxirgi [last] element null bo'lishi kerak.
- Keyin biz first-ning next ko'rsatgichini first-ning o'ziga yozib qo'yamiz. Bu orqali biz birinchi elementni olib tashlab, ikkinchi elementni navbatni [queue-ni] boshiga joylashtiramiz.
- Metodni oxirida biz olib tashlangan elementning ichidagi saqlab qo'yilgan qiymatini qaytaramiz.

Yaxshi! Bugun ham yangi narsa o'rgandik.
Omad! βœŒοΈπŸ€“
πŸ‘3
πŸ‘7
πŸ‡¬πŸ‡§ Queue methods and implementation. Part 2.

Hi, guys! Let's see what fields and methods Queue class will consist of.

Queue:
1. A pointer to FirstLastList class.
2. Constructor which will create new FirstLastList object.
3. isEmpty() method which will return boolean value.
4. push() method which adds new element to our queue [to the end of it].
5. poll() or remove method which removes first added element.
6. peek() method which shows first element's value. Here I added a method into FirstLastList class which returns data inside the first node of the list. Then I called this method inside of peek().

This should be it. Hope you enjoyed it and everything was pretty clear.
Good luck! βœŒοΈπŸ€“
πŸ‡ΊπŸ‡Ώ Queue metodlari va implementatsiyasi. 2-qism.

Hammaga salom! Keling endi Queue klassi nimalardan iborat bo'lishini ko'rib chiqamiz.

Queue:
1. FirstLastList klassiga ko'rsatgich.
2. Queue konstruktorinig ichida yangi FirstLastList type-dagi object yaratamiz.
3. isEmpty() boolean qiymat qaytaradi. U Queue-bo'shligini bildiradi.
4. push() metodi navbatimizga [oxiriga] yangi elementni qo'shib qo'yadi.
5. poll() metodi birinchi elementni olib tashlaydi.
6. peek() birinchi elementning ichidagi ma'lumotning qiymatini qaytaradi. Buning uchun FirstLastList klassida yangi metod yaratdim. Bu metod birinchi element ichidagi qiymatni qaytaradi. Keyin bu metodni peek() ichida ishlatdim.

Ana shu xolos. Umid qilamanki sizlarga bu tarzda ma'lumotlar tuzilmalarini ko'rib chiqishimiz yoqyapti va hammasi tushunarli.
Omad! βœŒοΈπŸ€“
πŸ‘2πŸ“1
πŸ‘3🐳1