Skip to content

Commit 0d955d7

Browse files
committed
Practise 23-Jul-2020
1 parent e1af9ef commit 0d955d7

8 files changed

+511
-0
lines changed

array/merge_intervals.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Given a collection of Intervals,merge all the overlapping Intervals.
3+
For example:
4+
5+
Given [1,3], [2,6], [8,10], [15,18],
6+
7+
return [1,6], [8,10], [15,18].
8+
9+
Make sure the returned intervals are sorted.
10+
11+
12+
13+
Input:
14+
15+
The first line contains an integer T, depicting total number of test cases.
16+
Then following T lines contains an integer N depicting the number of Intervals and next line followed by the intervals starting and ending positions 'x' and 'y' respectively.
17+
18+
19+
Output:
20+
21+
Print the intervals after overlapping in sorted manner. There should be a newline at the end of output of every test case.
22+
23+
Constraints:
24+
25+
1 ≤ T ≤ 50
26+
1 ≤ N ≤ 100
27+
0 ≤ x ≤ y ≤ 100
28+
29+
30+
Example:
31+
32+
Input
33+
2
34+
4
35+
1 3 2 4 6 8 9 10
36+
4
37+
6 8 1 9 2 4 4 7
38+
39+
Output
40+
1 4 6 8 9 10
41+
1 9
42+
*/
43+
44+
45+
46+
47+
48+
49+
50+
#include<bits/stdc++.h>
51+
using namespace std;
52+
53+
bool comp(vector<int> i1, vector<int> i2){
54+
return i1[0] < i2[0];
55+
}
56+
57+
vector<vector<int>> merge(vector<vector<int>>& intervals) {
58+
vector<vector<int> > res;
59+
if(intervals.size()<=1) return intervals;
60+
sort(intervals.begin(), intervals.end(), comp);
61+
pair<int, int> tmp;
62+
tmp.first=intervals[0][0];
63+
tmp.second=intervals[0][1];
64+
for(int i=1;i<intervals.size();i++){
65+
if(tmp.second>=intervals[i][0]){
66+
tmp.second=max(tmp.second, intervals[i][1]);
67+
} else{
68+
res.push_back({tmp.first, tmp.second});
69+
tmp.first=intervals[i][0];
70+
tmp.second=intervals[i][1];
71+
}
72+
}
73+
res.push_back({tmp.first, tmp.second}); // adding last interva;
74+
return res;
75+
}
76+
77+
int main(){
78+
ios_base::sync_with_stdio(false);
79+
cin.tie(NULL);
80+
cout.tie(NULL);
81+
int t;
82+
cin>>t;
83+
while(t--){
84+
int n;
85+
cin>>n;
86+
vector<vector<int> > intervals, res;
87+
for(int i=0;i<n;i++){
88+
int s, e;
89+
cin>>s>>e;
90+
intervals.push_back({s, e});
91+
}
92+
93+
res=merge(intervals);
94+
95+
for(int i=0;i<res.size();i++){
96+
cout<<res[i][0]<<" "<<res[i][1]<<" ";
97+
}
98+
cout<<endl;
99+
}
100+
101+
return 0;
102+
}

