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
πŸ‡ΊπŸ‡Ώ equals() va hashCode() metodlarini override qilish

Hammaga salom! Bugun masalalar bo'lmaydi.
Men equals() va hashCode() metodlarini override qanday va nima uchun qilish kerakligi haqida tushuntiruvchi video tayyorlab qo'ydim. Video uzunligi 6.5 daqiqa.

Havola: Overriding equals and hashCode in Java - 039

Dam olish kuningiz yaxshi o'tsin!
πŸ‘5
πŸ‡¬πŸ‡§ Practice. Hashtable.

Hi, guys! Let's solve medium level problem today. You can use Hashtable implementation in your language.

🟑 Medium: 2225. Find Players With Zero or One Losses
πŸ‘¨β€πŸ’» My solution:
Java Solution using HashMap and Set

P.S. Unfortunately, I could not visit GDG event😒😒😒
Share if you liked it and which part of it you enjoyed the most.
πŸ‘2
πŸ‡ΊπŸ‡Ώ Amaliyot. Hashtable.

Hammaga salom! Bugun o'rta darajali vazifa tayyorlab qo'ydim. Shu masalani yechish uchun siz ishlatadigan dasturlash tilidagi Hashtable-ning implementation-ni bilan foydalanishingiz mumkin.

🟑 O'rta daraja: 2225. Find Players With Zero or One Losses
πŸ‘¨β€πŸ’» Mening yechimim:
Java Solution using HashMap and Set

P.S. Afsuski, GDG tashkil qilgan event-ga bora olmadim😒😒😒.
Qanday o'tgani va siz eng ko'p yoqqan tarafi bilan comments-da bo'lishing.
πŸ‘4
πŸ‡ΊπŸ‡Ώ Nazariya. Hashtable.

Hammaga salom! Bugungi post "Nimaga equals() va hashCode() metodlarining HashMap-da muhim?" degan savolga javob berish uchun yozilgan.

Esingizda bo'lsa men put() va get() metodlari haqida bu post-da yozgan edim.

put():
πŸ‘‰ Bizga hashCode() metodi ma'lumotimizni qaysi bucket-ga (yoki savatga) joylashtirishimiz kerakligini bilishda ishlatiladi. Savatni topganimizda Hashmap shu savatda teng hash code-li kalit borligini tekshiradi. Agar savatda kalitlarining hash code-lari teng elementlar bo'lsa, equals() metodi orqali shu elementlarning kalitlari tengligini tekshiradi. Agar equals() true qaytarsa, biz yangi elementni eski elementni o'rniga joylashtiramiz. Agar equals() false qaytarsa, biz yangi elementni savatga biriktirilgan Linked list-ga qo'shib qo'yamiz.

get():
πŸ‘‰ hashCode() bilan foydalangan holda savatni topamiz;
πŸ‘‰ savatga biriktirilgan elementlarni hashCode() va equals() metodlarini ishlatgan holda aylanib chiqamiz. Agar kalitlarning hashCode() qiymatlari teng va equals() natijasi true bo'lsa - biz elementni topdik. Agar bu ikkita shart bajarilmasa, biz elementni topa olmadik va null qaytariladi.

Shunday qilib, equals() va hashCode() metodlarini Hashmap-da nima uchun kerakligini oddiy tilda tushuntirishga harakat qildim.

O'qiganingiz uchun rahmatπŸ˜ƒ
πŸ‘1
πŸ‡¬πŸ‡§ Theory. Hashtable.

Hi, guys! It is time to answer the question: why equals() and hashCode() methods are so important for HashMap in Java?

put():
πŸ‘‰ Remember that we use put() and get() methods? I explained how HashMap works in simple words here. We need hashCode() applied to the key to find into which bucket we should put the data. When we find the bucket it checks if there is already a key in the bucket with the same hash code. If we have an element with the same hash code of the key, then equals() method is used for new key and stored key in the bucket. If equals() is true, then we overwrite the element, else we add new element into linked list under this bucket.

