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
πŸ‡¬πŸ‡§ Personal Achievement

Hi, guys! I just want to share a small personal achievement with you. I was able to solve a hard problem within 1 hour! πŸ™ŠπŸ™ŠπŸ™Š. It is a math problem. Here we go.

πŸ”΄ Hard: 149. Max Points on a Line
πŸ‘¨β€πŸ’» My solution: Java | 98% Memory | Maths | Explained In Details

I'll explain my solution in the next post.

P.S. We will continue Linked List topic after that.
Cheers!βœŒοΈπŸ€“
πŸ†3πŸ‘2
πŸ‡ΊπŸ‡Ώ Shaxsiy yutuq

Hammaga salom! Bugun kichik shaxsiy yutug'im bilan ulashishmoqchiman. Men qiyin darajali masalani 1 soatda yecha oldim! πŸ™ŠπŸ™ŠπŸ™Š Bu matematikadan bilim talab qiladigan misol.

πŸ”΄ Qiyin daraja: 149. Max Points on a Line
πŸ‘¨β€πŸ’» Mening yechimim: Java | 98% Memory | Maths | Explained In Details

Keyingi postda shu yechimni ko'rib chiqamiz.

P.S. Bundan keying esa Linked List mavzusini davom ettiramiz.
Omad!βœŒοΈπŸ€“
πŸ†12πŸ‘4⚑2πŸŽ‰2
πŸ‡¬πŸ‡§ 149. Max Points on a Line

πŸ”΄ Hard: 149. Max Points on a Line
πŸ‘¨β€πŸ’» My solution: Java | 98% Memory | Maths | Explained In Details

Hi, guys! Let's dive into the solution of the above problem.

πŸ‘¨β€πŸ’» Task
You are given points on the X-Y plane. Your task is to find the max number of points that lie on the same straight line.

πŸ‘¨β€πŸ’» Explanation
- We have a formula for a straight line: y = k * x + b.
- I take 2 points to find k and b. Why 2 points? Because we have axiom about 2 points and a straight line.
- There is a formula y - y0 = k/(x - x0). So, I derive k from this formula and get: k = (y - y0)/(x - x0).
By putting coordinates of two points into this formula I can find k and b.
- After finding k and b I go through all other points and check if they are on the same straight line, i.e. I put y3 and x3 into y = k * x + b formula and check if it is correct. That's why time complexity is O(N ^ 3).
- We have corner cases when x1 equals to x2 or y1 equals to y2 which means that a straight line is vertical or horizontal respectively. In such cases I just check if other points have equal x's or y's respectively.
- I have yPotential variable where I use rounding. I round up to 4 zeros after dot because we are given numbers from -10^4 to 10^4.

This is it :)
Let me know if you have questions.
Peace!βœŒοΈπŸ€“
πŸ‘3🐳2
πŸ‡ΊπŸ‡Ώ 149. Max Points on a Line

πŸ”΄ Qiyin daraja: 149. Max Points on a Line
πŸ‘¨β€πŸ’» Mening yechimim: Java | 98% Memory | Maths | Explained In Details

Hammaga salom! Keling yechimni tahlil qilib chiqamiz.

πŸ‘¨β€πŸ’» Vazifa
Bizga tekkislikdagi Dekart koordinatlar sistemasi (TDKS) berilgan. Bizning vazifamiz - berilgan nuqtalar ichidan bitta to'g'ri chiziqqa tegishli bo'lgan nuqtalarning maksimal sonini topish.

