Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added leetcode problems - 75-sort_colors.cpp, 16-3sum_closest.cpp, 31-next_permutation.cpp, 155-min_stack.cpp, 84-Largest_Rectangle_in_Histogram.cpp issue #27 #288

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
int threeSumClosest(vector<int>& A, int target) {
sort(A.begin(),A.end());
int f,s,t;
int res=0;
int min_diff =INT_MAX;
for(f=0;f<A.size()-2;f++)
{
s= f+1;
t=A.size()-1;
while(s<t)
{ int sum= A.at(f) + A.at(s) + A.at(t);
int cur = fabs(sum-target);
if(cur<min_diff)
{
min_diff =cur;
res =sum;
}
if(sum>target)
t--;
else if(sum<target)
s++;
else
return sum;

}
}
return res;
}
};
26 changes: 26 additions & 0 deletions leetcode/cpp/Array/31.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
void nextPermutation(vector<int>& 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 ;
}
};
24 changes: 24 additions & 0 deletions leetcode/cpp/Array/84.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int largestRectangleArea(vector<int> &A) {
int res = 0;
A.push_back(0);

stack<int> st;

for(auto i=0; i<A.size(); i++)
{
if(st.empty() || A[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;
}
};
49 changes: 49 additions & 0 deletions leetcode/cpp/Stacks/155.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class MinStack {
public:
/** initialize your data structure here. */
MinStack() {

}
stack<int> st;
stack<int> min_st;
void push(int x) {
st.push(x);
int val =x;
if(min_st.size()>0 && min_st.top()<x)
{
val =min_st.top();
}
min_st.push(val);
}

void pop() {
if(!st.empty())
{ st.pop();
min_st.pop();
}
}

int top() {
if(st.empty())
return -1;
else
return st.top();

}

int getMin() {
if(st.empty())
return -1;
else
return min_st.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
31 changes: 31 additions & 0 deletions leetcode/cpp/Two Pointer/16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
int threeSumClosest(vector<int>& A, int target) {
sort(A.begin(),A.end());
int f,s,t;
int res=0;
int min_diff =INT_MAX;
for(f=0;f<A.size()-2;f++)
{
s= f+1;
t=A.size()-1;
while(s<t)
{ int sum= A.at(f) + A.at(s) + A.at(t);
int cur = fabs(sum-target);
if(cur<min_diff)
{
min_diff =cur;
res =sum;
}
if(sum>target)
t--;
else if(sum<target)
s++;
else
return sum;

}
}
return res;
}
};
43 changes: 21 additions & 22 deletions leetcode/cpp/Two Pointer/75.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
class Solution {
public:
void sortColors(vector<int>& 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<int>& A) {
int count_0=0,count_1=0,count_2=0;
for(int i=0;i<A.size();i++)
{
if(A[i]==0)
count_0++;
else if(A[i]==1)
count_1++;
else if(A[i]==2)
count_2++;
}
for(int i=0;i<count_0;i++)
A[i]=0;
for(int i=count_0;i<count_0+count_1;i++)
A[i]=1;
for(int i=count_1+count_0;i<count_0+count_1+count_2;i++)
A[i]=2;
}
};