Day4: (Hashing)
2 Sum problem Solved on: Aug 11 2020
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> m;
int n=nums.size();
int diff;
for (int i=0;i<n;i++) {
int ele = target-nums[i];
if (m.count(ele)) {
return {m[ele], i };
}
m[nums[i]] = i;
}
return {};
}
Longest Consecutive Sequence Solved on: 20th Aug 2020
def longestConsecutive(self, nums: List[int]) -> int:
count=1
nums=list(set(nums))
n=len(nums)
if (n==0):
return 0
nums.sort()
print (nums)
max_count=1
for i in range(1, n):
if nums[i-1]+1 == nums[i]:
count=count+ 1
if (count > max_count):
max_count=count
else:
count=1
return max_count
Last updated
Was this helpful?