LeetCode 1369: Get the Second Most Recent Activity Solution

Master LeetCode problem 1369 (Get the Second Most Recent Activity), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1369. Get the Second Most Recent Activity

Problem Explanation

Explanation:

To solve this problem, we can maintain a hashmap to store the most recent occurrence of each activity. We iterate through the given list of activities, updating the hashmap with the latest timestamp of each activity. Finally, we extract the second most recent activity based on the timestamp stored in the hashmap.

  • Start by initializing an empty hashmap to store activities and their timestamps.
  • Iterate through the list of activities and update the hashmap with the latest timestamp of each activity.
  • Extract the activity with the second most recent timestamp from the hashmap.
  • Return the second most recent activity.

Time Complexity:

The time complexity of this approach is O(N), where N is the number of activities in the input list.

Space Complexity:

The space complexity of this approach is O(N) to store the hashmap where N is the number of activities in the input list.

: :

Solution Code

class Solution {
    public String secondMostRecentActivity(String[] activities) {
        Map<String, Integer> activityMap = new HashMap<>();
        
        for (String activity : activities) {
            activityMap.put(activity, activityMap.getOrDefault(activity, 0) + 1);
        }
        
        PriorityQueue<Map.Entry<String, Integer>> maxHeap = 
            new PriorityQueue<>((a, b) -> Integer.compare(b.getValue(), a.getValue()));
        maxHeap.addAll(activityMap.entrySet());
        
        maxHeap.poll(); // Skip the most recent activity
        return maxHeap.poll().getKey();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1369 (Get the Second Most Recent Activity)?

This page provides optimized solutions for LeetCode problem 1369 (Get the Second Most Recent Activity) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1369 (Get the Second Most Recent Activity)?

The time complexity for LeetCode 1369 (Get the Second Most Recent Activity) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1369 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1369 in Java, C++, or Python.

Back to LeetCode Solutions