Skip to content

Commit 963ef6f

Browse files
committed
Practise 18-Jul-2020
1 parent 9a46bbc commit 963ef6f

9 files changed

+654
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Given an array A of size N, construct a Product Array P (of same size) such that P is equal to the product of all the elements of A except A[i].
3+
4+
Input:
5+
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input. The first line is N. The second line contains N elements separated by spaces.
6+
7+
Output:
8+
For each testcase, print the Product array P.
9+
10+
Constraints:
11+
1 <= T <= 10
12+
1 <= N <= 1000
13+
1 <= Ai <= 20
14+
15+
Example:
16+
Input:
17+
2
18+
5
19+
10 3 5 6 2
20+
2
21+
12 20
22+
Output:
23+
180 600 360 300 900
24+
20 12
25+
26+
Explanation:
27+
Testcase1: For the product array P, at i=0 we have 3*5*6*2. At i=1 10*5*6*2. At i=2 we have 10*3*6*2. At i=3 we have 10*3*5*2. At i=4 we have 10*3*5*6
28+
So P is 180 600 360 300 900
29+
*/
30+
31+
32+
33+
34+
35+
#include<bits/stdc++.h>
36+
using namespace std;
37+
38+
int main(){
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
cout.tie(NULL);
42+
int t;
43+
cin>>t;
44+
while(t--){
45+
int n;
46+
cin>>n;
47+
int a[n];
48+
for(int i=0;i<n;i++) cin>>a[i];
49+
int left[n], right[n], res[n];
50+
int prod=1;
51+
left[0]=1;
52+
right[n-1]=1;
53+
54+
for(int i=1;i<n;i++)
55+
left[i]=a[i-1]*left[i-1];
56+
57+
for(int i=n-2;i>=0;i--)
58+
right[i]=a[i+1]*right[i+1];
59+
60+
res[0]=right[0];
61+
res[n-1]=left[n-1];
62+
for(int i=1;i<n-1;i++)
63+
res[i]=left[i]*right[i];
64+
65+
for(int i=0;i<n;i++)
66+
cout<<res[i]<<" ";
67+
cout<<endl;
68+
}
69+
70+
return 0;
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
2+
3+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
4+
5+
Example:
6+
7+
X X X X
8+
X O O X
9+
X X O X
10+
X O X X
11+
After running your function, the board should be:
12+
13+
X X X X
14+
X X X X
15+
X X X X
16+
X O X X
17+
Explanation:
18+
19+
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
32+
void dfs(vector<vector<char> >& board, int x, int y){
33+
if(x>=0 && x< board.size() && y>=0 && y< board[0].size() && board[x][y]=='O'){
34+
board[x][y]='P';
35+
dfs(board,x-1,y);
36+
dfs(board,x+1,y);
37+
dfs(board,x,y-1);
38+
dfs(board,x,y+1);
39+
} else return;
40+
}
41+
42+
void solve(vector<vector<char>>& board) {
43+
int rows=board.size();
44+
if(rows==0) return;
45+
int cols=board[0].size();
46+
if(cols==0) return;
47+
48+
for(int i=0; i<cols;i++){
49+
if(board[0][i]=='O') // first row
50+
dfs(board, 0, i);
51+
if(board[rows-1][i]=='O') // last row
52+
dfs(board, rows-1, i);
53+
}
54+
55+
for(int i=0; i<rows;i++){
56+
if(board[i][0]=='O') // first col
57+
dfs(board, i, 0);
58+
if(board[i][cols-1]=='O') // last col
59+
dfs(board, i, cols-1);
60+
}
61+
62+
for(int i=0;i<rows;i++){
63+
for(int j=0;j<cols;j++){
64+
if(board[i][j]=='O') board[i][j]='X';
65+
if(board[i][j]=='P') board[i][j]='O';
66+
67+
}
68+
}
69+
}
70+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
2+
3+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
4+
5+
Example:
6+
7+
X X X X
8+
X O O X
9+
X X O X
10+
X O X X
11+
After running your function, the board should be:
12+
13+
X X X X
14+
X X X X
15+
X X X X
16+
X O X X
17+
Explanation:
18+
19+
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
32+
void dfs(vector<vector<char> >& board, int x, int y){
33+
if(x>=0 && x< board.size() && y>=0 && y< board[0].size() && board[x][y]=='O'){
34+
board[x][y]='P';
35+
dfs(board,x-1,y);
36+
dfs(board,x+1,y);
37+
dfs(board,x,y-1);
38+
dfs(board,x,y+1);
39+
} else return;
40+
}
41+
42+
void solve(vector<vector<char>>& board) {
43+
int rows=board.size();
44+
if(rows==0) return;
45+
int cols=board[0].size();
46+
if(cols==0) return;
47+
48+
for(int i=0; i<cols;i++){
49+
if(board[0][i]=='O') // first row
50+
dfs(board, 0, i);
51+
if(board[rows-1][i]=='O') // last row
52+
dfs(board, rows-1, i);
53+
}
54+
55+
for(int i=0; i<rows;i++){
56+
if(board[i][0]=='O') // first col
57+
dfs(board, i, 0);
58+
if(board[i][cols-1]=='O') // last col
59+
dfs(board, i, cols-1);
60+
}
61+
62+
for(int i=0;i<rows;i++){
63+
for(int j=0;j<cols;j++){
64+
if(board[i][j]=='O') board[i][j]='X';
65+
if(board[i][j]=='P') board[i][j]='O';
66+
67+
}
68+
}
69+
}
70+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
There are a total of numCourses courses you have to take, labeled from 0 to numCourses-1.
2+
3+
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
4+
5+
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
6+
7+
8+
9+
Example 1:
10+
11+
Input: numCourses = 2, prerequisites = [[1,0]]
12+
Output: true
13+
Explanation: There are a total of 2 courses to take.
14+
To take course 1 you should have finished course 0. So it is possible.
15+
Example 2:
16+
17+
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
18+
Output: false
19+
Explanation: There are a total of 2 courses to take.
20+
To take course 1 you should have finished course 0, and to take course 0 you should
21+
also have finished course 1. So it is impossible.
22+
23+
24+
Constraints:
25+
26+
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
27+
You may assume that there are no duplicate edges in the input prerequisites.
28+
1 <= numCourses <= 10^5
29+
30+
31+
32+
33+
34+
class Solution {
35+
public:
36+
37+
bool dfs(vector<int> adj[], vector<bool> &vis, vector<bool> &rec, int node){
38+
if(vis[node]==false){
39+
vis[node]=true;
40+
rec[node]=true;
41+
for(auto it: adj[node]){
42+
if(!vis[it] && dfs(adj, vis, rec, it)) return true;
43+
else if(rec[it]==true) return true;
44+
}
45+
}
46+
rec[node]=false;
47+
return false;
48+
}
49+
50+
bool canFinish(int numCourses, vector<vector<int>>& preReq) {
51+
vector<int> adj[numCourses];
52+
for(int i=0;i<preReq.size();i++){
53+
adj[preReq[i][0]].push_back(preReq[i][1]);
54+
}
55+
vector<bool> vis(numCourses, false);
56+
vector<bool> rec(numCourses, false);
57+
for(int i=0;i<numCourses;i++){
58+
if(!vis[i])
59+
if(dfs(adj, vis, rec, i)){
60+
// cycle exists, coureses cannot be taken
61+
return false;
62+
}
63+
}
64+
// cycle doesn't exist, courses can be taken
65+
return true;
66+
67+
}
68+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
2+
3+
If possible, output any possible result. If not possible, return the empty string.
4+
5+
Example 1:
6+
7+
Input: S = "aab"
8+
Output: "aba"
9+
Example 2:
10+
11+
Input: S = "aaab"
12+
Output: ""
13+
Note:
14+
15+
S will consist of lowercase letters and have length in range [1, 500].
16+
17+
18+
19+
20+
21+
22+
23+
class Solution {
24+
public:
25+
string reorganizeString(string s) {
26+
vector<int> freq(26, 0);
27+
int n=s.length();
28+
for(int i=0;i<n;i++){
29+
freq[s[i]-'a']++;
30+
}
31+
32+
priority_queue<pair<int, char> > pq; // max heap
33+
for(int i=0;i<26;i++){
34+
if(freq[i]>0){
35+
pq.push({freq[i], char(i+97)});
36+
}
37+
}
38+
39+
40+
string res;
41+
while(pq.size()>1){
42+
pair<int, char> cur, next;
43+
cur=pq.top();
44+
pq.pop();
45+
next=pq.top();
46+
pq.pop();
47+
res+=cur.second;
48+
res+=next.second;
49+
cur.first-=1;
50+
next.first-=1;
51+
if(cur.first>0) pq.push(cur);
52+
if(next.first>0) pq.push(next);
53+
}
54+
55+
if(!pq.empty()){
56+
pair<int , char> last= pq.top();
57+
if(last.first>1) return "";
58+
else res+=last.second;
59+
}
60+
return res;
61+
}
62+
};

0 commit comments

Comments
 (0)