For get() method the same logic is applied:
πŸ‘‰ by using hashCode() we find the bucket;
πŸ‘‰ we go through elements in the bucket and use hashCode() and equals() to find our value. Thus, if hashCode() values are equals and equals() returns true, then it means that we have found our entry, else return null.

So, basically there are the reasons why equals() and hashCode() methods are important.

Thanks for readingπŸ˜ƒ
πŸ‘4
πŸ‡¬πŸ‡§ 383. Ransome Note

Hi, guys! Today I'll explain the solution for a problem.

🟒 Easy: 383. Ransom Note
πŸ‘¨β€πŸ’» My solution: Java 2ms | 99% Time | 95% Memory | Explained

πŸ‘¨β€πŸ’» Task:
- Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
- Each letter in magazine can only be used once in ransomNote.

πŸ‘¨β€πŸ’» My solution:

public boolean canConstruct(String r, String m) {
int[] alph = new int[26];
for (int i = 0; i < m.length(); i++) {
alph[m.charAt(i) - 'a']++;
}
for (int i = 0; i < r.length(); i++) {
alph[r.charAt(i) - 'a']--;
}
for (int n : alph) {
if (n < 0) return false;
}
return true;
}

πŸ‘¨β€πŸ’» Explanation:
- I create array of ints with size 26 for all lowercase letters.
- In the first loop I count occurences of letters in the m string by incrementing corresponding int in array. For example, if the letter (char) is 'c', then 'c' - 'a' is equal to 2. So, I increment value with index 2: array[2]++.
- In the second loop I do the same, but decrement for each letter from string r.
- In the third loop I make sure that all values in array are >= 0. If yes, then the answer is true. If not, then the given strings are not valid, i.e. we cannot construct ransomNote from magazine.

Hope you like the format with explanation.
Let me know in the comments belowπŸ€“
πŸ‘3
πŸ‡ΊπŸ‡Ώ 383. Ransome Note

Hammaga salom! Bugun LeetCode-da yechgan masalamni tushuntirib beraman.

🟒 Oson: 383. Ransom Note
πŸ‘¨β€πŸ’» Mening yechimim: Java 2ms | 99% Time | 95% Memory | Explained

πŸ‘¨β€πŸ’» Vazifa:
- Ikkita String berilgan ransomNote va magazine. Agar magazine-ning harflarini ishlatgan holda ransomNote-ni tuzish mumkin bo'lsa - true qaytaramiz. Agar tuzish mumkin bo'lmasa - false qaytaramiz.
- Har bir harf bir marta ishlatilishi mumkin.

πŸ‘¨β€πŸ’» Mening yechimim:

public boolean canConstruct(String r, String m) {
int[] alph = new int[26];
for (int i = 0; i < m.length(); i++) {
alph[m.charAt(i) - 'a']++;
}
for (int i = 0; i < r.length(); i++) {
alph[r.charAt(i) - 'a']--;
}
for (int n : alph) {
if (n < 0) return false;
}
return true;
}

πŸ‘¨β€πŸ’» Yechim izohi:
- Men 26 integer sonidan iborat massiv yarataman. Bu massiv ingliz tilidagi harflarni sanash uchun.
- Birinchi loop ichida magazine String-dan har bir harfning necha marta uchraganini yozib chiqaman. Masalan, agar harf char 'c' bo'lsa, unda 'c'-'a' 2ga teng bo'ladi. Java char o'zgaruvchilari bilan sonlar kabi foydalansa bo'ladi. Shunday qilib 'c' harfning indeksi 2 bo'ladi va alph[2]++ orgali uning sonini increment qilaman.
- Ikkinchi loop ichida huddi tepadagidek operatsiyalar bajariladi, ammo harfning sonini oshirishni o'rniga kamaytiraman.
- Uchinchi loop ichida alph massivning har bir indeksidagi sonni 0 ga teng yoki 0 dan kattaroqligini tekshiraman. Agar bir son manfiy bo'lsa - keyingi sonlarni qaramasdan false qaytaraman, chunki vazifaning shartini bajarmaydi.

