LeetCode 1831: Maximum Transaction Each Day
Problem Description
Explanation:
To solve this problem, we can iterate through the given transactions and maintain a map to store the transactions that occur on each day. We can then iterate through the map to find the maximum transaction that occurs on each day. Finally, we return the maximum transaction from all days.
- Create a map to store transactions occurring on each day.
- Iterate through the transactions and update the map accordingly.
- Iterate through the map and find the maximum transaction for each day.
- Return the overall maximum transaction value.
Time Complexity: O(N), where N is the number of transactions. Space Complexity: O(N) for storing the transactions in the map.
:
Solutions
class Solution {
public int maxTaxiEarnings(int n, int[][] rides) {
Map<Integer, List<int[]>> map = new HashMap<>();
for (int[] ride : rides) {
map.computeIfAbsent(ride[1], k -> new ArrayList<>()).add(ride);
}
int[] dp = new int[n + 1];
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
if (map.containsKey(i)) {
for (int[] ride : map.get(i)) {
dp[i] = Math.max(dp[i], dp[ride[0]] + ride[1] - ride[0] + ride[2]);
}
}
}
return dp[n];
}
}
Loading editor...