LeetCode 2687: Bikes Last Time Used
Problem Description
Explanation
To solve this problem, we need to find the last time a bike was used given the start and end time of each bike's rides. We can achieve this by iterating through the rides and storing the last time each bike was used in a HashMap. Finally, we return the HashMap with the last time each bike was used.
- Create a HashMap to store the last time each bike was used.
- Iterate through the rides and update the last time each bike was used.
- Return the HashMap with the last time each bike was used.
Time Complexity: O(n), where n is the number of rides Space Complexity: O(n) for storing the last time each bike was used
Solutions
import java.util.HashMap;
import java.util.Map;
class Solution {
public Map<Integer, Integer> lastTimeUsed(int[][] rides) {
Map<Integer, Integer> lastUsed = new HashMap<>();
for (int[] ride : rides) {
int bikeId = ride[0];
int endTime = ride[1];
lastUsed.put(bikeId, Math.max(lastUsed.getOrDefault(bikeId, -1), endTime));
}
return lastUsed;
}
}
Loading editor...