Skip to content

Commit 4bb8968

Browse files
committed
Jump Game II
1 parent 769edb9 commit 4bb8968

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

1.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Description:
55
Shanbo Cheng: [email protected]
66
Date: 2016-07-06 15:00:26
7-
Last modified: 2016-07-06 16:43:26
7+
Last modified: 2016-08-02 08:57:05
88
GCC version: 4.7.3
99
*/
1010

10.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Description:
55
Shanbo Cheng: [email protected]
66
Date: 2016-07-19 20:12:52
7-
Last modified: 2016-07-19 20:27:38
7+
Last modified: 2016-08-02 10:44:08
88
GCC version: 4.9.3
99
***********************************************************/
1010

383.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//coding:utf-8
2+
/*****************************************
3+
Program: 383 ransom note
4+
Description:
5+
6+
Date: 2016-08-11 12:13:40
7+
Last modified: 2016-08-11 12:23:04
8+
GCC version: 4.9.3
9+
*****************************************/
10+
11+
#include <unordered_map>
12+
class Solution {
13+
unordered_map<char, int> count;
14+
public:
15+
bool canConstruct(string ransomNote, string magazine) {
16+
if(ransomNote.empty())
17+
return true;
18+
if(magazine.empty())
19+
return false;
20+
for(auto c: magazine)
21+
count[c]++;
22+
for(auto c: ransomNote) {
23+
if(count.find(c) == count.end() || count[c] == 0)
24+
return false;
25+
count[c]--;
26+
}
27+
return true;
28+
}
29+
};

39.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Program: Combination Sum
44
Description:
55
Shanbo Cheng: [email protected]
66
Date: 2016-07-29 15:33:14
7-
Last modified: 2016-07-29 15:55:15
7+
Last modified: 2016-08-11 12:27:13
88
GCC version: 4.9.3
99
***********************************************************/
1010

@@ -14,6 +14,7 @@ GCC version: 4.9.3
1414

1515
using namespace std;
1616

17+
//standard backtracking solution
1718
class Solution {
1819
void helper(vector<int>& candidates, vector<vector<int>>& ret, vector<int>& one, int num, int index) {
1920
if(num == 0 || index == candidates.size()) {

45.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//coding:utf-8
2+
/*****************************************
3+
Program: Jump Game II
4+
Description:
5+
6+
Date: 2016-08-17 15:58:04
7+
Last modified: 2016-08-17 16:15:04
8+
GCC version: 4.9.3
9+
*****************************************/
10+
11+
#include <vector>
12+
#include <iostream>
13+
using namespace std;
14+
15+
class Solution {
16+
public:
17+
int jump(vector<int>& nums) {
18+
if(nums.size() <= 1)
19+
return 0;
20+
int i = 0, temp = 0, level = 0, maxJump = 0;
21+
while(maxJump < nums.size()) {
22+
level++;
23+
for(; i <= temp; ++i) {
24+
maxJump = max(maxJump, nums[i] + i);
25+
if(maxJump >= nums.size() - 1)
26+
return level;
27+
}
28+
temp = maxJump;
29+
}
30+
return level - 1;
31+
}
32+
};
33+
34+
int main() {
35+
vector<int> vec{2, 3, 1, 1, 3, 1};
36+
Solution s;
37+
cout << s.jump(vec) << endl;
38+
}

0 commit comments

Comments
 (0)