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
πŸ‡¬πŸ‡§ Theory. Linked LIst. delete().

Hi, guys! Let's check what should we write inside of delete() method of a simple Linked List.

public Node delete(int val) {
Node current = first;
Node previous = first;
while (current.data != val) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}

πŸ‘¨β€πŸ’» Explanation
1. We create two variables previous and current and assign the first node to them. The idea behind deleting is the following:
- Suppose you have three nodes in a list and you need to remove the second node. What you do?
- You just need to take the first node and assign the third one as next node for it. Then the initial second node will no longer belong to the list.
2. Inside the while loop we loop until we find a node with given value.
- If next node is null then return null, since we didn't find a node with data equal to val,
- If next node exists, then we assign current node to previous variable and make current point to the next node.
3.
- When while loop stops we check if the reference of current equals to the reference of first. If they are equal, then we just make the first point to next. It means that the first node's data is equal to val and we remove the initial first node from the list.
- If current is not equal to first then we just assign next of the current as the next for the previous [like I explained in the beginning].

Q: Why we compare references of current and first?
A: Because we assigned reference first to current in the beginning and if current == first is true, that means we didn't go inside of while loop and current still has the same value.

That's it for today :)
Peace! ✌️πŸ₯Ά
πŸ‘3
πŸ‡ΊπŸ‡Ώ Nazariya. Linked List. delete().

Hammaga salom! Keling bugun delete() [qidirilayotgan tugunni o'chirish] metodini oddiy Linked List-da qanday bo'lishini ko'rib chiqamiz.

public Node delete(int val) {
Node current = first;
Node previous = first;
while (current.data != val) {
if (current.next == null) {
return null;
} else {
previous = current;
current = current.next;
}
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}

πŸ‘¨β€πŸ’» Izoh
1. Biz ikkita previous va current o'zgaruvchilarini yaratamiz va ikkalasiga birinchi elementga ko'rsatgichni qiymatini tayinlaymiz. Tugun o'chirish tagidagi mantiq quyidagicha:
- Tasavvur qiling sizda 3ta tugundan iborat list bor va siz o'rtadagi tugunni o'chirmoqchisiz. Buning uchun nima qilish lozim?
- Siz birinchi tugunning keyingi tugunga ko'rsatuvchi ko'rsatgichni o'rtadagi emas oxiridagi tugunga ko'rsatadigan qilasiz.
2. While loop orqali biz list-ni aylanib chiqamiz. Agar berilgan val tugun ichidagi ma'lumotiga teng bo'lsa, to'xtaymiz.
- Agar keyingi tugun null bo'lsa biz null qaytaramiz, chunki biz kerakli tugunni topa olmadik.
- Agar keyingi tugun null bo'lmasa, biz previous o'zgaruvchisiga hozirgi (current) tugunini tayinlaymiz va hozirgi (current) tugunga undan keyingi tugunni tayinlaymiz.
3.
- While loop to'xtaganida biz current va first ko'rsatgichlarini tengligini tekshiramiz. Agar teng bo'lishsa, biz first o'zgaruvchisiga undan keyingi tugunni tayinlaymiz. Bu harakat orqali biz birinchi tugunni o'chiramiz, chunki, uning data qiymati val-ga teng.
- Agar current first-ga teng bo'lmasa biz current-ning keyingi tugunini previous-ning keyingi tuguni sifatida tayinlaymiz [boshida aytganimdek].

S: Nimaga biz current va first ko'rsatgichlarini "==" operatori orqali tekshiramiz?
J: Chunki biz metodning boshida previous va current-larga first ko'rsatgichini tayinladik va current == first true bo'lsa, demak while loop-ning ichiga kirmadik va current hali ham shu qiymatni saqlab turibdi.

Bugunga shu :)
Bunday sovuq havoda LeetCode-da masala yechish qiyin bo'lmayaptimi? πŸ₯ΆπŸ₯ΆπŸ₯Ά
Omad! ✌️πŸ₯Ά
πŸ‘8
πŸ‡¬πŸ‡§ Linked List. Finish.

Hi, guys! I'd like to inform that we are done with simple unidirected Linked List topic! Yaay! Pat yourself on the back :)
Here are the links to all posts related to this topic. Enjoy!

1. Theory. Linked List. Intro.
2. Theory. Linked List. Relationship, Not Position.
3. Theory. Linked List. Time complexity.
4. Theory. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Theory. Linked List. find().
7. Theory. Linked LIst. delete().

Bonus: Link-1 and Link-2.
Let me know if these posts were useful by giving some reactions to the post.

Thanks! πŸ‘¨β€πŸ’»πŸ€œπŸ€›πŸ‘¨β€πŸ’»
πŸ‘4❀1
πŸ‡ΊπŸ‡Ώ Linked List. Finish.

Hammaga salom! Biz bir tomonlama yo'naltirilgan Linked List (ro'yxat) mavzusini tugatdik! Urraaa! O'zingizga shirinlik olib, o'zingizni tabriklab qo'ying :)

