Skip to content

Commit 1da2a5f

Browse files
committed
🚀 02-Aug-2021
1 parent 25eb808 commit 1da2a5f

26 files changed

+1753
-1252
lines changed

SQL/175. Combine Two Tables.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Table: Person
2+
3+
+-------------+---------+
4+
| Column Name | Type |
5+
+-------------+---------+
6+
| PersonId | int |
7+
| FirstName | varchar |
8+
| LastName | varchar |
9+
+-------------+---------+
10+
PersonId is the primary key column for this table.
11+
Table: Address
12+
13+
+-------------+---------+
14+
| Column Name | Type |
15+
+-------------+---------+
16+
| AddressId | int |
17+
| PersonId | int |
18+
| City | varchar |
19+
| State | varchar |
20+
+-------------+---------+
21+
AddressId is the primary key column for this table.
22+
23+
24+
Write a SQL query for a report that provides the following information for each person in the Person table,
25+
regardless if there is an address for each of those people:
26+
27+
FirstName, LastName, City, State
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
SELECT FirstName, LastName, City, State
40+
FROM Person LEFT JOIN Address
41+
ON Person.PersonId = Address.PersonId;

SQL/175. Combine Two Tables.txt

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Table: Person
2+
3+
+-------------+---------+
4+
| Column Name | Type |
5+
+-------------+---------+
6+
| PersonId | int |
7+
| FirstName | varchar |
8+
| LastName | varchar |
9+
+-------------+---------+
10+
PersonId is the primary key column for this table.
11+
Table: Address
12+
13+
+-------------+---------+
14+
| Column Name | Type |
15+
+-------------+---------+
16+
| AddressId | int |
17+
| PersonId | int |
18+
| City | varchar |
19+
| State | varchar |
20+
+-------------+---------+
21+
AddressId is the primary key column for this table.
22+
23+
24+
Write a SQL query for a report that provides the following information for each person in the Person table,
25+
regardless if there is an address for each of those people:
26+
27+
FirstName, LastName, City, State
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
Solution:
38+
39+
Since the PersonId in table Address is the foreign key of table Person,
40+
we can join this two table to get the address information of a person.
41+
42+
Considering there might not be an address information for every person,
43+
we should use outer join instead of the default inner join.
44+
45+
Note: Using where clause to filter the records will fail if there is no address
46+
information for a person because it will not display the name information.
47+
48+
49+
SELECT FirstName, LastName, City, State
50+
FROM Person LEFT JOIN Address
51+
ON Person.PersonId = Address.PersonId;

algorithms/graph/topological_sorting.cpp renamed to algorithms/graph/topological_sorting_dfs.cpp

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
Given a Directed Graph. Find any Topological Sorting of that Graph.
33
44
Input:
5-
The first line of input takes the number of test cases then T test cases follow . Each test case contains two lines. The first line of each test case contains two integers E and V representing no of edges and the number of vertices. Then in the next line are E pairs of integers u, v representing an edge from u to v in the graph.
5+
The first line of input takes the number of test cases then T test cases follow .
6+
Each test case contains two lines. The first line of each test case contains two integers E and V
7+
representing no of edges and the number of vertices. Then in the next line are E pairs of integers u, v
8+
representing an edge from u to v in the graph.
69
710
Output:
811
For each test case output will be 1 if the topological sort is done correctly else it will be 0.
912
1013
Your Task:
11-
You don't need to read input or print anything. Your task is to complete the function topoSort() which takes the adjacency list of the Graph and the number of vertices (N) as inputs are returns an array consisting of a the vertices in Topological order. As there are multiple Topological orders possible, you may return any of them.
14+
You don't need to read input or print anything. Your task is to complete the function topoSort()
15+
which takes the adjacency list of the Graph and the number of vertices (N) as inputs are returns
16+
an array consisting of a the vertices in Topological order. As there are multiple Topological
17+
orders possible, you may return any of them.
1218
1319
Expected Time Complexity: O(V + E).
1420
Expected Auxiliary Space: O(V).
@@ -91,34 +97,51 @@ int main() {
9197
}// } Driver Code Ends
9298

9399

100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
94115
// The Graph structure is as folows
95116

96117
/* Function which sorts the graph vertices in topological form
97118
* N: number of vertices
98119
* adj[]: input graph
99120
*/
100121

