Skip to content

Commit 9fb6fc5

Browse files
🔥 Day 25
1 parent a738ffb commit 9fb6fc5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
1. [Random Point in Non-overlapping Rectangles](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3433/) ➡️ [CPP Solution](Week4/randomPoint.cpp)
4040
2. [Stream of Characters](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3434/) ➡️ [CPP Solution](Week4/streamChecker.cpp)
4141
3. [Sum of Left Leaves](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3435/) ➡️ [CPP Solution](Week4/sumOfLeftLeaves.cpp)
42+
4. [Minimum Cost For Tickets](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3435/) ➡️ [CPP Solution](Week4/mincostTickets.cpp)
4243

4344
## Week 5 🚧
4445

Week4/mincostTickets.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int mincostTickets(vector<int>& days, vector<int>& costs) {
4+
int n = days[days.size() - 1];
5+
bool travelDays[n + 1];
6+
int dp[n + 1];
7+
8+
fill(travelDays, travelDays + n + 1, false);
9+
fill(dp, dp + n + 1, 0);
10+
11+
for(int day: days) {
12+
travelDays[day] = true;
13+
}
14+
15+
for(int i = 1; i < n + 1; ++i) {
16+
if(!travelDays[i]) {
17+
dp[i] = dp[i - 1];
18+
continue;
19+
}
20+
21+
int one = dp[i - 1] + costs[0];
22+
int seven = dp[max(i - 7, 0)] + costs[1];
23+
int thirty = dp[max(i - 30, 0)] + costs[2];
24+
25+
dp[i] = min(one, min(seven, thirty));
26+
}
27+
28+
return dp[n];
29+
}
30+
};

0 commit comments

Comments
 (0)