Pastda bu mavzu bo'yicha yozgan postlarga havolalarni qoldiraman.

1. Nazariya. Linked List. Intro.
2. Nazariya. Linked List. Pozitsiya (joy) emas, munosabat
3. Nazariya. Linked List. Vaqt murakkabligi [Time complexity].
4. Nazariya. Linked List. Structure.
5. Linked List. insertFirst() and deleteFirst().
6. Nazariya. Linked List. find().
7. Nazariya. Linked LIst. delete().

Bonus: Havola-1 and Havola-2.
Agar shu postlar sizlarga foydali bo'lgan bo'lsa, bu postga reaction-lar qo'ying. Menga bu motivatsiya beradi.

Rahmat! πŸ‘¨β€πŸ’»πŸ€œπŸ€›πŸ‘¨β€πŸ’»
πŸ”₯17πŸ‘3πŸ“2πŸ‘¨β€πŸ’»1
πŸ‡¬πŸ‡§ Practice. 404. Sum of Left Leaves.

Hi, guys! Let's have some practice after theoretical posts. Here is a binary tree problem for you.

🟒 Easy: 404. Sum of Left Leaves
πŸ‘¨β€πŸ’» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution

Let me know how much time does it take to solve this problem.
Good luck! βœŒοΈπŸ€“
πŸ‘1
πŸ‡ΊπŸ‡Ώ Amaliyot. 404. Sum of Left Leaves.

Hammaga salom! Keling amaliyot ham bajaraylik. Bugun sizlarga binar daraxt mavzusiga oid masala tayyorlab qo'ydim.

🟒 Oson: 404. Sum of Left Leaves
πŸ‘¨β€πŸ’» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution

Comment-larda bu masalani yechishga qancha vaqt sarflaganingizni yozing.
Omad! βœŒοΈπŸ€“
πŸ‘3
πŸ‡¬πŸ‡§ Practice. 404. Sum of Left Leaves.

🟒 Easy: 404. Sum of Left Leaves
πŸ‘¨β€πŸ’» My solution: Java 100% Faster | 0ms | 4 Lines | DFS Solution

Hi, guys! Let's see one of the possible solutions for the problem above.

πŸ‘¨β€πŸ’» Task
Your task is to return sum of all left leaves.

πŸ‘¨β€πŸ’» Solution
The solution will be easy for you if you read my post here. This is basic DFS tree problem.

- Since we are using recursive approach I check the base case: if node is null then return 0.
- I create a sum variable and make its value 0.
- Then I check a condition I need. Our condition for a left leaf is when left node is not null and left and right nodes of it are null. If it's true, we just add that to sum.
- Finally, I recursively return sum for each subtree by recursively calling the method for left and right nodes.

This is it :)
Good luck! βœŒοΈπŸ€“
❀1
πŸ‡ΊπŸ‡Ώ Amaliyot. 404. Sum of Left Leaves.

🟒 Oson: 404. Sum of Left Leaves
πŸ‘¨β€πŸ’» Mening yechimim: Java 100% Faster | 0ms | 4 Lines | DFS Solution

Hammaga salom! Keling shu masalaning yechimlaridan bittasini ko'rib chiqaylik.

πŸ‘¨β€πŸ’» Vazifa
Bizning vazifamiz - bu berilgan binar daraxtning chap barglarining jamini topish. Binar daraxtda barg - bu bollari yo'q tugundir.

πŸ‘¨β€πŸ’» Yechim
Yechimni yaxshiroq tushunishingiz uchun eski postimga havola qoldiraman - Binar daraxtni aylanib chiqish.

- Rekursiv usul ishlatayotganimiz uchun asosiy holatni tekshiramiz: if (node == null) return 0;
- Sum o'zgaruvchisini yarataman va qiymatini 0 qilaman.
- Keyin esa bizga kerak bo'lgan shartni tekshiramiz. Tugun barg bo'lishi uchun uning o'zi null-ga teng emasligi va uning bollari null-ga tengligini tekshiramiz. Agar shart true bo'lsa, sum shu bargning qiymatiga teng bo'ladi.
- Oxirida, shu sum va bola daraxtlardan olingan natijalarni qo'shamiz. Shu natijalarni rekursiv chaqiruv orqali olamiz.

