-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC983.java
More file actions
91 lines (75 loc) · 2.81 KB
/
LC983.java
File metadata and controls
91 lines (75 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* LC983
*/
import java.util.*;
public class LC983 {
public static int mincostTickets(int[] days, int[] costs) {
Set<Integer> set = new HashSet<>();
int lastDay = days[days.length - 1];
// Using DP for overlapping subproblem
int[] dp = new int[lastDay + 1];
Arrays.fill(dp, 0);
// Adding to the set to track the values which should be added or not added
for (int day : days) {
set.add(day);
}
for (int currDay = 1; currDay <= lastDay; currDay++) {
if (!set.contains(currDay)) {
// If not a travel day, carry forward the previous day's cost
dp[currDay] = dp[currDay - 1];
} else {
// Calculate the minimum cost for the current day
int oneDayCost = dp[Math.max(0, currDay - 1)] + costs[0];
int sevenDayCost = dp[Math.max(0, currDay - 7)] + costs[1];
int thirtyDayCost = dp[Math.max(0, currDay - 30)] + costs[2];
dp[currDay] = Math.min(oneDayCost, Math.min(sevenDayCost, thirtyDayCost));
}
}
return dp[lastDay];
}
// public static int recur(int[] days, int[] costs, int currDay, int[] dp) {
// // base case
// if (currDay > days[days.length - 1]) {
// return 0;
// }
// if (dp[currDay] != -1) {
// return dp[currDay];
// }
// if (!set.contains(currDay)) {
// dp[currDay] = recur(days, costs, currDay + 1, dp);
// return dp[currDay];
// }
// // Calculating the days calls
// int oneDay = costs[0] + recur(days, costs, currDay + 1, dp);
// int sevenDay = costs[1] + recur(days, costs, currDay + 7, dp);
// int thirtyDay = costs[2] + recur(days, costs, currDay + 30, dp);
// dp[currDay] = Math.min(oneDay, Math.min(sevenDay, thirtyDay));
// return dp[currDay];
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Days Array Size : ");
int size = sc.nextInt();
System.out.println();
int[] days = new int[size];
System.out.println("Enter The Days Array Elements : ");
for (int i = 0; i < days.length; i++) {
System.out.printf("%d : ", i);
days[i] = sc.nextInt();
}
System.out.println();
System.out.print("Enter The Costs Array Size : ");
int n = sc.nextInt();
System.out.println();
int[] costs = new int[n];
System.out.println("Enter The Costs Array Size : ");
for (int i = 0; i < costs.length; i++) {
System.out.printf("%d : ", i);
costs[i] = sc.nextInt();
}
System.out.println();
int ans = mincostTickets(days, costs);
System.out.println(ans);
sc.close();
}
}