Skip to content

Commit 0fb63a3

Browse files
committed
More solutions added
1 parent 55dc8a3 commit 0fb63a3

24 files changed

+760
-5
lines changed

Clone_Graph.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Definition for undirected graph.
3+
* struct UndirectedGraphNode {
4+
* int label;
5+
* vector<UndirectedGraphNode *> neighbors;
6+
* UndirectedGraphNode(int x) : label(x) {};
7+
* };
8+
*
9+
*
10+
* Solution: this clone is based on the edge instead of the node.
11+
* This way will make sure the edge will be copied to the
12+
* cloned graph as well.
13+
*
14+
*/
15+
class Solution {
16+
public:
17+
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
18+
if(node == nullptr) return nullptr;
19+
20+
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> map;
21+
stack<UndirectedGraphNode *> st;
22+
UndirectedGraphNode *t = nullptr;
23+
UndirectedGraphNode *nGraph = new UndirectedGraphNode(node->label);
24+
map[node] = nGraph;
25+
26+
st.push(node);
27+
while(!st.empty()){
28+
t = st.top();
29+
st.pop();
30+
31+
for(int i = 0; i < t->neighbors.size(); i++){
32+
UndirectedGraphNode *nb = t->neighbors[i];
33+
if(map.find(nb) == map.end()){
34+
UndirectedGraphNode *x = new UndirectedGraphNode(nb->label);
35+
map[nb] = x;
36+
map[t]->neighbors.push_back(x);
37+
st.push(nb);
38+
}else{
39+
map[t]->neighbors.push_back(map[nb]);
40+
}
41+
}
42+
}
43+
44+
return nGraph;
45+
}
46+
};
47+
48+
49+
/**
50+
* Definition for undirected graph.
51+
* struct UndirectedGraphNode {
52+
* int label;
53+
* vector<UndirectedGraphNode *> neighbors;
54+
* UndirectedGraphNode(int x) : label(x) {};
55+
* };
56+
*
57+
*
58+
* BFS based solution.
59+
* It is almost identical to the DFS solution except the order of edge copying is different from DFS.
60+
* DFS will make go deeper and deeper, while BFS will copy edges layer by layer.
61+
*/
62+
class Solution {
63+
public:
64+
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
65+
if(node == nullptr) return nullptr;
66+
67+
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> map;
68+
queue<UndirectedGraphNode *> st;
69+
UndirectedGraphNode *t = nullptr;
70+
UndirectedGraphNode *nGraph = new UndirectedGraphNode(node->label);
71+
map[node] = nGraph;
72+
73+
st.push(node);
74+
while(!st.empty()){
75+
t = st.front();
76+
st.pop();
77+
78+
for(int i = 0; i < t->neighbors.size(); i++){
79+
UndirectedGraphNode *nb = t->neighbors[i];
80+
if(map.find(nb) == map.end()){
81+
UndirectedGraphNode *x = new UndirectedGraphNode(nb->label);
82+
map[nb] = x;
83+
map[t]->neighbors.push_back(x);
84+
st.push(nb);
85+
}else{
86+
map[t]->neighbors.push_back(map[nb]);
87+
}
88+
}
89+
}
90+
91+
return nGraph;
92+
}
93+
};

Maximum_Subarray.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(int A[], int n) {
4+
int sum = 0;
5+
int max_sum = A[0]; // contain at least one element
6+
7+
for(int i = 0; i < n ; i++){
8+
sum += A[i];
9+
// make sure every elememt is compared
10+
if(sum > max_sum){
11+
max_sum = sum;
12+
}
13+
14+
if(sum < 0){
15+
sum = 0;
16+
}
17+
}
18+
return max_sum;
19+
}
20+
};
21+
22+
23+
/* Divide and conquer solution */
24+
25+
class Solution {
26+
public:
27+
int maxSubArray(int A[], int n) {
28+
29+
int max_sum;
30+
max_sum = max_sub_helper(A, 0, n);
31+
32+
return max_sum;
33+
}
34+
private:
35+
36+
int max_sub_helper(int A[], int sInd, int eInd){
37+
if (sInd+1 == eInd) return A[sInd]; // one elem array
38+
if (sInd >= eInd) return INT_MIN; // empty array
39+
40+
int l_max, r_max, c_max;
41+
int mInd;
42+
43+
mInd = sInd + (eInd-sInd)/2;
44+
45+
l_max = max_sub_helper(A, sInd, mInd);
46+
r_max = max_sub_helper(A, mInd+1, eInd);
47+
48+
49+
c_max = A[mInd];
50+
51+
int prefix_sum = 0;
52+
int prefix_max = INT_MIN;
53+
54+
for (int i = mInd+1; i < eInd; i++) {
55+
prefix_sum += A[i];
56+
if (prefix_sum > prefix_max) prefix_max = prefix_sum;
57+
}
58+
59+
if (prefix_max > 0) {
60+
c_max += prefix_max;
61+
}
62+
63+
int suffix_sum = 0;
64+
int suffix_max = INT_MIN;
65+
for (int i = mInd-1; i >= 0; i--) {
66+
suffix_sum += A[i];
67+
if (suffix_sum > suffix_max) suffix_max = suffix_sum;
68+
}
69+
70+
if (suffix_max > 0) {
71+
c_max += suffix_max;
72+
}
73+
74+
return max(max(l_max, r_max), c_max);
75+
}
76+
};

