Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 135. Candy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> count(n, 1); // Step 1: Initialize with 1

// Step 2: Left to Right
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
count[i] = count[i - 1] + 1;
}
}

// Step 3: Right to Left
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
count[i] = max(count[i], count[i + 1] + 1);
}
}

// Step 4: Total candies
return accumulate(count.begin(), count.end(), 0);
}
};
27 changes: 27 additions & 0 deletions 188. Best Time to Buy and Sell Stock IV.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
vector<vector<vector<int>>> dp;

int profit(vector<int>& prices, int i, int isBuy, int k) {
if (i == prices.size() || k == 0) return 0;

if (dp[i][isBuy][k] != -1) return dp[i][isBuy][k];

int a, b;
if (isBuy) {
a = profit(prices, i + 1, 1, k);
b = profit(prices, i + 1, 0, k) - prices[i];
} else {
a = profit(prices, i + 1, 0, k);
b = profit(prices, i + 1, 1, k - 1) + prices[i];
}

return dp[i][isBuy][k] = max(a, b);
}

int maxProfit(int k, vector<int>& prices) {
int n = prices.size();
dp = vector<vector<vector<int>>>(n, vector<vector<int>>(2, vector<int>(k + 1, -1)));
return profit(prices, 0, 1, k);
}
};
11 changes: 11 additions & 0 deletions 96. Unique Binary Search Trees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int dp[20]{};
int numTrees(int n) {
if(n <= 1) return 1;
if(dp[n]) return dp[n];
for(int i = 1; i <= n; i++)
dp[n] += numTrees(i-1) * numTrees(n-i);
return dp[n];
}
};