πŸ‘¨β€πŸ’» Izoh
- To'g'ri chiziq uchun y = k * x + b formulasi bor.
- Formula uchun k va b topish uchun 2ta nuqta ishlataman. Nimaga? Chunki 2ta nuqta va to'g'ri chiziq haqida aksioma bor. Internetda qidirib ko'ring.πŸ˜‰
- Chiziq uchun yana bitta formula bor: y - y0 = k/(x - x0). Bu formuladan k-ni topamiz: k = (y - y0)/(x - x0). Shu formulaga ikkita nuqtaning koordinatalarini joylashtirsak k-ni topishimiz mumkin. Va k-ni topgandan keyin b = y - k * x orqali b-ni topsak bo'ladi.
- k va b-larni topgandan song, berilgan nuqtalarni [ikkita nuqtadan tashqari] hammasini aylanib chiqaman. Bundan maqsad - shu nuqtalarning y3 va x3 koordinatalarini y = k * x + b formulasiga qo'yib, chap tomondagi natija o'ng tomondagi natijaga tengligini ko'raman. Agar teng bo'lsa, bu nuqta to'g'ri chiziqda joylashgan.
Uchta for loop ishlatganim uchun algoritm tezligi O(N ^ 3) ga teng.
- Bizda edge case-lar bor: x1 teng x2 yoki y1 teng y2. Bu degani to'g'ri chiziq vertikal yoki gorizontal holatda bo'ladi. Shuning uchun boshqa nuqtalarning x va y koordinatalari mos ravishda tengligini tekshiraman.
- Agar ahamiyat bersangiz yPotential o'zgaruvchisiga nisbatan yaxlitlash ishlatilgan. Yaxlitlashning aniqligi nuqtadan keyin 4ta raqamga teng bo'lishining sababi - bu berilgan sonlarning qiymati -10^4 va 10^4 ichida bo'lgani.

Film tugadi :)
Tushunarli bo'ldi degan umiddaman. Savollar bo'lsa comments-da yozing.
Omad!βœŒοΈπŸ€“
πŸ‘9
πŸ‡¬πŸ‡§ Theory. Linked List.

Hi, guys! We covered theory on how linked list works. Let's see some pieces of code to understand how to build our own simple Linked list.

LinkedList class.
- LinkedList class contains a link to the first node. If it's null, then we don't have node in our LinkedList yet.
- constructor to create our linked list with certain state.
- methods to manipulate nodes: adding, removing, finding, and etc.

Node class.
- a node contains value - data which we won't to store and a link to the next node.
- constructors to create a node with certain state
- we may have some extra methods to display or show string representation of a node.

Check the images below.
Peace! βœŒοΈπŸ€“
πŸ‘3
πŸ‡ΊπŸ‡Ώ Nazariya. Linked List.

Hammaga salom! Biz sizlar bilan Linked list qanday ishlashi haqidagi nazariyani ko'rib chiqdik. Keling, o'zimizning oddiy Linked list-imizni qanday yaratishni tushunish uchun ba'zi kod qismlarini ko'rib chiqaylik.

LinkedList klassida shular mavjud:
- LinkedList klassida birinchi tugunga ko'rsatgich saqlanadi. Agar u null bo'lsa, bizning LinkedListimizda hali tugun yo'q degan ma'noni bildiradi.
- ma'lum bir holat bilan linked listimizni yaratish uchun konstruktor.
- tugunlar ustida manipulyatsiya qilish metodlari: qo'shish, o'chirish, topish va boshqalar.

Element (tugun -> Node) klassi shularda iborat:
- tugunda ma'lumot saqlash uchun o'zgaruvchi va keyingi tugunga ko'rsatgich.
- o'zimiz xohlagan holatga ega tugun yaratish uchun konstruktorlar.
- tugunda boshqa metodlar bo'lishi mumkin. Masalan, tugunning string tasvirini ko'rsatish metodi.

Postning ostida bo'lgan rasmlarning ko'ring.
Omad!βœŒοΈπŸ€“
πŸ‘5
πŸ‡¬πŸ‡§ Linked List class example

πŸ‡ΊπŸ‡Ώ Linked List klassining namunasi
πŸ‘5
πŸ‡¬πŸ‡§ Node class example

πŸ‡ΊπŸ‡Ώ Node klassining namunasi
πŸ‘7πŸ”₯1
πŸ‡¬πŸ‡§ Linked List. insertFirst() and deleteFirst().

Hi, guys! Let's see how we write methods above for a simple Linked List in Java.

πŸ“Œ insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);
newFirstNode.next = first;
first = newFirstNode;
}

First you create a new node with you data. Then you assign current first node as a next node to the newly created. Then you assign the reference to the newly created node to first variable. Thus, you made the previous first node to be the second node and new node has become the first.

