2668. Find Latest Salaries
Explanation:
To find the latest salaries from a given list of salaries, we can use a HashMap to store the latest salary for each employee. We iterate through the list of salaries and update the HashMap with the latest salary for each employee. Finally, we retrieve the latest salaries from the HashMap.
- Create a HashMap to store the latest salary for each employee.
- Iterate through the list of salaries.
- Update the HashMap with the latest salary for each employee.
- Retrieve the latest salaries from the HashMap.
Time Complexity: O(n) where n is the number of salaries in the input list. Space Complexity: O(n) for the HashMap to store the latest salaries.
:
import java.util.*;
class Solution {
public Map<Integer, Integer> findLatestSalaries(List<List<Integer>> salaries) {
Map<Integer, Integer> latestSalaries = new HashMap<>();
for (List<Integer> salary : salaries) {
int employeeId = salary.get(0);
int employeeSalary = salary.get(1);
latestSalaries.put(employeeId, employeeSalary);
}
return latestSalaries;
}
}
Code Editor (Testing phase)
Improve Your Solution
Use the editor below to refine the provided solution. Select a programming language and try the following:
- Add import statement if required.
- Optimize the code for better time or space complexity.
- Add test cases to validate edge cases and common scenarios.
- Handle error conditions or invalid inputs gracefully.
- Experiment with alternative approaches to deepen your understanding.
Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.