Skip to content

Commit 2aca8fb

Browse files
committed
2016-4-13 11:00:11
2 parents f6eee38 + 0b204e4 commit 2aca8fb

13 files changed

Lines changed: 730 additions & 111 deletions

Combinations.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <memory.h>
5+
#include <sstream>
6+
#include <iostream>
7+
#include <stack>
8+
#include <vector>
9+
#include <iterator>
10+
#include <numeric>
11+
12+
using namespace std;
13+
14+
class Solution{
15+
private:
16+
std::vector<std::vector<int> > ret;
17+
std::vector<int> a;
18+
public:
19+
void solve(int dep, int maxDep, int n, int start){
20+
if( dep == maxDep ){
21+
ret.push_back(a);
22+
return;
23+
}
24+
25+
for(int i = start; i <= n; ++i){
26+
a[dep] = i;
27+
solve(dep + 1, maxDep, n, i+1);
28+
}
29+
}
30+
31+
std::vector<std::vector<int> > combine(int n, int k ){
32+
a.resize(k);
33+
ret.clear();
34+
solve(0, k, n, 1);
35+
return ret;
36+
}
37+
38+
};
39+
40+
41+
int main(int argc, char const *argv[])
42+
{
43+
Solution s;
44+
return 0;
45+
}

CountofRangeSum.cc

Lines changed: 0 additions & 44 deletions
This file was deleted.

IncreasingTripletSubsequence.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <memory.h>
5+
#include <limits.h>
6+
7+
#include <sstream>
8+
#include <iostream>
9+
#include <stack>
10+
#include <vector>
11+
#include <iterator>
12+
#include <numeric>
13+
#include <algorithm>
14+
15+
using namespace std;
16+
17+
class Solution {
18+
public:
19+
bool increasingTriplet(vector<int>& nums) {
20+
int x1 = INT_MAX, x2 = INT_MAX;
21+
for(int num: nums){
22+
if(num <= x1) x1 = num;
23+
else if(num <=x2) x2 = num;
24+
else return true;
25+
}
26+
return false;
27+
}
28+
};
29+
30+
31+
int main(int argc, char const *argv[])
32+
{
33+
/* code */
34+
return 0;
35+
}