Yechimni tushuntirish formatdagi post yoqdi degan umiddaman.
Shu format-dagi postlarni ko'proq qilaymi? Comments-da yozing πŸ€“
πŸ‘9πŸ‘1
πŸ‡¬πŸ‡§ Practice. 61. Rotate List.

Hi, guys! Today's task is interesting as usualπŸ˜„πŸ˜„.

🟑 Medium: 61. Rotate List

You need to rotate list by K places (or nodes).
I'll leave example below in the picture.

I'll provide the solution and explain it in the next post.
Try to find O(N) solution.
Peace✌️
🀩1
πŸ‡ΊπŸ‡Ώ Amaliyot. 61. Rotate List.

Hammaga salom! Bugun ham bitta qiziqarli masala tayyorlab qo'ydimπŸ˜„πŸ˜„.

🟑 O'rta daraja: 61. Rotate List

Vazifa bo'yicha siz list-ni (ro'yxatni) K elementga surishingiz kerak.
Tushunish uchun LeetCode-dan bitta misolni rasm qilib pastda qoldiraman.

Keyingi postda men yozgan yechimni va uning izohini yozishni harakat qilaman.
Ungacha shu masalaga O(N) tezlikda bo'lgan yechim topishga harakat qiling.
Omad✌️
πŸ‘2
Taken from LeetCode
πŸ‘6
πŸ‡¬πŸ‡§ Solution for 61. Rotate List

🟑 Medium: 61. Rotate List

Hi, guys! You can see the link to problem above. And today I'm going to show you my solution and explain it. Let's go!

πŸ‘¨β€πŸ’» My solution:

public ListNode rotateRight(ListNode head, int k) {
//I run thru the given list twice.
//At first run I find the length of it - count variable.
if (head == null head.next == null k == 0) return head;
int count = 0;
ListNode temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
//Then I find remainder move from division count by k.
//Move variable shows how many elements should be rotated.
int move = k % count; //to find where to cut the list
if (k == count || move == 0) return head;
temp = head;
int count1 = 0;
ListNode result = head, prev = head;

//Inside the while loop:
while (count1 < count) {
//if clause below saves the result
//if count is 5 and count1 is 3, then 5 - 3 is 2 and it's equal to
//move. This means head for result is temp or
//node with value 4.
if (count - count1 == move) {
//to make the list finite I made prev.next = null
prev.next = null;
result = temp;
}
count1++;
//here I connect last element of initial list with its head
//this action connect left part with the right part of result
if (count1 == count) {
temp.next = head;
}
//prev is previous node, temp is node
//with index equal to count
prev = temp;
temp = temp.next;
}
return result;
}

Basically, I break list into two parts when I find new head element for result and connect initial end of the list with its initial head.

πŸ‘¨β€πŸ’» My solution: Java Solution in just 1 ms

I hope it was usefulπŸ€œπŸ€›
πŸ‘2
πŸ‡ΊπŸ‡Ώ 61. Rotate List-ning yechimi.

🟑 O'rta daraja: 61. Rotate List

Hammaga salom! Bugun kechagi masalaga men yozgan yechimni tushuntirishga harakat qilaman. Boshladik!

πŸ‘¨β€πŸ’» Mening yechimim:


