Skip to content

Commit 24c7b38

Browse files
committed
hyperdrive before interviews
1 parent a983348 commit 24c7b38

6 files changed

+224
-0
lines changed

Diff for: 153. Find Minimum in Rotated Sorted Array.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int findMin(vector<int>& nums) {
4+
5+
if(nums.empty())
6+
return -1;
7+
8+
int low = 0;
9+
int high = nums.size()-1;
10+
11+
while(low < high) {
12+
int mid = low + (high-low)/2;
13+
14+
if(nums[low] <= nums[mid] && nums[mid] >= nums[high])
15+
low = mid+1;
16+
17+
else
18+
high = mid;
19+
20+
}
21+
22+
return nums[low];
23+
24+
}
25+
};

Diff for: 209. Minimum Size Subarray Sum.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
int minSubArrayLen(int s, vector<int>& nums) {
4+
5+
int n = nums.size();
6+
7+
if(!n)
8+
return 0;
9+
10+
int start = 0;
11+
int end = 0;
12+
int sum = 0;
13+
int minlen = INT_MAX;
14+
15+
while(start < n) {
16+
17+
if (sum < s) {
18+
sum += nums[start];
19+
start++;
20+
}
21+
22+
while (sum >=s) {
23+
minlen = min(minlen, start-end);
24+
sum -= nums[end];
25+
end++;
26+
}
27+
28+
}
29+
30+
if(minlen == INT_MAX)
31+
minlen = 0;
32+
33+
return minlen;
34+
35+
36+
37+
}
38+
};

Diff for: 252. Meeting Rooms.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for an interval.
3+
* struct Interval {
4+
* int start;
5+
* int end;
6+
* Interval() : start(0), end(0) {}
7+
* Interval(int s, int e) : start(s), end(e) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool canAttendMeetings(vector<Interval>& intervals) {
13+
14+
if (intervals.empty())
15+
return true;
16+
17+
sort(intervals.begin(), intervals.end(), [] (Interval &i1, Interval &i2 ) { return i1.start < i2.start;});
18+
19+
for(int i = 0; i<intervals.size()-1; ++i) {
20+
21+
if(intervals[i].end > intervals[i+1].start)
22+
return false;
23+
24+
}
25+
26+
return true;
27+
28+
29+
}
30+
};

Diff for: 285. Inorder Successor in BST.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
12+
void succ(TreeNode *root, TreeNode *p, TreeNode* &s) {
13+
14+
if (!root)
15+
return;
16+
17+
if (p->val < root->val) {
18+
s = root;
19+
succ(root->left, p, s);
20+
}
21+
22+
else
23+
succ(root->right, p, s);
24+
25+
return;
26+
}
27+
28+
29+
30+
public:
31+
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
32+
33+
TreeNode *s = nullptr;
34+
35+
if (p->right) {
36+
37+
s = p->right;
38+
39+
while (s->left)
40+
s = s->left;
41+
42+
return s;
43+
}
44+
45+
else
46+
succ(root, p, s);
47+
48+
return s;
49+
50+
}
51+
};

Diff for: 314. Binary Tree Vertical Order Traversal.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
12+
public:
13+
vector<vector<int>> verticalOrder(TreeNode* root) {
14+
15+
vector<vector<int> > result;
16+
17+
if(!root)
18+
return result;
19+
20+
map<int,vector<int> > m;
21+
22+
queue<pair<TreeNode *,int> > q;
23+
24+
q.push(make_pair(root, 0));
25+
26+
while(!q.empty()) {
27+
28+
auto top = q.front();
29+
q.pop();
30+
31+
if(m.find(top.second) == m.end())
32+
m.insert(make_pair(top.second, vector<int> ()));
33+
34+
m[top.second].push_back(top.first->val);
35+
36+
if(top.first->left)
37+
q.push(make_pair(top.first->left, top.second-1));
38+
39+
if(top.first->right)
40+
q.push(make_pair(top.first->right, top.second+1));
41+
42+
}
43+
44+
for(auto vec: m) {
45+
result.push_back(vector<int> ());
46+
result.back() = vec.second;
47+
}
48+
49+
return result;
50+
51+
}
52+
};

Diff for: 325. Maximum Size Subarray Sum Equals k.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int maxSubArrayLen(vector<int>& nums, int k) {
4+
5+
unordered_map<int, int> m;
6+
int maxlen = 0;
7+
int cursum = 0;
8+
9+
for(int i = 0;i<nums.size();++i) {
10+
11+
cursum += nums[i];
12+
13+
if(cursum == k)
14+
maxlen = max(maxlen, i+1);
15+
16+
if (m.find(cursum-k)!=m.end())
17+
maxlen = max(maxlen, i-m[cursum-k]);
18+
19+
else
20+
m.insert(make_pair(cursum, i));
21+
22+
}
23+
24+
return maxlen;
25+
26+
27+
}
28+
};

0 commit comments

Comments
 (0)