πΊπΏ 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!
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!
YouTube
Overriding equals and hashCode in Java - 039
How can I tell if two Java instances represent the same instance? And what does class equality mean in Java?
The question here is, what does it mean to be the same or different in Java. If you modeled me, maybe two of me had blue shirts and the third hadβ¦
The question here is, what does it mean to be the same or different in Java. If you modeled me, maybe two of me had blue shirts and the third hadβ¦
π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.
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.
LeetCode
Find Players With Zero or One Losses - LeetCode
Can you solve this real interview question? Find Players With Zero or One Losses - You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of sizeβ¦
Return a list answer of sizeβ¦
π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.
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.
LeetCode
Find Players With Zero or One Losses - LeetCode
Can you solve this real interview question? Find Players With Zero or One Losses - You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of sizeβ¦
Return a list answer of sizeβ¦
π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π
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π
Telegram
LeetCodin
πΊπΏ Javadagi equals() metodi
Hammaga salom! Java-da HashMap qanday ishlashini tushunish uchun Object klassidagi equals() va hashCode() metodlarining shartnomalarini bilishimiz kerak. Keling bu postda equals()-ning shartnomasini ko'rib chiqaylik.
Ikki obyektβ¦
Hammaga salom! Java-da HashMap qanday ishlashini tushunish uchun Object klassidagi equals() va hashCode() metodlarining shartnomalarini bilishimiz kerak. Keling bu postda equals()-ning shartnomasini ko'rib chiqaylik.
Ikki obyektβ¦
π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π
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π
Telegram
LeetCodin
π¬π§ equals() method in Java
Hi, guys! In order to understand how HashMap works in Java, we need to understand the contract of equals() and hashCode() methods inside of Object class. Let's cover the contract in Java docs for the first method.
There are fiveβ¦
Hi, guys! In order to understand how HashMap works in Java, we need to understand the contract of equals() and hashCode() methods inside of Object class. Let's cover the contract in Java docs for the first method.
There are fiveβ¦
π4
π¬π§ Hashtable. Sum up.
Hi, guys! Today I just want to leave links to all posts where I talked about Hashtable and its implementation in Java.
1οΈβ£ Hashtable - start
2οΈβ£ Hashtable. How to use.
3οΈβ£ Hashtable. Hashing.
4οΈβ£ Hashtable. Collision.
5οΈβ£ equals() method in Java
6οΈβ£ Theory: hashCode() contract.
7οΈβ£ Overriding equals() and hashCode()
8οΈβ£ Why equals() and hashCode() methods are so important for HashMap in Java?
If you think we need to cover some topic related to Hashtables, please, let me know in the comments.
Hi, guys! Today I just want to leave links to all posts where I talked about Hashtable and its implementation in Java.
1οΈβ£ Hashtable - start
2οΈβ£ Hashtable. How to use.
3οΈβ£ Hashtable. Hashing.
4οΈβ£ Hashtable. Collision.
5οΈβ£ equals() method in Java
6οΈβ£ Theory: hashCode() contract.
7οΈβ£ Overriding equals() and hashCode()
8οΈβ£ Why equals() and hashCode() methods are so important for HashMap in Java?
If you think we need to cover some topic related to Hashtables, please, let me know in the comments.
Telegram
LeetCodin
π¬π§Hashtable
Hi, guys! I think we covered Binary trees and Recursion well enough to be able to solve problems on LeetCode. Let's start another topic today - Hashtable. I chose this topic because it was the second most voted choice in the poll. I'll leaveβ¦
Hi, guys! I think we covered Binary trees and Recursion well enough to be able to solve problems on LeetCode. Let's start another topic today - Hashtable. I chose this topic because it was the second most voted choice in the poll. I'll leaveβ¦
π₯2
πΊπΏ Hashtable. Yakunlash.
Hammaga salom! Bugun sizlarga chiroyli qilib, Hashtable mavzusiga oid yozilgan post-larga havolalarni qoldiraman.
1οΈβ£ Hashtable - boshlanish.
2οΈβ£ Hashtable ishlatilishi.
3οΈβ£ Hashtable. Xeshlash.
4οΈβ£ Hashtable. Kolliziya.
5οΈβ£ Javadagi equals() metodi.
6οΈβ£ Nazariya: hashCode() shartnomasi.
7οΈβ£ equals() va hashCode() metodlarini override qilish
8οΈβ£ Nimaga equals() va hashCode() metodlarining HashMap-da muhim?
Agar siz Hashtable mavzusiga oid yana qaysidir mavzuni o'rganib chiqishimiz lozim deb o'ylasangiz, iltimos, comments-da yozing.
Hammaga salom! Bugun sizlarga chiroyli qilib, Hashtable mavzusiga oid yozilgan post-larga havolalarni qoldiraman.
1οΈβ£ Hashtable - boshlanish.
2οΈβ£ Hashtable ishlatilishi.
3οΈβ£ Hashtable. Xeshlash.
4οΈβ£ Hashtable. Kolliziya.
5οΈβ£ Javadagi equals() metodi.
6οΈβ£ Nazariya: hashCode() shartnomasi.
7οΈβ£ equals() va hashCode() metodlarini override qilish
8οΈβ£ Nimaga equals() va hashCode() metodlarining HashMap-da muhim?
Agar siz Hashtable mavzusiga oid yana qaysidir mavzuni o'rganib chiqishimiz lozim deb o'ylasangiz, iltimos, comments-da yozing.
Telegram
LeetCodin
π¬π§Hashtable
Hi, guys! I think we covered Binary trees and Recursion well enough to be able to solve problems on LeetCode. Let's start another topic today - Hashtable. I chose this topic because it was the second most voted choice in the poll. I'll leaveβ¦
Hi, guys! I think we covered Binary trees and Recursion well enough to be able to solve problems on LeetCode. Let's start another topic today - Hashtable. I chose this topic because it was the second most voted choice in the poll. I'll leaveβ¦
π6
π¬π§ 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π€
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:
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;
}
- 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π€
LeetCode
Ransom Note - LeetCode
Can you solve this real interview question? Ransom Note - 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.β¦
Each letter in magazine can only be used once in ransomNote.β¦
π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 π€
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:
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;
}
- 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 π€
LeetCode
Ransom Note - LeetCode
Can you solve this real interview question? Ransom Note - 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.β¦
Each letter in magazine can only be used once in ransomNote.β¦
π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βοΈ
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βοΈ
LeetCode
Rotate List - LeetCode
Can you solve this real interview question? Rotate List - Given the head of a linked list, rotate the list to the right by k places.
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
π€©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βοΈ
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βοΈ
LeetCode
Rotate List - LeetCode
Can you solve this real interview question? Rotate List - Given the head of a linked list, rotate the list to the right by k places.
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
π2
π¬π§ 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:
//At first run I find the length of it - count variable.
//Move variable shows how many elements should be rotated.
//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.
//this action connect left part with the right part of result
//with index equal to count
π¨βπ» My solution: Java Solution in just 1 ms
I hope it was usefulπ€π€
π‘ 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π€π€
LeetCode
Rotate List - LeetCode
Can you solve this real interview question? Rotate List - Given the head of a linked list, rotate the list to the right by k places.
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
π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:
//Boshida men list-ning uzunligini topaman
//U count-ni k-ga bo'lganda qoladigan qoldiqqa tenk.
//Move nechta element surilishi kerakligini ko'rsatadi.
//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
//Uning bosh elementiga bilan ulayman
//Bu natijadagi list-ning chap va o'ng qismlarini ulaydi.
//count-ga teng bo'lgan element
π¨βπ» Mening yechimim: Java Solution in just 1 ms
Foydali bo'ldi degan umiddaman π€π€
π‘ 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 π€π€
LeetCode
Rotate List - LeetCode
Can you solve this real interview question? Rotate List - Given the head of a linked list, rotate the list to the right by k places.
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
Example 1:
[https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg]
Input: head = [1,2,3,4,5], k = 2
Output: [4β¦
π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!
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!
YouTube
Fergie - A Little Party Never Killed Nobody (All We Got) ft. Q-Tip, GoonRock
Buy Now!
iTunes: http://smarturl.it/GatsbyMusic
iTunes: http://smarturl.it/GatsbyMusic
π»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!
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!
YouTube
Fergie - A Little Party Never Killed Nobody (All We Got) ft. Q-Tip, GoonRock
Buy Now!
iTunes: http://smarturl.it/GatsbyMusic
iTunes: http://smarturl.it/GatsbyMusic
π₯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 :(
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:
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!βοΈ
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!βοΈ
LeetCode
Difference Between Ones and Zeros in Row and Column - LeetCode
Can you solve this real interview question? Difference Between Ones and Zeros in Row and Column - You are given a 0-indexed m x n binary matrix grid.
A 0-indexed m x n difference matrix diff is created with the following procedure:
* Let the number ofβ¦
A 0-indexed m x n difference matrix diff is created with the following procedure:
* Let the number ofβ¦
π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:
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!βοΈ
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!βοΈ
LeetCode
Difference Between Ones and Zeros in Row and Column - LeetCode
Can you solve this real interview question? Difference Between Ones and Zeros in Row and Column - You are given a 0-indexed m x n binary matrix grid.
A 0-indexed m x n difference matrix diff is created with the following procedure:
* Let the number ofβ¦
A 0-indexed m x n difference matrix diff is created with the following procedure:
* Let the number ofβ¦
π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!βοΈ
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!βοΈ
LeetCode
Sort Colors - LeetCode
Can you solve this real interview question? Sort Colors - You are given an array nums with n objects colored red, white, or blue, sort them in-place [https://en.wikipedia.org/wiki/In-place_algorithm] so that objects of the same color are adjacent, with theβ¦
π4