Skip to content

Commit adcaf86

Browse files
committed
Commit
1 parent 9d79d22 commit adcaf86

10 files changed

+235
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
5+
if(prices.size() == 0)
6+
return 0;
7+
8+
int ans = 0;
9+
10+
int mn=prices[0];
11+
12+
for(int i=1;i<prices.size();i++){
13+
ans = max(ans,prices[i]-mn);
14+
mn = min(mn,prices[i]);
15+
}
16+
17+
return ans;
18+
19+
}
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices, int fee) {
4+
5+
6+
int cash = 0 , hold = -prices[0];
7+
8+
for(int i=1;i<prices.size();i++){
9+
cash = max(cash,hold+prices[i]-fee);
10+
hold = max(hold,cash-prices[i]);
11+
}
12+
13+
return cash;
14+
15+
}
16+
};

Leetcode/ClimbingStairs.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n) {
4+
5+
if(n==1)
6+
return 1;
7+
8+
int second = 1;
9+
int first = 2;
10+
11+
for(int i=n-2;i>=1;i--){
12+
int temp = first + second;
13+
second = first;
14+
first = temp;
15+
}
16+
17+
return first;
18+
19+
}
20+
};

Leetcode/DivisorGame.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool divisorGame(int N) {
4+
bool dp[N+1]={false};
5+
6+
7+
for(int i=2;i<=N;i++){
8+
bool flag=0;
9+
for(int j=1;j<=sqrt(i);j++){
10+
if(i%j==0 && dp[i-j]==0){
11+
flag=true;
12+
}
13+
}
14+
dp[i]=flag;
15+
}
16+
17+
return dp[N];
18+
19+
}
20+
};

Leetcode/HouseRobber.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
5+
6+
int n = nums.size();
7+
8+
if(n == 0)
9+
return 0;
10+
11+
vector<int>dp(n+1,0);
12+
13+
dp[0] = 0;
14+
dp[1] = nums[0];
15+
16+
for(int i=2;i<=nums.size();i++){
17+
dp[i] = max(nums[i-1]+dp[i-2],dp[i-1]);
18+
}
19+
20+
return dp[n];
21+
22+
}
23+
};

Leetcode/IntegerBreak.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
4+
int integerBreak(int n) {
5+
6+
if( n == 1)return 1;
7+
8+
if( n == 2)return 1;
9+
10+
if( n == 3)return 2;
11+
12+
if( n == 4)return 4;
13+
14+
if(n == 5) return 6;
15+
16+
if(n == 6) return 9;
17+
18+
19+
if(n%3 == 1) return pow(3,n/3-1)*4;
20+
21+
if(n%3 == 2) return pow(3,n/3)*2;
22+
23+
return pow(3,n/3);
24+
25+
26+
}
27+
};

Leetcode/MaxLengthOfPairChain.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
3+
4+
public:
5+
6+
static bool comp(vector<int>& a, vector<int>& b){
7+
return a[0] < b[0];
8+
}
9+
10+
int findLongestChain(vector<vector<int>>& pairs) {
11+
12+
int n = pairs.size();
13+
14+
int dp[n+1] = {0};
15+
16+
if(n == 0)
17+
return 0;
18+
19+
sort(pairs.begin(),pairs.end(),comp);
20+
21+
for(int i=0;i<n;i++){
22+
for(int j=i-1;j>=0;j--){
23+
if(pairs[i][0] > pairs[j][1]){
24+
dp[i] = max(dp[i],dp[j]);
25+
}
26+
}
27+
28+
dp[i]++;
29+
}
30+
31+
int ans = 0;
32+
33+
for(int i=0;i<n;i++)
34+
ans = max(ans,dp[i]);
35+
36+
return ans;
37+
}
38+
};

Leetcode/MaximumSubarray.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
5+
int ans = nums[0];
6+
int sum;
7+
8+
if(ans < 0)
9+
sum = 0;
10+
else
11+
sum = ans;
12+
13+
for(int i=1;i<nums.size();i++){
14+
if(sum+nums[i]<0){
15+
ans = max(ans,sum+nums[i]);
16+
sum = 0;
17+
}else{
18+
ans = max(ans,sum+nums[i]);
19+
sum+=nums[i];
20+
}
21+
}
22+
23+
return ans;
24+
25+
}
26+
};

Leetcode/MinCostClimbingStairs.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int minCostClimbingStairs(vector<int>& cost) {
4+
5+
int n=cost.size();
6+
vector<int>dp(n,0);
7+
8+
for(int i=n-1;i>=0;i--){
9+
if(i+1 >= n || i+2 >= n)
10+
dp[i] = cost[i];
11+
else
12+
dp[i] = cost[i] + min(dp[i+1],dp[i+2]);
13+
}
14+
15+
return min(dp[0],dp[1]);
16+
17+
}
18+
};

Leetcode/RangeSumQuery.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class NumArray {
2+
public:
3+
4+
5+
vector<int>sum;
6+
7+
NumArray(vector<int>& nums) {
8+
9+
for(int i=0;i<=nums.size();i++)
10+
sum.push_back(0);
11+
12+
for(int i = 0 ; i < nums.size(); i++) {
13+
sum[i+1]+= sum[i]+nums[i];
14+
}
15+
16+
}
17+
18+
int sumRange(int i, int j) {
19+
return sum[j+1]-sum[i];
20+
}
21+
};
22+
23+
/**
24+
* Your NumArray object will be instantiated and called as such:
25+
* NumArray* obj = new NumArray(nums);
26+
* int param_1 = obj->sumRange(i,j);
27+
*/

0 commit comments

Comments
 (0)