From 3358ff24bba8addb7ff4980a1028f4248e753f3d Mon Sep 17 00:00:00 2001 From: Napri Date: Tue, 4 Oct 2022 15:06:39 +0530 Subject: [PATCH 1/2] 3Sum Leetcode(15) --- Medium/15.3Sum.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Medium/15.3Sum.cpp diff --git a/Medium/15.3Sum.cpp b/Medium/15.3Sum.cpp new file mode 100644 index 0000000..d5a7432 --- /dev/null +++ b/Medium/15.3Sum.cpp @@ -0,0 +1,42 @@ +/* Leetcode Problem: 15. 3Sum + Link: https://leetcode.com/problems/3sum/ +*/ + + +class Solution { +public: + vector> threeSum(vector& nums) { + int n = nums.size(); + sort(nums.begin(), nums.end()); + + vector> ans; + for(int i=0; i 0 && nums[i] == nums[i-1])continue; + int a = i+1, b = n-1; + while(a < b) + { + int sum = nums[i]+nums[a]+nums[b]; + // cout< 0) + b--; + else + a++; + } + } + return ans; + } +}; \ No newline at end of file From fca922fd7e1b61f2864e805150298aaf8b657f44 Mon Sep 17 00:00:00 2001 From: Napri Date: Tue, 4 Oct 2022 18:42:03 +0530 Subject: [PATCH 2/2] Added Leetcode(56) --- Medium/15.3Sum.cpp | 42 ------------------------------ Medium/56.Merge_Intervals.cpp | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 42 deletions(-) delete mode 100644 Medium/15.3Sum.cpp create mode 100644 Medium/56.Merge_Intervals.cpp diff --git a/Medium/15.3Sum.cpp b/Medium/15.3Sum.cpp deleted file mode 100644 index d5a7432..0000000 --- a/Medium/15.3Sum.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* Leetcode Problem: 15. 3Sum - Link: https://leetcode.com/problems/3sum/ -*/ - - -class Solution { -public: - vector> threeSum(vector& nums) { - int n = nums.size(); - sort(nums.begin(), nums.end()); - - vector> ans; - for(int i=0; i 0 && nums[i] == nums[i-1])continue; - int a = i+1, b = n-1; - while(a < b) - { - int sum = nums[i]+nums[a]+nums[b]; - // cout< 0) - b--; - else - a++; - } - } - return ans; - } -}; \ No newline at end of file diff --git a/Medium/56.Merge_Intervals.cpp b/Medium/56.Merge_Intervals.cpp new file mode 100644 index 0000000..f6b701a --- /dev/null +++ b/Medium/56.Merge_Intervals.cpp @@ -0,0 +1,48 @@ +/* +Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, + and return an array of the non-overlapping intervals that cover all the intervals in the input. + +Example 1: +Input: intervals = [[1,3],[2,6],[8,10],[15,18]] +Output: [[1,6],[8,10],[15,18]] + +1----3 => 1----6 + 2----6 + 8----10 => 8----10 + 15----18 => 15----18 + +*/ + + +class Solution { +public: + vector> merge(vector>& intervals) { + + vector> ans; + sort(intervals.begin(), intervals.end()); + + int n = intervals.size(); + for(int i=0; i