Find the Difference of Two Arrays - Leetcode 2215 - Python

  Рет қаралды 10,885

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
Solving Find the Difference of Two Arrays - Leetcode 2215, today's daily leetcode on May 2nd.
🥷 Discord: / discord
🐦 Twitter: / neetcode1
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
Problem Link: leetcode.com/problems/find-th...
0:00 - Read the problem
0:30 - Drawing Explanation
3:35 - Coding Explanation
leetcode 2215
#neetcode #leetcode #python

Пікірлер: 21
@callofduty1
@callofduty1 Жыл бұрын
I also solved it today, and this is my approach: set1 = set(nums1) set2 = set(nums2) return [list(set1 - set2), list(set2 - set1)]
@Andrew_J123
@Andrew_J123 Жыл бұрын
This was my first thought too
@callofduty1
@callofduty1 Жыл бұрын
@@Andrew_J123 The reason I went with this approach is because it's short, simple and easy to understand for beginners,
@DeathSugar
@DeathSugar Жыл бұрын
what numbers it yielded?
@jymival2355
@jymival2355 Жыл бұрын
You have explained the problem in a way more complicated way than it has to be. You dont need set to list conversions. When you turn the lists into sets at the beginning to get constant time access to the numbers you also instantly remove any duplicates. From there you can either just use 2 list comprehensions or write a for loop or even use more pythonic code like set1-set2. The problem is obviously aimed at beginners so no need to make it any more confusing by worrying about duplicates in a set and then adding them to another set and then making it a list when there were no duplicates anymore at all in the beginning.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
True that we should iterate the sets rather than lists. But for the pythonic solution, I usually try to avoid those to make it readable for ppl from other langs.
@jymival2355
@jymival2355 Жыл бұрын
@@NeetCodeIOyeah i totally understand why you wouldnt want to use too much pythonic code due to people watching that maybe dont code in python and non pythonic solutions help create a deeper knowledge of coding and the ideas behind it. I love your videos and they help me a ton just wanted to point it out.
@blossomprogrammingworld4654
@blossomprogrammingworld4654 Жыл бұрын
good day, I just came across your channel and I have been following it. It's good and I appreciate the explanation. I will appreciate it if more video on programming skill study plan from leetcode was created. Many thanks for considering my request.
@kirillzlobin7135
@kirillzlobin7135 24 күн бұрын
Great video!
@notgreen11
@notgreen11 Жыл бұрын
my first thoughts for this problem were: 1) O(n+m) space and time, turn the arrays into sets, and use those to form the output 2) sort the arrays with O(n log n) time and O(1) space. Iterate over each array and perform a binary lookup (log n) in the second array. This gives n log m + m log n time
@asmahamdym
@asmahamdym Жыл бұрын
they say that result is only of length 2 so I initialized res like this: res = [ [ ], [ ] ] and append to res[0] and res[1]
@keerthivasan764
@keerthivasan764 Жыл бұрын
Bro when does google lift hiring freeze
@HarshaVardhan-fu9kv
@HarshaVardhan-fu9kv Жыл бұрын
Bro when you converted list into set (line 3) then duplicates are removed, no need to fear about that
@massimomonticelli6010
@massimomonticelli6010 Жыл бұрын
Agreed: instead of traversing nums1 (line 6) and nums2 (line 10), just traverse nums1Set and nums2Set. In this way, both res1 and res2 can be lists.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Yep, a silly mistake on my part. Should've iterated through the Sets not the Lists.
@TheElementFive
@TheElementFive Жыл бұрын
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: a = set(nums1).difference(set(nums2)) b = set(nums2).difference(set(nums1)) return [list(a), list(b)]
@vignan555
@vignan555 2 ай бұрын
@TheElementFive as the set is unordered how we are getting the relative order
@mohamedhassan-ub4kj
@mohamedhassan-ub4kj 11 ай бұрын
why don't you use hash map and map each element to it with value 1 , then remove elements from the hash keys while building the answer2 , lastly loop through the remaining keys and build the ans1 and return [ans1 , ans2]
@vignan555
@vignan555 2 ай бұрын
@NeetCodeIO: as the list is unordered how we are getting the relative order
@vignan555
@vignan555 2 ай бұрын
sorry set
@ChandraNandan-mb5sq
@ChandraNandan-mb5sq 3 ай бұрын
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: answer = [[],[]] max_len = max(len(nums1),len(nums2)) for i in range(max_len): if i < len(nums1): if nums1[i] not in nums2 and nums1[i] not in answer[0]: answer[0].append(nums1[i]) if i < len(nums2): if nums2[i] not in nums1 and nums2[i] not in answer[1]: answer[1].append(nums2[i]) return answer
Matrix Diagonal Sum - Leetcode 1572 - Python
8:55
NeetCodeIO
Рет қаралды 7 М.
Dota2 Senate - Leetcode 649 - Python
14:48
NeetCodeIO
Рет қаралды 19 М.
This is not my neighbor  Terrible neighbor! #funny #zoonomaly #memes
00:26
Самый Молодой Актёр Без Оскара 😂
00:13
Глеб Рандалайнен
Рет қаралды 7 МЛН
ТАМАЕВ УНИЧТОЖИЛ CLS ВЕНГАЛБИ! Конфликт с Ахмедом?!
25:37
Leetcode - Determine if Two Strings Are Close (Python)
5:33
Timothy H Chang
Рет қаралды 4,4 М.
Find the Difference - Leetcode 389 - Python
11:24
NeetCodeIO
Рет қаралды 10 М.
2215. Find the Difference of Two Arrays (Leetcode) || Editorial || C++ Code
4:24
UNIQUE NUMBER OF OCCURENCES | LEETCODE 1207 | PYTHON SOLUTION
10:31
Cracking FAANG
Рет қаралды 1 М.
LeetCode Find Pivot Index Solution Explained - Java
7:12
Nick White
Рет қаралды 20 М.
Count Ways to Build Good Strings - Leetcode 2466 - Python
14:09
Intersection of Two Linked Lists - Leetcode 160 - Python
8:13
This is not my neighbor  Terrible neighbor! #funny #zoonomaly #memes
00:26