public ListNode rotateRight(ListNode head, int k) {
//Berilgan list-ni ikki marta aylanib chiqaman.
//Boshida men list-ning uzunligini topaman
if (head == null head.next == null k == 0) return head;
int count = 0;
ListNode temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
//Keying move o'zgaruvchisini topaman.
//U count-ni k-ga bo'lganda qoladigan qoldiqqa tenk.
//Move nechta element surilishi kerakligini ko'rsatadi.
int move = k % count; //to find where to cut the list
if (k == count || move == 0) return head;
temp = head;
int count1 = 0;
ListNode result = head, prev = head;

//While loop-ning ichida:
while (count1 < count) {
//If-ning ichida result-ni saqlayman
//Agar count 5 va count1 3 ga teng bo'lsa
//Unda 5 - 3 = 2 va bu move-ga teng.
//Shu paytda temp qiymati 4 ga teng bo'lgan
//elementda bo'ladi. Bu degani natijaning boshi
//qiymati 4 ga teng bo'lgan elementligini ko'rsatadi
if (count - count1 == move) {
//to make the list finite I made prev.next = null
prev.next = null;
result = temp;
}
count1++;
//Bu yerda boshlang'ich list-ning oxirgi elementini
//Uning bosh elementiga bilan ulayman
//Bu natijadagi list-ning chap va o'ng qismlarini ulaydi.
if (count1 == count) {
temp.next = head;
}
//prev - bu oldingi element va temp - bu hozirgi, indeksi
//count-ga teng bo'lgan element
prev = temp;
temp = temp.next;
}
return result;
}

Oddiy so'z bilan aytganda, men natijadagi list-ning boshini topganimda list-ni ikkita qismga bo'laman va shu qismlarni, tartibini almashtirib, ulayman.

πŸ‘¨β€πŸ’» Mening yechimim: Java Solution in just 1 ms

Foydali bo'ldi degan umiddaman πŸ€œπŸ€›
πŸ‘5
πŸ‡¬πŸ‡§ Practice.

Hi, guys! "Little coding never killed nobody" as Fergie said! πŸ˜πŸ˜πŸ˜‚πŸ˜‚
Today we will practice 2D arrays.

🟑 Medium: 2482. Difference Between Ones and Zeros in Row and Column
πŸ‘¨β€πŸ’» My solution: Java 96% Faster | O(N) Solution

Next time I'll post the code and explain it.
Cheers!
πŸ‘»2😁1
πŸ‡ΊπŸ‡Ώ Amaliyot.

Hammaga salom! "Little coding never killed nobody" - Fergie opamiz aytganidek! πŸ˜πŸ˜πŸ˜‚πŸ˜‚
Mayli, bugun 2D massivlik masala yechamiz.

🟑 O'rta daraja: 2482. Difference Between Ones and Zeros in Row and Column
πŸ‘¨β€πŸ’» Mening yechimim: Java 96% Faster | O(N) Solution

Keyingi safar shu masalaning yechimini va uning izohini ko'rib chiqamiz.
Omad!
πŸ”₯3πŸ‘1😁1
πŸ‡¬πŸ‡§ No post

Sorry, guys, but no post today due to some reasons that don’t depend on me
:(

πŸ‡ΊπŸ‡Ώ Post bo’lmaydi

Hammaga salom! Bugun post bo’lmaydi. Sizlardan uzr so’rayman, chunki menga bog’liq bo’lmagan sabablarga ko’ra post yoza olmayman :(
πŸ‘12😱4😒3πŸ‘Œ3πŸ™1
πŸ‡¬πŸ‡§ Explanation. Problem 2482.

Hi, guys! I'll do my best to explain the solution for previous problem as simple as possible.

🟑 Medium: 2482. Difference Between Ones and Zeros in Row and Column
πŸ‘¨β€πŸ’» My solution: Java 96% Faster | O(N) Solution

πŸ‘¨β€πŸ’» Task.
We have 2D 0-indexed array. It's size is m x n. It's filled with only ones and zeros. We have to find how many ones and zeros are in each row and column and find difference of ones' sum and zero's sum for each cell.

πŸ‘¨β€πŸ’» My solution:

class Solution {
public int[][] onesMinusZeros(int[][] grid) {
int[] onesInRows = new int[grid.length];
int[] zerosInRows = new int[grid.length];
int[] onesInCols = new int[grid[0].length];
int[] zerosInCols = new int[grid[0].length];

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
onesInRows[i]++;
onesInCols[j]++;
} else {
zerosInRows[i]++;
zerosInCols[j]++;
}
}
}

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = onesInRows[i] + onesInCols[j] - zerosInRows[i] - zerosInCols[j];
}
}
return grid;
}
}