πŸ“Œ deleteFirst()
public Node deleteFirst() {
Node toBeDeleted = first;
first = first != null ? first.next : null;
return toBeDeleted;
}

In variable toBeDeleted you keep the reference to the first node. Then you assign the next node as the first (if it was not equal to null). Then you just return this node.

This is it. Check the pictures below.

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

Peace! βœŒοΈπŸ€“

πŸ‘‰ @LeetCodin πŸ‘ˆ
πŸ‘3
πŸ‡ΊπŸ‡Ώ Linked List. insertFirst() va deleteFirst().

Hammaga salom! Keling bugun sarlavhada berilgan metodlarni Javada oddiy Linked List uchun ko'rib chiqaylik.

πŸ“Œ insertFirst()
public void insertFirst(int dataForFirst) {
Node newFirstNode = new Node(dataForFirst);
newFirstNode.next = first;
first = newFirstNode;
}

Boshida sizga parametrda berilgan ma'lumot bilan yangi tugun yaratasiz. Keyin esa bu yangi tugunning next ko'rsatgichini birinchi tugunga ko'rsatadigan qilamiz. Bundan so'ng birinchi elementga ko'rsatuvchi first o'zgaruvchimizga yangi elementni tayinlaymiz. Shunday qilsak, eski birinchi element ikkinchi bo'lib qoladi va yangi element esa - birinchi.

πŸ“Œ deleteFirst()
public Node deleteFirst() {
Node toBeDeleted = first;
first = first != null ? first.next : null;
return toBeDeleted;
}

toBeDeleted
o'zgaruvchisi orqali birinchi elementga ko'rsatgich saqlaymiz. Keyin birinchi elementga ko'rsatuvchi o'zgaruvchini keyingisiga ko'rsatadigan qilamiz (agar birinchi null-ga teng bo'lmasa albatta). Bundan keyin saqlab qolgan o'zgaruchini qaytaramiz. Natijada birinchi tugunga ko'rsatuvchi ko'rsatgich ikkinchi tugunga (yoki null-ga) ko'rsatadigan bo'ladi.

Bugun shu. Tushunarliroq bo'lishi uchun rasmlarni qo'shaman.

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

Omad!βœŒοΈπŸ€“

πŸ‘‰ @LeetCodin πŸ‘ˆ
πŸ‘6
πŸ‘8
πŸ‘10
πŸ‡¬πŸ‡§ Practice. 151. Reverse Words in a String.

Hi, guys! Let's have a practice today. Today we have offtop problem. Our task is to reverse the sequence of words in a string.

🟑 Medium: 151. Reverse Words in a String
πŸ‘¨β€πŸ’» My solution: βœ”οΈ Java | 6 Lines | 93% Time | 93% Memory Solution

Wish you good luck with this!
Peace! βœŒοΈπŸ€“
πŸ‘4
πŸ‡ΊπŸ‡Ώ Amaliyot. 151. Reverse Words in a String.

Hammaga salom! Bugun amaliy mashq qilaylik. Bugun offtop masala. Sizning vazifangiz String ichidagi so'zlarni topish va ularni orqadan oldinga qarab chiqarish. E'tibor bering: harflarning emas, so'zlarning tartibini almashtirasiz.

🟑 O'rta daraja: 151. Reverse Words in a String
πŸ‘¨β€πŸ’» Mening yechimim: βœ”οΈ Java | 6 Lines | 93% Time | 93% Memory Solution

Uddalay olasizlar degan umiddaman.
Omad! βœŒοΈπŸ€“
πŸ‘5
πŸ‡¬πŸ‡§ Practice. 151. Reverse Words in a String.

🟑 Medium: 151. Reverse Words in a String
πŸ‘¨β€πŸ’» My solution: βœ”οΈ Java | 6 Lines | 93% Time | 93% Memory Solution

Hi, guys! Let's check the solution for the problem above.

πŸ‘¨β€πŸ’» Task
Our task is to reverse the sequence of words in a string.

πŸ‘¨β€πŸ’» Solution
- Trim the string. Split it by " " [space]. Write down into a list.
- Reverse the list.
- Put each word into a string builder and " ".
- Remove last " " [space] symbol .
- Return the result.