Bugunga shu :)
Omad! βœŒοΈπŸ€“
❀4
πŸ‘8
πŸ‡¬πŸ‡§ 1009. Complement of Base 10 Integer

Hi, guys! Let's solve one interesting problem.

🟒 Easy: 1009. Complement of Base 10 Integer
πŸ‘¨β€πŸ’» My solution: Java O(1) | 1 line Solution

One challenge for you: Can you write 1 line solution for this?
Good luck! βœŒοΈπŸ€“
πŸ‘1
πŸ‡ΊπŸ‡Ώ 1009. Complement of Base 10 Integer

Hammaga salom! Keling bugun bitta qiziqarli masala yechamiz.

🟒 Oson: 1009. Complement of Base 10 Integer
πŸ‘¨β€πŸ’» Mening yechimim: Java O(1) | 1 line Solution

Sizlarga challenge: bir qatorli yechim topa olasizmi?
Omad! βœŒοΈπŸ€“
πŸ‘5
πŸ‡¬πŸ‡§ 1009. Complement of Base 10 Integer

🟒
Easy: 1009. Complement of Base 10 Integer
πŸ‘¨β€πŸ’» My solution: Java O(1) | 1 line Solution

Let's check the solution for the problem above.

πŸ‘¨β€πŸ’» Task
Your task is to flip all 0's to 1's and all 1's to 0's of binary representation of given number and return new integer based on this.

πŸ‘¨β€πŸ’» Solution
My idea is to execute XOR operation to the binary representation of given number and binary representation of other number with the same length filled with only 1's.
For example, we are given 5. Its binary representation is "101". I apply XOR to "101" and "111". We get "010" which is 2 in base-10.

The steps are:
Integer.toBinaryString(n).length() - Find binary representation and its length. In the example above it is 3.

(1 << Integer.toBinaryString(n).length())
will look like 1 << 3. By left shift I move 1 three digits to left and will get "1000". In base-10 representation this is 8.

Then I substract 1. 8 - 1 = 7. 7 in binary is "111".
Finally, I apply XOR to n and 7.

So, this is it :)
Good luck! βœŒοΈπŸ€“
πŸ‘1
πŸ‡ΊπŸ‡Ώ 1009. Complement of Base 10 Integer

🟒 Oson: 1009. Complement of Base 10 Integer
πŸ‘¨β€πŸ’» Mening yechimim: Java O(1) | 1 line Solution

Hammaga salom! Keling kechagi masalaning yechimini ko'rib chiqaylik.

πŸ‘¨β€πŸ’» Vazifa
Bizning vazifamiz - bu berilgan sonning binar ko'rinishidagi hamma 0-larni 1-lar bilan va 1-larni 0-lar bilan almashtirib chiqish. Va natijadagi hosil bo'lgan sonni qaytarish.

πŸ‘¨β€πŸ’» Yechim
Yechimning tagidagi g'oya qiyin emas. Men berilgan sonning binar ko'rinishi uzunligini topaman va shu uzunlikdagi faqat 1-lardan iborat binar sonni yarataman. Keyin shu ikkita son ustida XOR operatsiyasini bajaraman.

Masalan, bizga 5 berilgan, "101" esa uning binar ko'rinishi. Shu uzunlikka ega "111" binar sonini yaratamiz va shu ikkita son ustida XOR-ni bajaramiz. Natijada "010" binarli son kelib chiqadi va base-10 ko'rinishida bu 2-ga teng.

Harakatlarimiz quyidagicha:
Integer.toBinaryString(n).length() - berilgan sonning binar ko'rinishini topib, uning uzunligini aniqlaymiz.

(1 << Integer.toBinaryString(n).length())
ifodasi 1 << 3 ko'rinishga ega bo'ladi. Bu operatsiya [left shift] orqali 1-ni uchta bitga chapga suramiz va "1000" hosil bo'ladi. Bu sonning base-10 ko'rinishi 8-ga teng.

Keyin 8-dan 1-ni ayriman. 7-ning binar ko'rinishi "111" bo'ladi.
Oxirida n va 7 sonlari ustida XOR-ni bajaraman.

Shu xolos :)
Omad! βœŒοΈπŸ€“
πŸ‘4
πŸ‡¬πŸ‡§ Solution's screenshot, btw

