LeetCode 1348: Tweet Counts Per Frequency Solution

Master LeetCode problem 1348 (Tweet Counts Per Frequency), a medium 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.

1348. Tweet Counts Per Frequency

Problem Explanation

Explanation

To solve this problem, we can use a HashMap to store the tweets recorded for each tweetName along with their timestamps. When querying for the tweet counts per frequency, we can iterate over the timestamps and count the number of tweets falling within the specified time range for each chunk based on the given frequency.

Algorithm:

  1. Initialize a HashMap to store tweetName as the key and a list of timestamps as the value.
  2. Implement the recordTweet method to store the tweetName along with the timestamp in the HashMap.
  3. Implement the getTweetCountsPerFrequency method to calculate the tweet counts per frequency based on the specified time range and frequency.
  4. For each chunk within the time range, iterate over the timestamps and count the number of tweets that fall within that chunk.
  5. Return the list of tweet counts for each chunk.

Time Complexity:

  • Recording a tweet: O(1)
  • Retrieving tweet counts per frequency: O(n), where n is the number of timestamps for the given tweetName.

Space Complexity:

  • O(n), where n is the total number of tweets recorded.

Solution Code

class TweetCounts {
    Map<String, List<Integer>> tweets;

    public TweetCounts() {
        tweets = new HashMap<>();
    }

    public void recordTweet(String tweetName, int time) {
        if (!tweets.containsKey(tweetName)) {
            tweets.put(tweetName, new ArrayList<>());
        }
        tweets.get(tweetName).add(time);
    }

    public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
        List<Integer> result = new ArrayList<>();
        int chunkSize = freq.equals("minute") ? 60 : freq.equals("hour") ? 3600 : 86400;
        
        List<Integer> tweetTimes = tweets.getOrDefault(tweetName, new ArrayList<>());
        Collections.sort(tweetTimes);
        
        int chunkCount = (endTime - startTime) / chunkSize + 1;
        
        for (int i = 0; i < chunkCount; i++) {
            int chunkStart = startTime + i * chunkSize;
            int chunkEnd = Math.min(chunkStart + chunkSize - 1, endTime);
            int count = 0;
            for (int time : tweetTimes) {
                if (time >= chunkStart && time <= chunkEnd) {
                    count++;
                }
            }
            result.add(count);
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1348 (Tweet Counts Per Frequency)?

This page provides optimized solutions for LeetCode problem 1348 (Tweet Counts Per Frequency) 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 1348 (Tweet Counts Per Frequency)?

The time complexity for LeetCode 1348 (Tweet Counts Per Frequency) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1348 on DevExCode?

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

Back to LeetCode Solutions