πŸ‘¨β€πŸ’» Explanation.
1. I create 4 arrays to store zeros' and ones' count for each row and column.
2. I go thru each cell and increment corresponding value in each array. For example:
- Assume I'm watching cell grid[2][3], We can see that it is located in row with index 2 and column with index 3. Then I check if the value of the cell is 1 or 0. If it is equal to 1, then I do onesInRows[2]++ and onesInCols[3]++. It means that I increment by 1 the value in onesInRows array with index 2 and in onesInCols array with index 3. Thus, repeating this action gives us total number of 1's in each row and column, which will be stored in abovementioned array under corresponding indices.
- The same is applied to zeros.
3. You can see that I don't use new 2d array in order to use less memory. I use initial 2d array and save result of difference between ones and zeros inside each cell.

I hope this made it clear!
Cheers!✌️
πŸ‘1
πŸ‡ΊπŸ‡Ώ 2482-masalaning izohi.

Hammaga salom! Bugun sizlarga avvalgi yozgan masalaning yechimining izohini bermoqchiman.

🟑 O'rta daraja: 2482. Difference Between Ones and Zeros in Row and Column
πŸ‘¨β€πŸ’» Mening yechimim: Java 96% Faster | O(N) Solution

πŸ‘¨β€πŸ’» Vazifa.
Bizga 0 indeksli 2D massiv berilgan. Uning o'lchamlari m x n. U faqat 0 va 1 lar bilan to'ldirilgan. Bizning vazifamiz bu har bir katak uchun uning qatori va ustunidagi bo'lgan 0lar va 1lar sonini topib, har bir katak uchun ularning farqini topish.

πŸ‘¨β€πŸ’» Mening yechimim:

class Solution {
public int[][] onesMinusZeros(int[][] grid) {
int[] onesInRows = new int[grid.length];
int[] zerosInRows = new int[grid.length];
int[] onesInCols = new int[grid[0].length];
int[] zerosInCols = new int[grid[0].length];

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
onesInRows[i]++;
onesInCols[j]++;
} else {
zerosInRows[i]++;
zerosInCols[j]++;
}
}
}

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = onesInRows[i] + onesInCols[j] - zerosInRows[i] - zerosInCols[j];
}
}
return grid;
}
}

πŸ‘¨β€πŸ’» Izoh.
1. Men qator va ustunlar uchun 0lar va 1lar sonini saqlash uchun 4ta massiv yarataman.
2. Har bir katakni aylanib chiqib, katakning qiymatini mos bo'lgan massivlarga saqlayman. Masalan:
- Tasavvur qiling men hozir grid[2][3] daman. Uning qator indeksi 2ga teng va istun indeksi 3ga teng. Yana men katakning qiymati 1ga yoki 0ga tengligini ko'raman. Agar u 1ga teng bo'lsa, onesInRows[2]++ and onesInCols[3]++ ishlatamiz. Bu degani 1larning soni indeksi 2ga teng bo'lgan qatorda yana 1ga oshadi va 1larning soni indeksi 3ga teng bo'lgan ustunda yana 1ga oshishi lozim. Shunday qilib, shu harakatni takroran bajarsak har bir qatorda va ustunda nechta 1lar borligini topib olamiz.
- Huddi shu harakatlarni katakning qiymati 0ga teng bo'lganida ham bajaramiz.
3. E'tibor bering, men yangi 2d massiv yaratmayman. Bu xotirani kamroq ishlatish uchun qilingan. Men bizga berilgan 2d massivni olib, uning har bir katagiga uning qatori va ustunidagi 1lar yig'indisidan 0lar yig'indisini ayriman.

Umid qilamanki, yechim izohi tushunarli bo'ldi.
Omad!✌️
πŸ‘3
πŸ‡¬πŸ‡§ Practice.

Hi, guys! Today I'll give a task and my solution to it. I'll explain it in the next post.

🟑 Medium: 75. Sort Colors
πŸ‘¨β€πŸ’» My solution:
Java 100% Faster | O(N + N) Solution

Try to solve it on your own.
Good luck!✌️
πŸ‘4