Skip to content

Commit

Permalink
Merge pull request #8 from Souravrao-31/souravrao
Browse files Browse the repository at this point in the history
Leetcode Sols
  • Loading branch information
Ashish-kumar7 authored Oct 15, 2020
2 parents 61f7206 + 5d60e5f commit 7b01b39
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 56.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<int>arr(2);
vector<vector<int>>ans;
if(intervals.size()==0) return ans;
sort(intervals.begin(),intervals.end());
arr[0]=intervals[0][0];arr[1]=intervals[0][1];
for(int i=1;i<intervals.size();i++)
{ if(arr[1]>=intervals[i][0])
arr[1]=max(intervals[i][1],arr[1]);
else
{ ans.push_back({arr[0],arr[1]});
arr[0]=intervals[i][0];
arr[1]=intervals[i][1];
}
}
ans.push_back({arr[0],arr[1]});
return ans;
}
};
35 changes: 35 additions & 0 deletions 59.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:

vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> matrix;
matrix.resize(n, std::vector<int>(n, -1));

int top = 0;
int bottom = n-1;
int left = 0;
int right = n-1;
int val = 1;
while (left<=right) {

for (int i = left;i<=right;i++) {
matrix[left][i] = val++;
}
top++;

for (int i = top;i<=bottom;i++) {
matrix[i][right] = val++;
}
right--;
for (int i = right;i>=left;i--) {
matrix[bottom][i] = val++;
}
bottom--;
for (int i = bottom;i>=top;i--) {
matrix[i][left] = val++;
}
left++;
}
return matrix;
}
};
24 changes: 24 additions & 0 deletions 60.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
string getPermutation(int n, int k) {
string st(n,'0');
int fact=1;
for(int i=1;i<=n;i++){
fact*=i;
st[i-1]+=(i);
}
for(int i=0;i<n;i++){
int block_num=fact/(n-i);
fact/=(n-i);
int first_index=i+(k-1)/(block_num);
char c=st[first_index];
k=k-(((k-1)/(block_num))*(block_num));
for(int j=first_index;j>=i+1;j--){
st[j]=st[j-1];
}
st[i]=c;
cout<<st<<" "<<k<<" ";
}
return st;
}
};

0 comments on commit 7b01b39

Please sign in to comment.