package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 1348. Tweet Counts Per Frequency
 *
 * Implement the class TweetCounts that supports two methods:
 *
 * 1. recordTweet(string tweetName, int time)
 * Stores the tweetName at the recorded time (in seconds).
 * 2. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)
 * Returns the total number of occurrences for the given tweetName per minute, hour, or day (depending on freq) starting from the startTime (in seconds) and ending at the endTime (in seconds).
 * freq is always minute, hour or day, representing the time interval to get the total number of occurrences for the given tweetName.
 * The first time interval always starts from the startTime,
 * so the time intervals are [startTime, startTime + delta*1>,  [startTime + delta*1, startTime + delta*2>, [startTime + delta*2, startTime + delta*3>, ... , [startTime + delta*i, min(startTime + delta*(i+1), endTime + 1)> for some non-negative number i and delta (which depends on freq).
 *
 * Example:
 * Input
 * ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
 * [[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]
 *
 * Output
 * [null,null,null,null,[2],[2,1],null,[4]]
 *
 * Explanation
 * TweetCounts tweetCounts = new TweetCounts();
 * tweetCounts.recordTweet("tweet3", 0);
 * tweetCounts.recordTweet("tweet3", 60);
 * tweetCounts.recordTweet("tweet3", 10);                             // All tweets correspond to "tweet3" with recorded times at 0, 10 and 60.
 * tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]. The frequency is per minute (60 seconds), so there is one interval of time: 1) [0, 60> - > 2 tweets.
 * tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2, 1]. The frequency is per minute (60 seconds), so there are two intervals of time: 1) [0, 60> - > 2 tweets, and 2) [60,61> - > 1 tweet.
 * tweetCounts.recordTweet("tweet3", 120);                            // All tweets correspond to "tweet3" with recorded times at 0, 10, 60 and 120.
 * tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210);  // return [4]. The frequency is per hour (3600 seconds), so there is one interval of time: 1) [0, 211> - > 4 tweets.
 *
 * Constraints:
 * There will be at most 10000 operations considering both recordTweet and getTweetCountsPerFrequency.
 * 0 <= time, startTime, endTime <= 10^9
 * 0 <= endTime - startTime <= 10^4
 * */
public class _1348 {
    public static class Solution1 {
        public static class TweetCounts {
            /**
             * credit: https://leetcode.com/problems/tweet-counts-per-frequency/discuss/503453/Java-TreeMap-Accepted-Solution-Easy-Understand
             */
            private Map<String, TreeMap<Integer, Integer>> map;

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

            public void recordTweet(String tweetName, int time) {
                if (!map.containsKey(tweetName)) {
                    map.put(tweetName, new TreeMap<>());
                }
                TreeMap<Integer, Integer> tweetMap = map.get(tweetName);
                tweetMap.put(time, tweetMap.getOrDefault(time, 0) + 1);
            }

            public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
                if (!map.containsKey(tweetName)) {
                    return null;
                }
                int interval;
                if (freq.equals("minute")) {
                    interval = 60;
                } else if (freq.equals("hour")) {
                    interval = 60 * 60;
                } else {
                    interval = 60 * 60 * 24;
                }
                int size = ((endTime - startTime) / interval) + 1;
                int[] buckets = new int[size];
                TreeMap<Integer, Integer> tweetMap = map.get(tweetName);
                for (Map.Entry<Integer, Integer> entry : tweetMap.subMap(startTime, endTime + 1).entrySet()) {
                    int index = (entry.getKey() - startTime) / interval;
                    buckets[index] += entry.getValue();
                }
                List<Integer> result = new ArrayList<>();
                for (int num : buckets) {
                    result.add(num);
                }
                return result;
            }
        }
    }
}

@@ -159,2610 +159,8 @@
 p;%0A%0A
-/**%0A * 1348. Tweet Counts Per Frequency%0A *%0A * Implement the class TweetCounts that supports two methods:%0A *%0A * 1. recordTweet(string tweetName, int time)%0A * Stores the tweetName at the recorded time (in seconds).%0A * 2. getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)%0A * Returns the total number of occurrences for the given tweetName per minute, hour, or day (depending on freq) starting from the startTime (in seconds) and ending at the endTime (in seconds).%0A * freq is always minute, hour or day, representing the time interval to get the total number of occurrences for the given tweetName.%0A * The first time interval always starts from the startTime,%0A * so the time intervals are %5BstartTime, startTime + delta*1%3E,  %5BstartTime + delta*1, startTime + delta*2%3E, %5BstartTime + delta*2, startTime + delta*3%3E, ... , %5BstartTime + delta*i, min(startTime + delta*(i+1), endTime + 1)%3E for some non-negative number i and delta (which depends on freq).%0A *%0A * Example:%0A * Input%0A * %5B%22TweetCounts%22,%22recordTweet%22,%22recordTweet%22,%22recordTweet%22,%22getTweetCountsPerFrequency%22,%22getTweetCountsPerFrequency%22,%22recordTweet%22,%22getTweetCountsPerFrequency%22%5D%0A * %5B%5B%5D,%5B%22tweet3%22,0%5D,%5B%22tweet3%22,60%5D,%5B%22tweet3%22,10%5D,%5B%22minute%22,%22tweet3%22,0,59%5D,%5B%22minute%22,%22tweet3%22,0,60%5D,%5B%22tweet3%22,120%5D,%5B%22hour%22,%22tweet3%22,0,210%5D%5D%0A *%0A * Output%0A * %5Bnull,null,null,null,%5B2%5D,%5B2,1%5D,null,%5B4%5D%5D%0A *%0A * Explanation%0A * TweetCounts tweetCounts = new TweetCounts();%0A * tweetCounts.recordTweet(%22tweet3%22, 0);%0A * tweetCounts.recordTweet(%22tweet3%22, 60);%0A * tweetCounts.recordTweet(%22tweet3%22, 10);                             // All tweets correspond to %22tweet3%22 with recorded times at 0, 10 and 60.%0A * tweetCounts.getTweetCountsPerFrequency(%22minute%22, %22tweet3%22, 0, 59); // return %5B2%5D. The frequency is per minute (60 seconds), so there is one interval of time: 1) %5B0, 60%3E - %3E 2 tweets.%0A * tweetCounts.getTweetCountsPerFrequency(%22minute%22, %22tweet3%22, 0, 60); // return %5B2, 1%5D. The frequency is per minute (60 seconds), so there are two intervals of time: 1) %5B0, 60%3E - %3E 2 tweets, and 2) %5B60,61%3E - %3E 1 tweet.%0A * tweetCounts.recordTweet(%22tweet3%22, 120);                            // All tweets correspond to %22tweet3%22 with recorded times at 0, 10, 60 and 120.%0A * tweetCounts.getTweetCountsPerFrequency(%22hour%22, %22tweet3%22, 0, 210);  // return %5B4%5D. The frequency is per hour (3600 seconds), so there is one interval of time: 1) %5B0, 211%3E - %3E 4 tweets.%0A *%0A * Constraints:%0A * There will be at most 10000 operations considering both recordTweet and getTweetCountsPerFrequency.%0A * 0 %3C= time, startTime, endTime %3C= 10%5E9%0A * 0 %3C= endTime - startTime %3C= 10%5E4%0A * */%0A
 publ

refactor 1348