Almost same with Binary Tree Level Order Traversal
I used the standard DFS solution:
It can be easily realized based on tree methods in my previous article: http://codinggamestart.blogspot.com/2015/06/leetcode-binary-tree-level-order.html
for BFS with queue: change result.push_back(v) to result.insert(result.begin(), v);
for BFS with two queues: same with above;
for DFS the tricky solution: way 1 is to change it to standard solution like below
way 2 is to change push_back to insert, and notice the index change, become not straightforward.
Dynamic programming. Let rec[i] keep recording the maxim money with (i+1) houses.
Key is rec[i]=max(rec[i-1],rec[i-2]+nums[i]). TIP: Make sure allocate memory for vector, or in leetcode test will display runtime error which equals to core dump in c++ compiler. Debugging for too long time, not happy...
Key is how to test the number loops endlessly. Test loop by a hashmap recording all sums appeared. If same sum appear again, it's easy to see the sum of the squares of its digits will loop.
At first, I tried for loop to remove, but the logics become a mess. Maybe try for loop in the future.
Difficulty: how to deal with head point; Considering one node, two node, and last node.
In Contains Duplicate problem, we just need to judge whether duplication exist, so set is enough.
Here, we need to search number to get index, so unordered_map(hashmap) will be first choice.
For each loop, if current number is in hashmap, this number appeared before. We calculate the index distance. If distance less or equal to k, return true. If distance is larger than k, we need to update index information in hashmap for current number.
Here is a point, there may be more than just two identical numbers in the array. So we need to update. If current number appears in the future, the future position to position now will be closer than to old position. So we just need to record the current position (corresponding to current number in Hashmap)
Complexity: O(nums.size)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
public:bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int,int> Map;
for(int i=0;i<nums.size();i++){
if(Map.find(nums[i])!=Map.end()) {
int d=i-Map[nums[i]];
if (d<=k) returntrue;
else Map[nums[i]]=i;
}
else Map[nums[i]]=i;
}
returnfalse;
}
};
Difficulty: how to get dist_w/h.
At first I just got this distance with a difference from points ==!
dist_w/h ==> the distance between two farthest horizontal/vertical points of two rectangles
rep_w/h ==>repetition distance of width and height
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
public:int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int width1=abs(A-C);
int width2=abs(E-G);
int height1=abs(B-D);
int height2=abs(F-H);
int dist_w= ((C>G)?C:G) - ((A<E)?A:E) ;
int dist_h= ((D>H)?D:H) - ((B<F)?B:F);
int rep_w=abs(width1+width2-dist_w);
int rep_h=abs(height1+height2-dist_h);
int area_sum=width1*height1+width2*height2;
if(dist_w<(width1+width2) && dist_h<(height1+height2))
return area_sum-rep_w*rep_h;
return area_sum;
}
};
This problem is solve with key-value structure.
The smap stored key from char in s.
The tmap stroed key from char in t. The effect of tmap is just to make sure that values in tmap are not repeated. So I used a set replace the tmap. ( annotations are how to use tmap)
If changing map and set to unordered, speed will be better.
From the leetcode performance, there should be quicker way...
HashMap(unordered_set): n*O(1)
Tip: map element search O(log n), advantage compared to unordered_map: map is ordered
unordered_set has a convenient function count(): return 1 if key exists, 0 if not. official definition: Count elements with a specific key
Searches the container for elements whose key is k and returns the number of elements found. Because unordered_map containers do not allow for duplicate keys, this means that the function actually returns 1 if an element with that key exists in the container, and zero otherwise.
Container adaptor and underlying container: stack, queue and priority_queue are implemented as container adaptors. Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes (such as dequeor list) to handle the elements. The underlying container is encapsulated in such a way that its elements are accessed by the members of the container adaptor independently of the underlying container class used. http://www.cplusplus.com/reference/stl/
struct DATA{
TreeNode* node;
int level;
DATA(TreeNode* n, int l):node(n),level(l){}
};
classSolution {
public:int minDepth(TreeNode* root) {
queue< DATA > que;
if(!root) return0;
que.push(DATA(root,1));
while(!que.empty()){
DATA current=que.front();//in java, pop() will return element, in c++, not
que.pop();
if(!current.node->left &&!current.node->right) return current.level;
if(current.node->left)
//not sure whether DATA() is right for initialization an object
que.push(DATA(current.node->left, current.level+1));
if(current.node->right)
que.push(DATA(current.node->right, current.level+1));
}
}
};