Merge Two 2D Arrays by Summing Values - Leetcode 2570 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
4:57 - Coding Explanation

leetcode 2570

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Can you make a video on how to start competitive programming in code forces

krishnaawasthi
Автор

For absolute beginners you can use a Hashmap and store the ids and values of nums1 by iterating to it . After it iterate over nums2 and now add the values based upon ids . For ex. If id 2 already exist in map with value 7 and the same id 2 is again encountered in nums2 with value 4 then just add the values for same id and store it like map[nums2[i]] += nums2[i];
If the I'd already exist then it will do this otherwise it will create a new key-value pair and store it. Now we have updated id and values in the form of key value pair in the map. Now just sort the map on the basis of id and push the id, value in the array and return it.
This is an more general and easier approach. Time complexity will be O(n) .

legendrebel
Автор

this is a better beginner friendly approach with Ologn complexity
class Solution:
def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
data = {}

for id, val in nums1:
data[id] = val

for id, val in nums2:
if id in data:
data[id] += val
else:
data[id] = val

res = []
for num in sorted(data):
res.append([num, data[num]])
return res

midhunskani
Автор

In Ruby (and perhaps here in Python as well), it's worth considering doing replacement of values in one of the given arrays, rather than building a new one, can help with memory use.

augustsbautra
Автор

I solved that using SortedSet and HashMap.
1. While traversing 2D arrays just add id in SortedSet and save value for that id in HashMap
2. Loop through Sorted set and just add pairs id and value from dictionary.

dusvn
Автор

Added [ inf, 0 ] to both the arrays
And when returning the result pop one element
😅
This reduces 2 more while loops code

damodar___
Автор

instead of using extra two loops for appending remaining values we can

class Solution:
def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
p1=0
p2=0
ans=[]
while p1<len(nums1) and p2<len(nums2):
if nums1[p1][0]<nums2[p2][0]:
ans.append(nums1[p1])
p1+=1
elif nums1[p1][0]>nums2[p2][0]:
ans.append(nums2[p2])
p2+=1
else:
nums1[p1][1]+=nums2[p2][1]
ans.append(nums1[p1])
p1+=1
p2+=1
return (ans+nums1[p1:]+nums2[p2:])

manindharthirupathi
Автор

class Solution:
def mergeArrays(self, a: List[List[int]], b: List[List[int]]) -> List[List[int]]:

return [(k, a[k])for k in sorted(a)]

qulinxao
Автор

shameful that i wasn't able to solve it myself this being an easy question should have been solvable

alizu
Автор

Thanks to examples otherwise I could have never solved this problem.

sheik-tlvu
Автор

I really like this solution, but could you not just use Python's built-in .extend function? It seems like that would be a lot cleaner than the last two unnecessary while loops. Thanks again for the video!

mattk.
Автор

i'm so noob. i solved it using heap with two iterations but it was super slow

JamesBond-mqpd
join shbcf.ru