LeetCode Two Sum | C++ | KeepLearningEveryday

preview_player
Показать описание
Two Sum - LeetCode solutions.

This video contains the solution for the problem #TwoSum in two ways. This is one of common questions asked in Telephonic #Interview round of #Microsoft #Amazon #facebook
1. Bruteforce, Time complexity O(n^2)
2. Efficient, Time complexity O(n)

Please do let me know if you have any inputs/concerns for the solution.

Also do like, share and Subscribe :) Learn everyday

Thank you
Рекомендации по теме
Комментарии
Автор

Thank you a lot Nikash! It was such a beginner friendly video.

luffy_lyf
Автор

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> ans;
int size = nums.size();
int k ;
vector<int> ret;
for (int i =0; i<size ; i++){
k = target - nums[i];
if (ans.find(k) != ans.end() && ans.find(k)-> second != i){
//then the k does exist in teh map
ret.emplace_back(i);

return ret ;
}
//if you reached here then the k doesn't exist, then add the nums[i]
ans.emplace(nums[i], i );
}
return ret;
}
};

mohamedantar
Автор

You don't need the second check on line 12 because in a single pass when you are at i, it can never be in the map. It works without the check because you only check against the previous ones and never with yourself.

AdityaSaurabh
Автор

wow you made this look easy. I thought we had to iterate through the vector with for(auto iter = nums.begin(); iter != nums.end(); iter++){//logic}
maybe that is when you are messing with a vector holding objects? I need to open up my intro to C++ book again.

michaelhernandez
Автор

i think in condition of maps it should be compared to the element in vector not the index...

srujanbandam
Автор

Thanks Nikash. Clear Explanation. Keep it up. Looking forward to seeing more videos in Leetcode C++

saravanakumar
Автор

please please tell me why are checking the m.end() condition i dont get that, please explain that

mirdulswarup
Автор

For the first for loop, why do you do “i < size - 1”? I am just confuse about it. For size; would it mean the number of index inside the array of nums? Which is 3. Do you subtract the array as the loops keeps going until the the condition is met where “i” is less than “size - 1”?

Overrunnerr
Автор

What should I learn to be able solving these problems ? (I only learned the basic of c++)

مريممجدي-صح
Автор

what is the meaning of
m.find(diff)!=m.end()
at 11:44

yogeshyts
Автор

#include<iostream>
#include<unordered_map>
#include<vector>

using namespace std;

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int>nums_indices;
int size = nums.size();
int second_integer;

unordered_map<int, int>m;

for(int i = 0; i < size; i++){
second_integer = target - nums.at(i);
if(m.find(second_integer) != m.end()){
nums_indices.push_back(i);
-> second);
return nums_indices;
}
}
return nums_indices;
}
};

int main(){
vector<int>nums{2, 7, 11, 15};
int target = 9;
Solution s;
s.twoSum(nums, target);
return 0;
}

rhehebbfb
Автор

Hey Nikash Thank you so much for such easy to understand code but I am getting runtime error when I type the same code. Can you please help me out?

vedikasangle
Автор

Can u tell me the Y v used two ' return ' in brute -force method.

mrnoobie
Автор

Why do we need to return the vector in if statement (it's not a function either) someone pls help

adios
Автор

what does m.find(diff)--> second signify? I lost it here.

prashantpant
Автор

I didn't get the part of time complexity : Insertion and look up takes log(size) time in map.
and the outer for loop runs N times.
then shouldnt it be Nlog(size) complexity == NlogN.
please do explain

tusharshaily
Автор

thanks can you recommend books for competitive programming

khushbookumari
Автор

i don't understand how push_back worked there can someone help?

madanmohan
Автор

Your voice is very low
Next time keep your mic near your mouth !

ananydubey
join shbcf.ru