LeetCode Entry
3433. Count Mentions Per User
Count messages and track online users
3433. Count Mentions Per User medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1201
Problem TLDR
Count messages and track online users #medium #simulation
Intuition
The problem is not that big. Sort by timestamp and put queries after offline modifications.
Approach
- use queue of coming online or just array of coming online times for all users
Complexity
-
Time complexity: \(O(n^2)\)
-
Space complexity: \(O(n)\)
Code
// 61ms
fun countMentions(n: Int, e: List<List<String>>): IntArray {
val r = IntArray(n); val on = IntArray(n)
for ((type, ts, ids) in e.sortedBy { it[1].toInt()*100-it[0][0].toInt() })
if (type[0] == 'O') on[ids.toInt()] = ts.toInt() + 60
else if (ids[0] == 'A') for (i in 0..<n) ++r[i]
else if (ids[0] == 'i') for (i in ids.split(" ")) ++r[i.drop(2).toInt()]
else for (i in 0..<n) if (on[i]<=ts.toInt()) ++r[i]
return r
}
// 0ms
pub fn count_mentions(n: i32, mut e: Vec<Vec<String>>) -> Vec<i32> {
let n = n as usize; let (mut r, mut o) = (vec![0; n], vec![0; n]);
e.sort_unstable_by_key(|v| v[1].parse::<i32>().unwrap()*100-v[0].as_bytes()[0] as i32);
for v in &e {
let (tp, ts) = (v[0].as_bytes()[0], v[1].parse::<i32>().unwrap());
if tp == b'O' { o[v[2].parse::<usize>().unwrap()] = ts + 60 }
else if v[2].as_bytes()[0] == b'A' { for i in &mut r { *i += 1 }}
else if v[2].as_bytes()[0] == b'i' { for i in v[2].split(" ") { r[i[2..].parse::<usize>().unwrap()] += 1 }}
else { for i in 0..n { if o[i] <= ts { r[i] += 1 }}}
} r
}