Merge_Sorted_Array.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
void merge(int A[], int m, int B[], int n) {
4+
int k = m+n-1;
5+
int i = m-1;
6+
int j = n-1;
7+
8+
while(i >= 0 && j >= 0){
9+
if(A[i] > B[j]){
10+
A[k--] = A[i--];
11+
}else{
12+
A[k--] = B[j--];
13+
}
14+
}
15+
16+
while(j >= 0){
17+
A[k--] = B[j--];
18+
}
19+
}
20+
};

Merge_Two_Sorted_Lists.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
12+
ListNode dummy(0);
13+
ListNode *d = &dummy;
14+
15+
while(l1 && l2){
16+
if(l1->val < l2->val){
17+
d->next = l1;
18+
d = d->next;
19+
l1 = l1->next;
20+
}else{
21+
d->next = l2;
22+
d = d->next;
23+
l2 = l2->next;
24+
}
25+
}
26+
27+
if(l1) d->next = l1;
28+
if(l2) d->next = l2;
29+
30+
return dummy.next;
31+
}
32+
};

Palindrome_Partitioning.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public:
3+
vector<vector<string>> partition(string s) {
4+
if(s == "") return vector<vector<string>>();
5+
6+
vector<vector<string>> result;
7+
vector<string> t;
8+
partition_helper(result, t, s);
9+
10+
return result;
11+
}
12+
13+
private:
14+
void partition_helper(vector<vector<string>> &result, vector<string> &sofar, string rest){
15+
if(rest.empty()){
16+
result.push_back(sofar);
17+
return;
18+
}
19+
20+
string w;
21+
for(int i = 0; i < rest.size(); i++){
22+
w = rest.substr(0, i+1);
23+
if(is_pal(w)){
24+
sofar.push_back(w);
25+
partition_helper(result, sofar, rest.substr(i+1));
26+
sofar.pop_back();
27+
}
28+
}
29+
}
30+
31+
bool is_pal(string &st){
32+
int i = 0;
33+
int j = st.size()-1;
34+
35+
while(i < j){
36+
if(st[i] != st[j]) return false;
37+
i++;
38+
j--;
39+
}
40+
return true;
41+
}
42+
};
43+

Permutations.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Solution {
1313

1414
private:
1515
// sofar and rest can be passed by reference to avoid unnecessary
16-
// space allocation
1716
void permute_helper(vector<vector<int>> &result, vector<int> &sofar, vector<int> &rest){
1817
if(rest.empty()){
1918
result.push_back(sofar);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* struct TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode *left, *right, *next;
6+
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
void connect(TreeLinkNode *root) {
12+
if(root == nullptr) return;
13+
if(root->left == nullptr && root->right == nullptr) return;
14+
15+
root->left->next = root->right;
16+
if(root->next){
17+
root->right->next = root->next->left;
18+
}
19+
connect(root->left);
20+
connect(root->right);
21+
}
22+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* struct TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode *left, *right, *next;
6+
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
void connect(TreeLinkNode *root) {
12+
if(root == nullptr) return;
13+
if(root->left == nullptr && root->right == nullptr) return;
14+
15+
queue<TreeLinkNode *> cq, nq;
16+
cq.push(root);
17+
18+
TreeLinkNode *t = nullptr;
19+
while(!cq.empty()){
20+
t = cq.front();
21+
cq.pop();
22+
23+
if(t->left) nq.push(t->left);
24+
if(t->right) nq.push(t->right);
25+
26+
if(!cq.empty()) t->next = cq.front();
27+
else{
28+
swap(cq, nq);
29+
}
30+
}
31+
}
32+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(int A[], int n) {
4+
if(n <= 1) return n;
5+
6+
int i = 0;
7+
int j = 1;
8+
9+
while(j < n){
10+
if(A[i] == A[j]){
11+
j++;
12+
}else{
13+
A[++i] = A[j++];
14+
}
15+
}
16+
return i+1;
17+
}
18+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(int A[], int n) {
4+
if(n <= 2) return n;
5+
6+
int p = 0;
7+
int q = p+1;
8+
int cnt = 1;
9+
10+
while(q < n){
11+
if(A[p] == A[q]){
12+
cnt++;
13+
if(cnt == 2){
14+
A[++p] = A[q++];
15+
}else q++;
16+
}else{
17+
A[++p] = A[q++];
18+
cnt = 1;
19+
}
20+
}
21+
22+
return p+1;
23+
}
24+
};

0 commit comments

Comments
 (0)