diff --git a/Java/0983.java b/Java/0983.java index fa451bc..69b30eb 100644 --- a/Java/0983.java +++ b/Java/0983.java @@ -1,31 +1,15 @@ -class Solution { - int[] days, costs; - Integer[] memo; - int[] durations = new int[]{1, 7, 30}; - - public int mincostTickets(int[] days, int[] costs) { - this.days = days; - this.costs = costs; - memo = new Integer[days.length]; - - return dp(0); - } - - public int dp(int i) { - if (i >= days.length) - return 0; - if (memo[i] != null) - return memo[i]; - - int ans = Integer.MAX_VALUE; - int j = i; - for (int k = 0; k < 3; ++k) { - while (j < days.length && days[j] < days[i] + durations[k]) - j++; - ans = Math.min(ans, dp(j) + costs[k]); - } - - memo[i] = ans; - return ans; - } -} +class Solution { + public int mincostTickets(int[] days, int[] costs) { + int n = days.length; + int[] dp = new int[n+1]; + for(int i = 0; i < n; ++i) dp[i] = 365*costs[0]; + + for(int i = n-1; i >= 0; --i){ + int d7 = i, d30 = i; + while(d7 < n && days[d7] < days[i] + 7) ++d7; + while(d30 < n && days[d30] < days[i] + 30) ++d30; + dp[i] = Math.min(costs[0] + dp[i+1], Math.min(costs[1] + dp[d7], costs[2] + dp[d30])); + } + return dp[0]; + } +} diff --git a/README.md b/README.md index 864760b..255d708 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Or from the Sheet 1 of this [Google Sheet](https://bit.ly/2EUhwnw) | 0695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | Apply DFS on the Gird by exploring every square connected to it 4-directionally, total number of squares explored will be the area of that connected shape. | [C++](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/C++/0695.cpp) | O(R∗C) | O(R∗C) | Medium | Depth-First-Search | | | 0876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/solution/) | Find the length of given linked list and then traverse from root till one less than half of linked list length and print the next node to current node | [Java](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Java/0876.java) | O(n) | O(1) | Easy | Linked List | | | 0983 | [Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/) | Here dp(i) is the cost to travel from day days[i] to the end of the plan. if days[j] < days[i] + 1 then j1=j. if days[j] < days[i] + 7 then j=j7. if days[j] < days[i] + 30 then j=j30 . dp(i)=min(dp(j1)+costs[0],dp(j7)+costs[1],dp(j30)+costs[2]) | [Java](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Java/0983.java) | O(n) | O(n) | Medium | Dynamic Programming | [📺](https://www.youtube.com/watch?v=2AnrAlCA578) | +| 0983 | [Minimum Cost For Tickets]https://leetcode.com/problems/minimum-cost-for-tickets/solution/) | | [Java](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Java/0983.java) | O(n) | O(n) | Medium | Dynamic Programming | [📺](https://www.youtube.com/watch?v=2AnrAlCA578) | | 1347 | [Minimum Number of Steps to Make Two Strings Anagram](https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram/) | Add 1 for char in s and remove 1 for char in t | [Java](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Java/1347.java) [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/1347.py) | O(n+m) | O(1) | Medium | Hash Table Heap | [📺](https://www.youtube.com/watch?v=xXXOpOYWtRE) | | 1352 | [Product of the Last K Numbers](https://leetcode.com/problems/product-of-the-last-k-numbers/) | Add new element to list by multiplying it with previous number and return arr[n-1]/arr[n-k-1] | [Python](https://github.com/sankalpdayal5/LeetCode-Solutions/blob/master/Python/1352.py) | O(1) | O(1) | Medium | Array Design | [📺](https://www.youtube.com/watch?v=8CuVduv0Kyg) |