πŸ‡ΊπŸ‡Ώ Yechimni yuborish esdan chiqibdi
πŸ‘9
πŸ‡¬πŸ‡§ Theory. Stack.

Hi, guys! Since we discussed Linked Lists, I think the logical continuation of the topic should be such Data Structures as Stack and Queue. So, let's start from Stack.

Stack is a data structure which allows access to only one data node - the last node inserted. When you remove [or pop] that item, then you can access next item and etc.

The approach implemented by this DS is called Last-In-First-Out or LIFO. It means that newest element added will be processed first.

We can write our own Stack. We will need two classes 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 Stack, but utilize it in a specific manner. We will discuss this later.
2. Stack class. Stack class contains a link to the first node. If it's null, then we don't have node in our Stack yet. Stack also has:
- constructor(s) to create our Stack with certain state.
- methods: adding push(), removing pop(), displaying last element peek(), and etc.

That's it for today.

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

Good luck! βœŒοΈπŸ€“
πŸ‘6
πŸ‡ΊπŸ‡Ώ 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 bitta, oxirgi kiritilgan, tugun bilan foydalanish imkonini beradigan ma'lumot tuzilmasi. Shu elementni olib tashlaganingizdan [pop()] so'ng, keyingi element bilan foydalana olasiz.

Ushbu MT tomonidan qo'llaniladigan usul Last-In-First-out yoki LIFO deb ataladi. Bu degani MT-ga oxirgi qo'shilgan element navbatda birinchi bo'lib ishlatiladi.

Biz ham o'zimizning sodda Stack MT-mizni yozishimiz mumkin. Bizga ikkita klass kerak bo'ladi:
1. Node klassi. Bu huddi Linked List uchun ishlatgan Node klass kabi tuzilishga ega. O'zi Stack linked list ustida qurilgan desak bo'ladi, lekin buni keyinroq ko'rib chiqamiz.
2. Stack klassi. Stack klassi o'zida oxirgi tugunga ko'rsatuvchi ko'rsatgichni saqlaydi. Agar u null bo'lsa, bizning Stack'imiz bo'sh degani. Stack-da yana:
- ma'lum bir holat bilan Stack-ni yaratuvchi konstruktor;
- qo'shish push(), olib tashlash pop(), oxirgi elementni ko'rsatish peek() kabi metodlar bo'ladi.

Bugunga shu.

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

Hammaga omad! βœŒοΈπŸ€“
πŸ‘12
πŸ‡¬πŸ‡§ Theory. Stack methods.

Hi, guys! Yesterday we discussed what Stack Data Structure is. Let's dig a little big deeper.

push():
- Adds new element to your stack. Here we can use Linked List's insertFirst() method.

pop():
- This method removes last element and returns it. Here we can use Linked List's deleteFirst() method.

peek():
- This method returns last element without deleting it. Here we just return the first element of our Linked List.

You can see in the picture that we have built our Stack based on Linked List. 😁

That's it.

P.S. LeetCode is challenging us with backtracking problems. It's getting tough, honestly.
Have a good day!βœŒοΈπŸ€“
πŸ‘3
πŸ‡ΊπŸ‡Ώ Nazariya. Stack-ning metodlari.

Hammaga salom! Keling kechagi Stack mavzusini sal chuqurroq ko'rib chiqamiz.

push():
- Stack-ga yangi element qo'shamiz. Bu yerda Linked List-ning insertFirst() metodini ishlatsak bo'ladi.

pop():
- Stack-ning oxirgi elementini olib tashlaydi va uni qaytaradi. Bu yerda Linked List-ning deleteFirst() metodi bilan foydalansak bo'ladi.

peek():
- Stack-ning oxirgi elementini qaytaradi (u joyida qolaveradi). Bu yerda esa biz Linked List-ning birinchi elemtini qaytaramiz.

Postning tagida rasmda siz Linked List ustida qurilgan Stack-ni kodini ko'rishingiz mumkin.

Bugunga shu.

P.S. LeetCode backtracking masalalari bilan juda ham qiynayapti.
Yaxshi dam olingizlar! βœŒοΈπŸ€“
πŸ‘4
πŸ‘12
πŸ‡¬πŸ‡§ Stack. Wrap up.

Hi, guys! So, we've looked through Stack topic. Let's do some practice with Stack Data Structure.

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

Actually, there is much more elegant solution than mine. But! Right now it's not that important. Your task is to use Stack to solve the problem.

Good luck! βœŒοΈπŸ€“
πŸ‘4