101-
stack<int> s;
122+
stack <int> s;
102123

103124
void dfs(vector<int> adj[],int node, vector<bool> &vis){
104-
vis[node]=true;
105-
for(auto x: adj[node]){
125+
vis[node] = true;
126+
for (auto x: adj[node]){
106127
if(!vis[x])
107128
dfs(adj, x, vis);
108129
}
109130
s.push(node);
110131
}
111132

112-
vector<int> topoSort(int V, vector<int> adj[]) {
113-
vector<bool> vis(V, false);
114-
for(int i=0;i<V;i++){
115-
if(!vis[i])
133+
vector <int> topoSort(int V, vector<int> adj[]) {
134+
vector <bool> vis(V, false);
135+
for (int i = 0; i < V; i++){
136+
if (!vis[i])
116137
dfs(adj, i, vis);
117138
}
118-
vector<int>res;
119-
while(!s.empty()){
139+
vector <int> res;
140+
141+
while (!s.empty()){
120142
res.push_back(s.top());
121143
s.pop();
122144
}
145+
123146
return res;
124147
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Given a string s, partition s such that every substring of the partition
2+
is a palindrome. Return all possible palindrome partitioning of s.
3+
4+
A palindrome string is a string that reads the same backward as forward.
5+
6+
7+
8+
Example 1:
9+
10+
Input: s = "aab"
11+
Output: [["a","a","b"],["aa","b"]]
12+
Example 2:
13+
14+
Input: s = "a"
15+
Output: [["a"]]
16+
17+
18+
Constraints:
19+
20+
1 <= s.length <= 16
21+
s contains only lowercase English letters.
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
34+
bool isPalindrome(int start, int end, string &s) {
35+
while (start < end) {
36+
if (s[start++] != s[end--]) return false;
37+
}
38+
return true;
39+
}
40+
41+
void dfs(int start, string &s, vector<string> &curr, vector<vector<string>> &res) {
42+
if (start == s.length()) {
43+
res.push_back(curr);
44+
return;
45+
}
46+
47+
for (int i = start; i < s.length(); i++) {
48+
if (isPalindrome(start, i, s)) {
49+
curr.push_back(s.substr(start, i - start + 1));
50+
dfs(i + 1, s, curr, res);
51+
curr.pop_back(); // backtrack
52+
}
53+
}
54+
}
55+
56+
vector<vector<string>> partition(string s) {
57+
vector<vector<string>> res;
58+
vector<string> curr;
59+
60+
dfs(0, s, curr, res);
61+
62+
return res;
63+
}
64+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Let's call a number a binary decimal if it's a positive integer and all digits in its decimal notation are either 0 or 1. For example, 1010111 is a binary decimal, while 10201 and 787788 are not.
3+
4+
Given a number n, you are asked to represent n as a sum of some (not necessarily distinct) binary decimals. Compute the smallest number of binary decimals required for that.
5+
6+
Input
7+
The first line contains a single integer t (1=t=1000), denoting the number of test cases.
8+
9+
The only line of each test case contains a single integer n (1=n=109), denoting the number to be represented.
10+
11+
Output
12+
For each test case, output the smallest number of binary decimals required to represent n as a sum.
13+
14+
Example
15+
inputCopy
16+
3
17+
121
18+
5
19+
1000000000
20+
outputCopy
21+
2
22+
5
23+
1
24+
Note
25+
In the first test case, 121 can be represented as 121=110+11 or 121=111+10.
26+
27+
In the second test case, 5 can be represented as 5=1+1+1+1+1.
28+
29+
In the third test case, 1000000000 is a binary decimal itself, thus the answer is 1.
30+
*/
31+
32+
33+
34+
35+
36+
37+
#include<bits/stdc++.h>
38+
using namespace std;
39+
40+
void solve() {
41+
int n;
42+
cin >> n;
43+
44+
int res = 0;
45+
46+
while (n) {
47+
res = max(res, n % 10);
48+
n /= 10;
49+
}
50+
51+
cout << res << "\n";
52+
}
53+
54+
int main(){
55+
ios_base::sync_with_stdio(false);
56+
cin.tie(NULL);
57+
58+
int t = 1;
59+
cin >> t;
60+
61+
while (t--) solve();
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)