Skip to content

Commit bb5fa22

Browse files
committed
More solutions to leetcode
1 parent 9dccd1e commit bb5fa22

19 files changed

+905
-0
lines changed

Combination_Sum.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Time Limit Exceeded */
2+
class Solution {
3+
public:
4+
vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
5+
vector<vector<int>> result;
6+
sort(candidates.begin(), candidates.end());
7+
8+
vector<int> factor(candidates.size());
9+
for(int i = 0; i < candidates.size(); i++){
10+
factor[i] = target/candidates[i];
11+
}
12+
13+
combine_helper(result, factor, candidates, 0, target, vector<int>());
14+
15+
return result;
16+
}
17+
private:
18+
void combine_helper(vector<vector<int>> &result, vector<int> &factor, vector<int> &candidates, int s, int target, vector<int> sofar){
19+
if(target == 0){
20+
result.push_back(sofar);
21+
return;
22+
}
23+
24+
if(s >= factor.size()){
25+
return;
26+
}
27+
28+
for(int i = 0; i <= factor[s]; i++){
29+
if(i > 0) {
30+
sofar.push_back(candidates[s]);
31+
target -= candidates[s];
32+
if(target < 0) break;
33+
}
34+
combine_helper(result, factor, candidates, s+1, target, sofar);
35+
}
36+
}
37+
38+
};

Combination_Sum_2.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
vector<vector<int> > combinationSum2(vector<int> &num, int target) {
4+
vector<vector<int>> result;
5+
sort(num.begin(), num.end());
6+
7+
combine_helper(result, num, 0, target, vector<int>());
8+
9+
return result;
10+
}
11+
private:
12+
void combine_helper(vector<vector<int>> &result, vector<int> &num, int s, int target, vector<int> sofar){
13+
if(target == 0){
14+
result.push_back(sofar);
15+
return;
16+
}
17+
18+
if(s >= num.size()){
19+
return;
20+
}
21+
22+
int i = s;
23+
while(i < (int)num.size()-1 && num[i] == num[i+1]) i++;
24+
25+
// not choose any element
26+
combine_helper(result, num, i+1, target, sofar);
27+
28+
for(int k = s; k <= i; k++){
29+
sofar.push_back(num[s]);
30+
target -= num[s];
31+
if(target < 0) return;
32+
33+
combine_helper(result, num, i+1, target, sofar);
34+
}
35+
}
36+
};

Combinations.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,27 @@ class Solution {
6060
}
6161
}
6262
};
63+
64+
class Solution {
65+
public:
66+
vector<vector<int> > combine(int n, int k) {
67+
vector<vector<int>> result;
68+
vector<int> t;
69+
combine_helper(result, t, n, 1, k);
70+
return result;
71+
}
72+
73+
private:
74+
void combine_helper(vector<vector<int>> &result, vector<int> &sofar, int n, int s, int k){
75+
if(k == 0){
76+
result.push_back(sofar);
77+
return;
78+
}
79+
80+
for(int i = s; i <= n; i++){
81+
sofar.push_back(i);
82+
combine_helper(result, sofar, n, i+1, k-1);
83+
sofar.pop_back();
84+
}
85+
}
86+
};

Count_and_Say.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string countAndSay(int n) {
4+
string st = "1";
5+
if(n == 1) return st;
6+
7+
stringstream ss;
8+
while(n > 1){
9+
int cnt = 1;
10+
ss.str("");
11+
ss.clear();
12+
for(int i = 1; i < st.size(); i++){
13+
if(st[i] == st[i-1]){
14+
cnt++;
15+
}else{
16+
ss << cnt << st[i-1];
17+
cnt = 1;
18+
}
19+
}
20+
ss << cnt << st[(int)st.size()-1];
21+
st = ss.str();
22+
23+
n--;
24+
}
25+
26+
return st;
27+
}
28+
};

Evaluate_Reverse_Polish_Notation.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int evalRPN(vector<string> &tokens) {
4+
if(tokens.empty()) return 0;
5+
6+
stack<int> st;
7+
string s;
8+
int x, y;
9+
10+
for(int i = 0; i < tokens.size(); i++){
11+
s = tokens[i];
12+
if(s == "+" || s == "-" || s == "*" || s == "/"){
13+
if(!st.empty()) {
14+
x = st.top();
15+
st.pop();
16+
}
17+
if(!st.empty()) {
18+
y = st.top();
19+
st.pop();
20+
}
21+
if(s == "+") st.push(y+x);
22+
if(s == "-") st.push(y-x);
23+
if(s == "*") st.push(y*x);
24+
if(s == "/") st.push(y/x);
25+
}else{
26+
st.push(atoi(s.c_str()));
27+
}
28+
}
29+
return st.top();
30+
}
31+
};

First_Missing_Positive.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* 1. remove all the negative numbers and zeros to get ready for mark up
3+
* 2. if a certain number exists, then mark number on that position into negative
4+
* 3. if a number is still positive, which means that number is missing.
5+
*/
6+
class Solution {
7+
public:
8+
int firstMissingPositive(int A[], int n) {
9+
if(n <= 0) return 1;
10+
11+
// the upper bound for the first missing positive integer [1, n+2)
12+
for(int i = 0; i < n; i++){
13+
if(A[i] <= 0) A[i] = n+2; // also mark out zeros
14+
}
15+
16+
// mark the cell where the positive between 1 and n showing up
17+
// indicate if an integer exists
18+
for(int i = 0; i < n; i++){
19+
int a = abs(A[i]);
20+
if(a <= n){ //mark the index for a into neg. number to indicate that it exists
21+
A[a-1] = (A[a-1] > 0)? -1*A[a-1]: A[a-1];
22+
}
23+
}
24+
25+
for(int i = 0; i < n; i++){
26+
if(A[i] > 0){
27+
return i+1;
28+
}
29+
}
30+
31+
return n+1;
32+
}
33+
};