Easy peasy lemon squeezy!
If you have solved it in less number of lines - post you solution down below 😁
Good luck!βœŒοΈπŸ€“
πŸ‘4
πŸ‡ΊπŸ‡Ώ Amaliyot. 151. Reverse Words in a String.

🟑 O'rta daraja: 151. Reverse Words in a String
πŸ‘¨β€πŸ’» Mening yechimim: βœ”οΈ Java | 6 Lines | 93% Time | 93% Memory Solution

Hammaga salom! Keling shu masalaning yechimini ko'rib chiqamiz.

πŸ‘¨β€πŸ’» Vazifa
Sizning vazifangiz String ichidagi so'zlarni topish va ularni orqadan oldinga qarab chiqarish. E'tibor bering: harflarning emas, so'zlarning tartibini almashtirasiz.

πŸ‘¨β€πŸ’» Yechim
- String-ni trim() qilamiz. Keying uni String-lar massiviga " " [space] belgisi orqali bo'lib chiqamiz. Bu massivni list-ga yozamiz.
- List-ni oldidan orqaga o'giramiz.
- StringBuilder yaratamiz va har bir so'z va " " [space] belgisini uning ichiga yozamiz.
- Oxirgi " " [space] belgisini olib tashlaymiz.
- Natijada hosil bo'lgan String-ni qaytaramiz.

Aslida ancha effective-roq va kamroq kod ishlatib yechsa bo'ladi.
Qani, siz ham yechimingiz bilan ulashing 😁
Omad! βœŒοΈπŸ€“
πŸ‘4🌚2
πŸ‘3🌚3
πŸ‡¬πŸ‡§ Theory. Linked List. find()

Hi, guys! Let's see what we need to have inside find() method of a simple Linked List.

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

1. We assign a link (current) to the first node to a temporary variable, since we don't want to lose the first node.
2. Then we loop through the list via while loop until we find a node with given value.
3. Inside the loop we
- check if current node has next node. If it does not, then we return null because we could not find a node with given key;
- if current node has next node, then assign it to the temporary variable and continue going through the loop.
4. Return a node assigned to current variable. If we can come to this line it means that we found a node with given key because the method not finished by returning null and we have gone out of while loop since it's check case does not meet.

This is it :)

P.S. Could you solve today's daily challenge on LeetCode? 😁

Peace! βœŒοΈπŸ€“
πŸ‘5
πŸ‡ΊπŸ‡Ώ Nazariya. Linked List. find()

Hammaga salom! Keling bugun find() [qidirish] metodini oddiy Linked List-da qanday bo'lishini ko'rib chiqamiz.

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

1. Biz alohida current o'zgaruvchisiga birinchi tugunga bo'lgan ko'rsatgichni yozib qo'yamiz, chunki loop qilish davomida first ishlatsangiz, birinchi elementning ko'rsatgichini topolmay qolasiz.
2. Keyin while loop ishlatib list bo'ylab aylanib chiqishni boshlaymiz. While-ning tekshirish holati hozirgi elementning ichidagi ma'lumotning qiymati qidirilayotgan qiymatga teng bo'lmagunicha ishlaydi.
3. While loop-ning ichida
- hozirgi element oxirgiligini tekshiramiz. Agar keyingi element null bo'lsa, unda while-ni tugashini kutmasdan null qaytaramiz. Chunki list-ning oxiriga yetib, qidirilayotgan ma'lumotni topa olmadik.
- agar keyingi element mavjud bo'lsa, u holatda vaqtinchalik ishlatilayotgan current o'zgaruvchisiga keyingi elementni tayinlaymiz.
4. Oxirida, while loop to'xtashidan so'ng, current-ni qaytaramiz. Chunki while-ning tekshiruv shartiga shu element to'g'ri kelmadi. Bu degani u saqlovchi ma'lumot qidirilayotgan qiymatga teng.

Bugunga shu :)

P.S. Bugungi LeetCode masalasini yecha oldingizmi? 😁

Omad! βœŒοΈπŸ€“
πŸ‘15