Skip to content

Commit 2815353

Browse files
committed
Improved task 3433
1 parent 3ed6855 commit 2815353

File tree

1 file changed

+30
-36
lines changed
  • src/main/kotlin/g3401_3500/s3433_count_mentions_per_user

1 file changed

+30
-36
lines changed
Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
11
package g3401_3500.s3433_count_mentions_per_user
22

3-
// #Medium #Array #Math #Sorting #Simulation #2025_01_26_Time_90_(100.00%)_Space_49.08_(100.00%)
3+
// #Medium #Array #Math #Sorting #Simulation #2025_01_29_Time_52_(100.00%)_Space_47.22_(60.71%)
44

55
class Solution {
66
fun countMentions(numberOfUsers: Int, events: List<List<String>>): IntArray {
7-
val sortedEvents = events.sortedWith { a, b ->
8-
val time1 = a[1].toInt()
9-
val time2 = b[1].toInt()
10-
if (time1 == time2 && a[0] == "OFFLINE" && b[0] == "MESSAGE") {
11-
-1
12-
} else {
13-
time1 - time2
14-
}
15-
}
167
val ans = IntArray(numberOfUsers)
17-
val userTimestamps = IntArray(numberOfUsers)
18-
for (event in sortedEvents) {
19-
val msg = event[0]
20-
val time = event[1].toInt()
21-
when (msg) {
22-
"OFFLINE" -> {
23-
userTimestamps[event[2].toInt()] = time + 60
8+
val l: MutableList<Int?> = ArrayList<Int?>()
9+
var c = 0
10+
for (i in events.indices) {
11+
val s = events[i][0]
12+
val ss = events[i][2]
13+
if (s == "MESSAGE") {
14+
if (ss == "ALL" || ss == "HERE") {
15+
c++
16+
if (ss == "HERE") {
17+
l.add(events[i][1].toInt())
18+
}
19+
} else {
20+
val sss: Array<String?> = ss.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
21+
for (j in sss.indices) {
22+
val jj = sss[j]!!.substring(2, sss[j]!!.length).toInt()
23+
ans[jj]++
24+
}
2425
}
25-
"MESSAGE" -> {
26-
val mentionsString = event[2]
27-
when (mentionsString) {
28-
"ALL" -> {
29-
for (i in 0 until numberOfUsers) {
30-
ans[i]++
31-
}
32-
}
33-
"HERE" -> {
34-
for (i in 0 until numberOfUsers) {
35-
if (userTimestamps[i] <= time) ans[i]++
36-
}
37-
}
38-
else -> {
39-
mentionsString.split(" ").forEach { id ->
40-
val curr = id.substring(2).toInt()
41-
ans[curr]++
42-
}
43-
}
26+
}
27+
}
28+
for (i in events.indices) {
29+
if (events[i][0] == "OFFLINE") {
30+
val id = events[i][2].toInt()
31+
val a = events[i][1].toInt() + 60
32+
for (j in l.indices) {
33+
if (l[j]!! >= a - 60 && l[j]!! < a) {
34+
ans[id]--
4435
}
4536
}
4637
}
4738
}
39+
for (i in 0..<numberOfUsers) {
40+
ans[i] += c
41+
}
4842
return ans
4943
}
5044
}

0 commit comments

Comments
 (0)