diff --git a/16.cpp b/16.cpp new file mode 100644 index 00000000..3c1583d4 --- /dev/null +++ b/16.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int threeSumClosest(vector& A, int target) { + sort(A.begin(),A.end()); + int f,s,t; + int res=0; + int min_diff =INT_MAX; + for(f=0;ftarget) + t--; + else if(sum& A) { + int i,j; + int n = A.size(); + for(i = n - 2;i >= 0;i --) + { + if(A[i] < A[i + 1]) + break; + } + if(i == -1) + { + reverse(A.begin(),A.end()); + return ; + } + + for(j = n - 1;j > i;j --) + { + if(A[j] > A[i]) + break; + } + swap(A[j],A[i]); + reverse(A.begin() + i + 1,A.end()); + return ; + } +}; diff --git a/leetcode/cpp/Array/84.cpp b/leetcode/cpp/Array/84.cpp new file mode 100644 index 00000000..8adfba47 --- /dev/null +++ b/leetcode/cpp/Array/84.cpp @@ -0,0 +1,24 @@ +class Solution { +public: +int largestRectangleArea(vector &A) { + int res = 0; + A.push_back(0); + + stack st; + + for(auto i=0; i= A[st.top()]) + st.push(i); + else + { + int j = st.top(); + st.pop(); + res = max(res, A[j]*(st.empty() ? i : i - st.top() - 1)); + i--; + } + } + + return res; +} +}; diff --git a/leetcode/cpp/Stacks/155.cpp b/leetcode/cpp/Stacks/155.cpp new file mode 100644 index 00000000..d389f30c --- /dev/null +++ b/leetcode/cpp/Stacks/155.cpp @@ -0,0 +1,49 @@ +class MinStack { +public: + /** initialize your data structure here. */ + MinStack() { + + } + stack st; + stack min_st; + void push(int x) { + st.push(x); + int val =x; + if(min_st.size()>0 && min_st.top()push(x); + * obj->pop(); + * int param_3 = obj->top(); + * int param_4 = obj->getMin(); + */ diff --git a/leetcode/cpp/Two Pointer/16.cpp b/leetcode/cpp/Two Pointer/16.cpp new file mode 100644 index 00000000..3c1583d4 --- /dev/null +++ b/leetcode/cpp/Two Pointer/16.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int threeSumClosest(vector& A, int target) { + sort(A.begin(),A.end()); + int f,s,t; + int res=0; + int min_diff =INT_MAX; + for(f=0;ftarget) + t--; + else if(sum& nums) { - int low=0,mid=0,high=nums.size()-1; - while(mid<=high){ - - if(nums[mid]==0) - { - swap(nums[low],nums[mid]); - low++,mid++; - } - else if(nums[mid]==1) - { - mid++; - } - else{ - swap(nums[mid],nums[high]); - high--; - } - } - } -}; +class Solution { +public: + void sortColors(vector& A) { + int count_0=0,count_1=0,count_2=0; + for(int i=0;i