Insert_Interval.cpp

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
13+
vector<Interval> result;
14+
if(intervals.empty()){
15+
result.push_back(newInterval);
16+
return result;
17+
}
18+
19+
int i;
20+
for(i = 0; i < intervals.size(); i++){
21+
if(intervals[i].end < newInterval.start){
22+
result.push_back(intervals[i]);
23+
}else if(intervals[i].start > newInterval.end){
24+
break;
25+
}else{
26+
newInterval.start = min(newInterval.start, intervals[i].start);
27+
newInterval.end = max(newInterval.end, intervals[i].end);
28+
}
29+
}
30+
31+
result.push_back(newInterval);
32+
while(i < intervals.size()){
33+
result.push_back(intervals[i]);
34+
i++;
35+
}
36+
37+
return result;
38+
}
39+
};
40+
41+
42+
/**
43+
* Definition for an interval.
44+
* struct Interval {
45+
* int start;
46+
* int end;
47+
* Interval() : start(0), end(0) {}
48+
* Interval(int s, int e) : start(s), end(e) {}
49+
* };
50+
*/
51+
class Solution {
52+
public:
53+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
54+
vector<Interval> result;
55+
if(intervals.empty()){
56+
result.push_back(newInterval);
57+
return result;
58+
}
59+
60+
auto ind_it = lower_bound(intervals.begin(), intervals.end(), newInterval, interval_cmp);
61+
intervals.insert(ind_it, newInterval);
62+
63+
int i = 1;
64+
int n = intervals.size();
65+
int s, e;
66+
67+
s = intervals[0].start;
68+
e = intervals[0].end;
69+
while(i < n){
70+
if(intervals[i].start <= e){
71+
e = max(e, intervals[i].end);
72+
}else{
73+
result.push_back(Interval(s, e));
74+
s = intervals[i].start;
75+
e = intervals[i].end;
76+
}
77+
78+
i++;
79+
}
80+
result.push_back(Interval(s, e));
81+
82+
return result;
83+
}
84+
private:
85+
static bool interval_cmp(const Interval &lhs, const Interval &rhs){
86+
return lhs.start < rhs.start;
87+
}
88+
};
89+
90+
/**
91+
* Definition for an interval.
92+
* struct Interval {
93+
* int start;
94+
* int end;
95+
* Interval() : start(0), end(0) {}
96+
* Interval(int s, int e) : start(s), end(e) {}
97+
* };
98+
*/
99+
class Solution {
100+
public:
101+
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
102+
vector<Interval> result;
103+
if(intervals.empty()){
104+
result.push_back(newInterval);
105+
return result;
106+
}
107+
108+
auto it = intervals.begin();
109+
while(it != intervals.end()){
110+
if(it->end < newInterval.start){
111+
++it;
112+
}else if(it->start > newInterval.end){
113+
break;
114+
}else{
115+
newInterval.start = min(it->start, newInterval.start);
116+
newInterval.end = max(it->end, newInterval.end);
117+
it = intervals.erase(it);
118+
}
119+
}
120+
intervals.insert(it, newInterval);
121+
return intervals;
122+
}
123+
private:
124+
static bool interval_cmp(const Interval &lhs, const Interval &rhs){
125+
return lhs.start < rhs.start;
126+
}
127+
};

Jump_Game.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* DP based on the recursive version */
2+
class Solution {
3+
public:
4+
bool canJump(int A[], int n) {
5+
if(n <= 1) return true;
6+
vector<int> dp(n, -1);
7+
dp[n-1] = 1;
8+
9+
return can_jump_helper(A, n, 0, dp);
10+
}
11+
private:
12+
bool can_jump_helper(int A[], int n, int s, vector<int> &dp){
13+
if(s >= n-1) return true;
14+
15+
bool result = false;
16+
for(int i = 1; i <= A[s]; i++){
17+
if(dp[s+i] == 1) return true;
18+
else if(dp[s+i] == 0){
19+
}else{
20+
result = can_jump_helper(A, n, s+i, dp);
21+
dp[s+i] = result;
22+
if(result) return true;
23+
}
24+
}
25+
26+
return false;
27+
}
28+
};
29+
30+
class Solution {
31+
public:
32+
bool canJump(int A[], int n) {
33+
if(n <= 1) return true;
34+
vector<bool> dp(n, false);
35+
dp[0] = true;
36+
37+
for(int i = 0; i < n; i++){
38+
if(dp[i]){
39+
for(int j = 1; j <= A[i]; j++){
40+
if(i+j < n) dp[i+j] = true;
41+
else return true;
42+
}
43+
}
44+
}
45+
46+
return dp[n-1];
47+
}
48+
};
49+
50+
class Solution {
51+
public:
52+
bool canJump(int A[], int n) {
53+
if(n == 0) return false;
54+
if(n == 1) return true;
55+
56+
int m = 0;
57+
for(int i = 0; i < n; i++){
58+
if(i <= m){
59+
if(i+A[i] > m){
60+
m = i+A[i];
61+
if(m >= n-1) return true;
62+
}
63+
}
64+
}
65+
66+
return false;
67+
}
68+
};

0 commit comments

Comments
 (0)