Merge Two 2D Arrays by Summing Values | Multiple Approaches | Leetcode 2570 | codestorywithMIK

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

Hi Everyone, this is the 71st video of our Playlist "Leetcode Easy".
Now we will be solving an easy practice problem on Array based on 2 Pointers - Merge Two 2D Arrays by Summing Values | Multiple Approaches | Leetcode 2570 | codestorywithMIK

We will solve it using multiple Approaches. First using (HashMap + Sorting), Second using Approach-2 (Ordered Map), and third using 2 Pointer approach.

Problem Name : Merge Two 2D Arrays by Summing Values | Multiple Approaches | Leetcode 2570 | codestorywithMIK
Company Tags : will update later

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Video Summary :
Approach-1 (HashMap + Sorting): We use a map to store the sum of values for each unique key, then extract and sort the entries. This approach leverages fast lookups but requires sorting at the end.

Approach-2 (Ordered Map): A map that maintains sorted keys automatically is used to merge values while ensuring order. This removes the need for explicit sorting but has logarithmic insertion time complexity.

Approach-3 (Two Pointers): Since both arrays are sorted, we merge them efficiently using two pointers, directly producing a sorted result in linear time without extra space.

✨ Timelines ✨
00:00 - Introduction
0:24 - Motivation
0:58 - Problem Explanation
3:31 - Brute Force
4:46 - Approach-1 (Using map and sorting)
7:10 - Approach-2 (Using Ordered Map)
9:00 - Approach-3 (Using 2 Pointer)
15:35 - Coding all approaches

#MIK #mik #Mik #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #codestorywithmik #codestorywithmick #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

hello MIK bhaiya i am requesting you to pls start uploading videos of upsolving of weekly and biweekly leetcode contest, if not full then only 2nd and 3rd problem pls try to upload it will be very helpful for us

Aryan_Rajput
Автор

Hello MIK sir please provide solution of weekly and biweekly leetcode contest. Because your teaching style and approach is quite amazing. It will be great help for us.🙏🙏🙏🙏

harshtripathi
Автор

Like who are preparing for product based company's

hareesh_sureddy
Автор

bohut easy hai bhaiya fir bhi aapke video mai ana toh reflex action hai ab🫡

s..
Автор

Thought of the optimal approach first, came here for other approaches. Thank you sir.

baitMaster
Автор

I hope your leg is better now.
Hats off to your dedication

FanIQQuiz
Автор

thanks man !!, i was able to do the brute force then i just watched your approached 2 hints part and solved it !!

rohithalder
Автор

Hello Bhaiya, thank you so much for uploading daily problem solutions—they are really helpful! 🙌 If possible, could you also upload solutions for weekly and biweekly contests? It would be greatly appreciated. Thanks again for your efforts! 😊

sahilkumar
Автор

Vote for Weekly and Biweekly contests solution 👇🏻👇🏻

jaykant
Автор

Assalam alaikum Mazhar Bhai.
Ramadan Mubarak to you.

mohammadaftabansari
Автор

please make video on permutations IV pls

KashishWadhwa-py
Автор

class Solution {
public:
vector<vector<int>> nums1, vector<vector<int>>& nums2) {
vector<vector<int>> ans;
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size())
{
if (nums1[i][0] == nums2[j][0])
{
ans.push_back({nums1[i][0], nums1[i][1] + nums2[j][1]});
i++;
j++;
}
else if (nums1[i][0] < nums2[j][0])
{
ans.push_back(nums1[i]);
i++;
}
else
{
ans.push_back(nums2[j]);
j++;
}
}
while (i < nums1.size())
{
ans.push_back(nums1[i]);
i++;
}
while (j < nums2.size())
{
ans.push_back(nums2[j]);
j++;
}
return ans;
}
};
solvedddd

sayanbiswas
Автор

Hi mik,

I am coding in c.

I have a doubt about how maps can be used in c

hareesh_sureddy
Автор

Hello MIK bhai, can you make HLD and LLD playlist also, we love your way for teaching, I learned recursion, graph, dp from your concepts playlist, I have also watched your system design playlist in funny way, it was amazing, so want if can make HLD and LLD that easy too, I know you didn't have much time from your day job, but if you can then we will love to learn

varunpalsingh
Автор

I know My solution isn't optimal but I did it on my own and i''ll soon try to improve the solution quality. Before this chennal I wasn't able to solve any question but now I can build a story to solve the problem and now gradually I am learning to convert the story into Code. Thank you So much sir.