array/set_matrix_ones_in_O(1).cpp

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 (or true) then make all the cells of ith row and jth column as 1.
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases.
6+
The first line of each test case is r and c, r is the number of rows and c is the number of columns.
7+
The second line of each test case contains all the elements of the matrix in a single line separated by a single space.
8+
9+
Output:
10+
Print the modified array.
11+
12+
Constraints:
13+
1 ≤ T ≤ 100
14+
1 ≤ r, c ≤ 1000
15+
0 ≤ A[i][j] ≤ 1
16+
17+
Example:
18+
Input:
19+
3
20+
2 2
21+
1 0
22+
0 0
23+
2 3
24+
0 0 0
25+
0 0 1
26+
4 3
27+
1 0 0
28+
1 0 0
29+
1 0 0
30+
0 0 0
31+
32+
Output:
33+
1 1
34+
1 0
35+
0 0 1
36+
1 1 1
37+
1 1 1
38+
1 1 1
39+
1 0 0
40+
41+
Explanation:
42+
Testcase1: Since only first element of matrix has 1 (at index 1,1) as value, so first row and first column are modified to 1.
43+
*/
44+
45+
46+
47+
48+
49+
50+
51+
#include<bits/stdc++.h>
52+
using namespace std;
53+
54+
void setZeroes(vector<vector<int>>& matrix) {
55+
int m=matrix.size();
56+
int n=matrix[0].size();
57+
bool fr=false, fc=false; // check if first row or column contains a zero
58+
59+
for(int i=0;i<m;i++) // col 0
60+
if(matrix[i][0]==1) fc=true;
61+
62+
for(int j=0;j<n;j++) // row 0
63+
if(matrix[0][j]==1) fr=true;
64+
65+
for(int i=1;i<m;i++){
66+
for(int j=1;j<n;j++){
67+
if(matrix[i][j]==1){
68+
matrix[0][j]=1;
69+
matrix[i][0]=1;
70+
}
71+
}
72+
}
73+
74+
for(int i=1;i<m;i++){
75+
for(int j=1;j<n;j++){
76+
if(matrix[0][j]==1 || matrix[i][0]==1) matrix[i][j]=1;
77+
}
78+
}
79+
if(fr){
80+
for(int j=0;j<n;j++) matrix[0][j]=1;
81+
}
82+
if(fc){
83+
for(int i=0;i<m;i++) matrix[i][0]=1;
84+
}
85+
}
86+
87+
int main(){
88+
ios_base::sync_with_stdio(false);
89+
cin.tie(NULL);
90+
cout.tie(NULL);
91+
int t;
92+
cin>>t;
93+
while(t--){
94+
int r, c;
95+
cin>>r>>c;
96+
vector<vector<int> > matrix(r, vector<int> (c, 0));
97+
for(int i=0;i<r;i++){
98+
for(int j=0;j<c;j++){
99+
int ch; cin>>ch;
100+
matrix[i][j]=ch;
101+
}
102+
}
103+
104+
setZeroes(matrix);
105+
106+
for(int i=0;i<r;i++){
107+
for(int j=0;j<c;j++){
108+
cout<<matrix[i][j]<<" ";
109+
}
110+
cout<<endl;
111+
}
112+
}
113+
return 0;
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.
2+
3+
4+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
5+
6+
Example:
7+
8+
Input: 5
9+
Output:
10+
[
11+
[1],
12+
[1,1],
13+
[1,2,1],
14+
[1,3,3,1],
15+
[1,4,6,4,1]
16+
]
17+
18+
19+
20+
21+
22+
23+
24+
25+
class Solution {
26+
public:
27+
vector<vector<int>> generate(int n) {
28+
vector<vector<int> > res;
29+
if(n==0) return res;
30+
int tmp[n][n];
31+
for(int i=0;i<n;i++){
32+
for(int j=0;j<=i;j++){
33+
if(i==j || j==0)
34+
tmp[i][j]=1;
35+
else tmp[i][j]=tmp[i-1][j-1]+tmp[i-1][j];
36+
}
37+
}
38+
39+
for(int i=0;i<n;i++){
40+
vector<int> res1;
41+
for(int j=0;j<=i;j++){
42+
res1.push_back(tmp[i][j]);
43+
}
44+
res.push_back(res1);
45+
}
46+
return res;
47+
}
48+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.
2+
3+
Note that the row index starts from 0.
4+
5+
6+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
7+
8+
Example:
9+
10+
Input: 3
11+
Output: [1,3,3,1]
12+
Follow up:
13+
14+
Could you optimize your algorithm to use only O(k) extra space?
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
24+
int gcdd(int a, int b){
25+
if(b==0) return a;
26+
return gcdd(b, a%b);
27+
}
28+
29+
int NCR(int n, int r){
30+
if(n==0) return 1;
31+
if(r==0) return 1;
32+
if(n-r<r) r=n-r;
33+
long long p=1, k=1;
34+
while(r){
35+
p*=n;
36+
k*=r;
37+
int gcd=gcdd(p, k);
38+
p/=gcd;
39+
k/=gcd;
40+
n--;
41+
r--;
42+
}
43+
return p/k;
44+
}
45+
46+
vector<int> getRow(int n) { // works fine till n<=33
47+
vector<int> res;
48+
for(int i=0;i<=n;i++){
49+
int val=NCR(n, i);
50+
res.push_back(val);
51+
}
52+
return res;
53+
}
54+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
2+
3+
Note:
4+
5+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
7+
Example 1:
8+
9+
Input: [2,2,3,2]
10+
Output: 3
11+
Example 2:
12+
13+
Input: [0,1,0,1,0,1,99]
14+
Output: 99
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
class Solution {
25+
public:
26+
int singleNumber(vector<int>& nums) {
27+
unordered_map<int, int> m;
28+
for(auto x: nums)
29+
m[x]++;
30+
for(auto it=m.begin(); it!=m.end(); it++)
31+
if(it->second==1) return it->first;
32+
return -1; // result not found
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
2+
3+
Note:
4+
5+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
7+
Example 1:
8+
9+
Input: [2,2,3,2]
10+
Output: 3
11+
Example 2:
12+
13+
Input: [0,1,0,1,0,1,99]
14+
Output: 99
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
class Solution {
25+
public:
26+
int singleNumber(vector<int>& nums) {
27+
unordered_map<int, int> m;
28+
for(auto x: nums)
29+
m[x]++;
30+
for(auto it=m.begin(); it!=m.end(); it++)
31+
if(it->second==1) return it->first;
32+
return -1; // result not found
33+
}
34+
};

0 commit comments

Comments
 (0)