LargestBSTSubtree.cc

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <limits.h>
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <memory.h>
8+
#include <limits.h>
9+
10+
#include <sstream>
11+
#include <iostream>
12+
#include <stack>
13+
#include <vector>
14+
#include <iterator>
15+
#include <numeric>
16+
#include <algorithm>
17+
18+
/* A binary tree node has data, pointer to left child
19+
and a pointer to right child */
20+
struct node
21+
{
22+
int data;
23+
struct node* left;
24+
struct node* right;
25+
};
26+
27+
/* Helper function that allocates a new node with the
28+
given data and NULL left and right pointers. */
29+
struct node* newNode(int data)
30+
{
31+
struct node* node = (struct node*)
32+
malloc(sizeof(struct node));
33+
node->data = data;
34+
node->left = NULL;
35+
node->right = NULL;
36+
37+
return(node);
38+
}
39+
40+
int largestBSTUtil(struct node* node, int *min_ref, int *max_ref,
41+
int *max_size_ref, bool *is_bst_ref);
42+
43+
/* Returns size of the largest BST subtree in a Binary Tree
44+
(efficient version). */
45+
int largestBST(struct node* node)
46+
{
47+
// Set the initial values for calling largestBSTUtil()
48+
int min = INT_MAX; // For minimum value in right subtree
49+
int max = INT_MIN; // For maximum value in left subtree
50+
51+
int max_size = 0; // For size of the largest BST
52+
bool is_bst = 0;
53+
54+
largestBSTUtil(node, &min, &max, &max_size, &is_bst);
55+
56+
return max_size;
57+
}
58+
59+
/* largestBSTUtil() updates *max_size_ref for the size of the largest BST
60+
subtree. Also, if the tree rooted with node is non-empty and a BST,
61+
then returns size of the tree. Otherwise returns 0.*/
62+
int largestBSTUtil(struct node* node, int *min_ref, int *max_ref,
63+
int *max_size_ref, bool *is_bst_ref)
64+
{
65+
66+
/* Base Case */
67+
if (node == NULL)
68+
{
69+
*is_bst_ref = 1; // An empty tree is BST
70+
return 0; // Size of the BST is 0
71+
}
72+
73+
int min = INT_MAX;
74+
75+
/* A flag variable for left subtree property
76+
i.e., max(root->left) < root->data */
77+
bool left_flag = false;
78+
79+
/* A flag variable for right subtree property
80+
i.e., min(root->right) > root->data */
81+
bool right_flag = false;
82+
83+
int ls, rs; // To store sizes of left and right subtrees
84+
85+
/* Following tasks are done by recursive call for left subtree
86+
a) Get the maximum value in left subtree (Stored in *max_ref)
87+
b) Check whether Left Subtree is BST or not (Stored in *is_bst_ref)
88+
c) Get the size of maximum size BST in left subtree (updates *max_size) */
89+
*max_ref = INT_MIN;
90+
ls = largestBSTUtil(node->left, min_ref, max_ref, max_size_ref, is_bst_ref);
91+
if (*is_bst_ref == 1 && node->data > *max_ref)
92+
left_flag = true;
93+
94+
/* Before updating *min_ref, store the min value in left subtree. So that we
95+
have the correct minimum value for this subtree */
96+
min = *min_ref;
97+
98+
/* The following recursive call does similar (similar to left subtree)
99+
task for right subtree */
100+
*min_ref = INT_MAX;
101+
rs = largestBSTUtil(node->right, min_ref, max_ref, max_size_ref, is_bst_ref);
102+
if (*is_bst_ref == 1 && node->data < *min_ref)
103+
right_flag = true;
104+
105+
// Update min and max values for the parent recursive calls
106+
if (min < *min_ref) *min_ref = min;
107+
if (node->data < *min_ref) // For leaf nodes
108+
*min_ref = node->data;
109+
if (node->data > *max_ref)
110+
*max_ref = node->data;
111+
112+
/* If both left and right subtrees are BST. And left and right
113+
subtree properties hold for this node, then this tree is BST.
114+
So return the size of this tree */
115+
if(left_flag && right_flag)
116+
{
117+
if (ls + rs + 1 > *max_size_ref)
118+
*max_size_ref = ls + rs + 1;
119+
return ls + rs + 1;
120+
}
121+
else
122+
{
123+
//Since this subtree is not BST, set is_bst flag for parent calls
124+
*is_bst_ref = 0;
125+
return 0;
126+
}
127+
}
128+
129+
/* Driver program to test above functions*/
130+
int main()
131+
{
132+
/* Let us construct the following Tree
133+
50
134+
/ \
135+
10 60
136+
/ \ / \
137+
5 20 55 70
138+
/ / \
139+
45 65 80
140+
*/
141+
142+
struct node *root = newNode(50);
143+
root->left = newNode(10);
144+
root->right = newNode(60);
145+
root->left->left = newNode(5);
146+
root->left->right = newNode(20);
147+
root->right->left = newNode(55);
148+
root->right->left->left = newNode(45);
149+
root->right->right = newNode(70);
150+
root->right->right->left = newNode(65);
151+
root->right->right->right = newNode(80);
152+
153+
/* The complete tree is not BST as 45 is in right subtree of 50.
154+
The following subtree is the largest BST
155+
60
156+
/ \
157+
55 70
158+
/ / \
159+
45 65 80
160+
*/
161+
printf(" Size of the largest BST is %d", largestBST(root));
162+
163+
getchar();
164+
return 0;
165+
}
166+

MaximumSizeSubarraySumEqualsk.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <limits.h>
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <memory.h>
8+
#include <limits.h>
9+
10+
#include <sstream>
11+
#include <iostream>
12+
#include <stack>
13+
#include <vector>
14+
#include <iterator>
15+
#include <numeric>
16+
#include <algorithm>
17+
#include <unordered_map>
18+
using namespace std;
19+
20+
class Solution {
21+
public:
22+
int maxSubArrayLen(vector<int>& nums, int k) {
23+
int size = nums.size();
24+
unordered_map<int, int> tool;
25+
tool[0] = -1;
26+
int sum = 0, maxi = 0;
27+
for(int e = 0; e < size; ++e)
28+
{
29+
sum += nums[e];
30+
int finder = sum-k;
31+
if(tool.find(finder) != tool.end())
32+
{
33+
int preIndex = tool[finder];
34+
maxi = maxi<e-preIndex?e-preIndex:maxi;
35+
}
36+
if(tool.find(sum) == tool.end())
37+
tool[sum] = e;
38+
}
39+
return maxi;
40+
}
41+
};
42+
43+
44+
45+
46+
int main(int argc, char const *argv[])
47+
{
48+
Solution s;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)