-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Souravrao-31/souravrao
Leetcode Sols
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |