All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1301_1400.s1348_tweet_counts_per_frequency.TweetCounts.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1301_1400.s1348_tweet_counts_per_frequency

// #Medium #Hash_Table #Sorting #Binary_Search #Design #Ordered_Set
// #2023_06_06_Time_701_ms_(100.00%)_Space_65_MB_(100.00%)

class TweetCounts {
    private val store: MutableMap>>>>

    init {
        store = HashMap()
    }

    fun recordTweet(tweetName: String, time: Int) {
        val d = time / DAY
        val h = (time - d * DAY) / HOUR
        val m = (time - d * DAY - h * HOUR) / MINUTE
        val dstore = store.computeIfAbsent(tweetName) { _: String? -> HashMap() }
        val hstore = dstore.computeIfAbsent(d) { _: Int? -> HashMap() }
        val mstore = hstore.computeIfAbsent(h) { _: Int? -> HashMap() }
        mstore.computeIfAbsent(m) { _: Int? -> ArrayList() }.add(time)
    }

    fun getTweetCountsPerFrequency(
        freq: String,
        tweetName: String,
        startTime: Int,
        endTime: Int
    ): List {
        val sfreq = convFreqToSecond(freq)
        val dstore: Map>>> = store[tweetName]!!
        val chunks = IntArray((endTime - startTime) / sfreq + 1)
        val sd = startTime / DAY
        val ed = endTime / DAY
        for (d in sd..ed) {
            if (!dstore.containsKey(d)) {
                continue
            }
            val sh = if (startTime <= d * DAY) 0 else (startTime - d * DAY) / HOUR
            val eh = if (endTime > (d + 1) * DAY) DAY / HOUR else (endTime - d * DAY) / HOUR + 1
            val hstore: Map>> = dstore[d]!!
            for (h in sh until eh) {
                if (!hstore.containsKey(h)) {
                    continue
                }
                val sm = if (startTime <= d * DAY + h * HOUR) 0
                else (startTime - d * DAY - h * HOUR) / MINUTE
                val em = if (endTime > d * DAY + (h + 1) * HOUR) HOUR / MINUTE
                else (endTime - d * DAY - h * HOUR) / MINUTE + 1
                val mstore: Map> = hstore[h]!!
                for (m in sm..em) {
                    if (!mstore.containsKey(m)) {
                        continue
                    }
                    for (rc in mstore[m]!!) {
                        if (startTime <= rc && rc <= endTime) {
                            chunks[(rc - startTime) / sfreq]++
                        }
                    }
                }
            }
        }
        val ans: MutableList = ArrayList()
        for (chunk in chunks) {
            ans.add(chunk)
        }
        return ans
    }

    private fun convFreqToSecond(freq: String): Int {
        return when (freq) {
            "minute" -> MINUTE
            "hour" -> HOUR
            "day" -> DAY
            else -> 0
        }
    }

    companion object {
        private const val MINUTE = 60
        private const val HOUR = 3600
        private const val DAY = 86400
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy