Skip to content

1353- Change- Updated the readme file and added 1353.java solution in java #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
47 changes: 21 additions & 26 deletions Java/0983.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
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]);
int lastDay = days[days.length - 1];
int n = lastDay + 1;

int[] dp = new int[n];
boolean[] bool = new boolean[n];
for (int d:days) bool[d] = true;

for (int i=1; i<n; i++){
if (bool[i]){
dp[i] = dp[i-1] + costs[0];
dp[i] = Math.min(dp[i], dp[Math.max(0, i-7)] + costs[1]);
dp[i] = Math.min(dp[i], dp[Math.max(0, i-30)] + costs[2]);
}
else{
dp[i] = dp[i-1];
}
}

memo[i] = ans;
return ans;
return dp[lastDay];

}
}


21 changes: 21 additions & 0 deletions Java/1353.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int maxEvents(int[][] events) {
int Count = 0;
int i = 0;
int n = events.length;
Arrays.sort(events, (e1, e2) -> Integer.compare(e1[0], e2[0]));
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int d=1; d<=100000; d++) {
while (i<n && events[i][0] == d)
pq.add(events[i++][1]);
while (!pq.isEmpty() && pq.peek() < d)
pq.poll();
if (!pq.isEmpty()) {
Count += 1;
pq.poll();
}
}
return Count;

}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Languages Used - C++, Java, Python3
| 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) |
| 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) | 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) |
| 1353 | [Maximum no of events that can be attended (sort)](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended/) | Sort the first element of the event, make use of a priority queue, add events that can be attend on day d, remove events that are already closed and use day d to attend the event that closes earlier. | [Java]() |O(d + nlogn)| O(n) | Medium | Greedy Segment Tree Sort | [📺](https://www.youtube.com/watch?v=UktCMArqmPA) |


Format
Expand Down