Here is my solution.
class Solution {
public:
vector<vector<int>> nums1,
vector<vector<int>>& nums2) {
vector<vector<int>> res;
int i = 0;
int j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i][0] == nums2[j][0]) {
res.push_back({nums2[j][0], nums1[i][1] + nums2[j][1]});
i++;
j++;
} else {
if (nums1[i][0] < nums2[j][0]) {
res.push_back(nums1[i]);
i++;
} else {
res.push_back(nums2[j]);
j++;
}
}
}
while (i < nums1.size()) {
res.push_back(nums1[i]);
i++;
}

while (j < nums2.size()) {
res.push_back(nums2[j]);
j++;
}
return res;
}
};

Rabiii
Автор

sir if possible, plz make video on leetcode yesterday biweekly contest ques 4th 3470

Anshul-qbpm
Автор

I solved it by myself today, using one loop

class Solution {
public int[][] mergeArrays(int[][] nums1, int[][] nums2) {
Map<Integer, Integer> map = new LinkedHashMap<>();
int i = 0, j = 0;
int len1 = nums1.length;
int len2 = nums2.length;
while(i < len1 || j < len2){
int id1 = i < len1 ? nums1[i][0] : Integer.MAX_VALUE;
int val1 = i < len1 ? nums1[i][1] : 0;

int id2 = j < len2 ? nums2[j][0] : Integer.MAX_VALUE;
int val2 = j < len2 ? nums2[j][1] : 0;

if(id1 == id2){
map.put(id1, val1 + val2);
i++;
j++;
}
else if(id1 < id2){
map.put(id1, val1);
i++;
}
else{
map.put(id2, val2);
j++;
}

}
int n = map.size();
int[][] result = new int[n][2];
int k = 0;
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
int key = entry.getKey();
int value = entry.getValue();
result[k][0] = key;
result[k][1] = value;
k++;
}
return result;
}
}

aizadiqbal
Автор

Please make a video on today's weekly contest

harshshah
Автор

The moment, qs says its sorted, I thought about two pointer but still for the sake of implementation 1st I went with unordered_map :
approach 1 : O(NlogN), sc : O(m+n)
class Solution {
public:
vector<vector<int>> nums1, vector<vector<int>>& nums2) {
unordered_map<int, int>mp;

for(auto num:nums1){
mp[num[0]] = num[1];
}

for(auto num : nums2){
if(mp.contains(num[0])){
mp[num[0]] += num[1];
}else{
mp[num[0]] = num[1];
}
}

vector<vector<int>>ans;

for(auto [a, b] : mp){
ans.push_back({a, b});
}
sort(ans.begin(), ans.end(), [](const vector<int>&a, const vector<int>&b){
return a[0]<b[0];
});
return ans;
}
};

/*Yesterday, I learned about the lambda function in the sort function, so I wanted to implement it again. It feels good! 😊 But even if I don't use it, sorting will be done based on the first element by default. However, if the function needs to sort based on the second element, I would have to use a custom sort.

It's better to practice the syntax regularly. ✅*/


approach 2 : Two pointers , O(N+M),
class Solution {
public:
vector<vector<int>> nums1, vector<vector<int>>& nums2) {
int n = nums1.size();
int m = nums2.size();

int i=0, j=0;
vector<vector<int>>ans;
while(i<n || j< m){
if( i >= n && j<m){
ans.push_back({nums2[j][0], nums2[j][1]});+j++;
}else if(j>=m && i<n){
ans.push_back({nums1[i][0], nums1[i][1]});++i;
}else if(nums1[i][0] > nums2[j][0]){
ans.push_back({nums2[j][0], nums2[j][1]});
j++;
}else if(nums1[i][0] < nums2[j][0]){
ans.push_back({nums1[i][0], nums1[i][1]});
i++;
}else{
int val = nums1[i][1]+nums2[j][1];
ans.push_back({nums1[i][0], val});
i++;j++;
}
}

return ans;
}
};

SRoy
Автор

Hello MIK,

I want to ask you something and hope you will reply. Companies will start visiting my college within six months. Currently, I am working through the arrays playlist, solving only array-based questions, and following the path you provided in your New Year guidance video. However, I’m worried that I might get stuck on one topic for too long if I try to complete an entire playlist. At the same time, these questions are important since they have been asked in previous interviews.

What should I do? I’m a bit confused. The clarity I get from your videos is unmatched, which is why I’m not following Striver’s sheet, whereas everyone else in my college is doing so. Please guide me a little bit on this—it would be a huge help.

I also tried reaching out to you on Topmate, but the option to book a session isn’t available.

Thanks! I hope you’ll reply.

kyaHiKarSakteH
visit shbcf.ru