3433. 统计用户被提及情况
今天试了下deepseek,我把题目内容、数据范围、方法名、参数全量喂给了deepseek。deepseek没能做对,这是deepseek的解。
def count_mentions(number_of_users, events)
# 初始化用户的提及次数和在线状态
mentions = Array.new(number_of_users, 0)
online_status = Array.new(number_of_users, true)
offline_events = []
# 按时间戳排序事件
events.sort_by! { |event| event[1].to_i }
events.each do |event|
type, timestamp, data = event
timestamp = timestamp.to_i
# 处理离线事件
if type == "OFFLINE"
user_id = data.to_i
online_status[user_id] = false
offline_events << [timestamp + 60, user_id]
else
# 处理消息事件
# 首先处理所有在当前时间戳之前的离线事件
while !offline_events.empty? && offline_events.first[0] <= timestamp
_, user_id = offline_events.shift
online_status[user_id] = true
end
# 解析提及字符串
if data == "ALL"
# 提及所有用户
number_of_users.times { |i| mentions[i] += 1 }
elsif data == "HERE"
# 提及所有在线用户
number_of_users.times { |i| mentions[i] += 1 if online_status[i] }
else
# 提及特定的用户
data.split.each do |mention|
if mention.start_with?("id")
user_id = mention[2..-1].to_i
mentions[user_id] += 1
end
end
end
end
end
mentions
end
我只好自己做了,然后就过了,这是我的解。
# @param {Integer} number_of_users
# @param {String[][]} events
# @return {Integer[]}
def count_mentions(number_of_users, events)
h2 = (0...number_of_users).to_h { |i| ["id" + i.to_s, true] }
h3 = (0...number_of_users).to_h { |i| ["id" + i.to_s, [0, 0]] }
events.sort! do |a, b|
if a[1].to_i != b[1].to_i
a[1].to_i - b[1].to_i
elsif a[1].to_i == b[1].to_i
b[0][0].ord - a[0][0].ord
end
end
events.each do |e|
if e[0] == "MESSAGE"
if e[2] == "ALL"
h3.each_pair do |k, v|
v[0] += 1
unless h2[k]
if e[1].to_i - v[1] >= 60
v[1] = e[1].to_i
h2[k] = true
end
else
v[1] = e[1].to_i
end
end
elsif e[2] == "HERE"
h3.each_pair do |k, v|
if h2[k]
v[0] += 1
v[1] = e[1].to_i
else
if e[1].to_i - v[1] >= 60
h2[k] = true
v[1] = e[1].to_i
v[0] += 1
end
end
end
else
t = e[2].split(" ")
t1 = Hash.new(0)
t.each do |t2|
t1[t2] += 1
end
h3.each_pair do |k, v|
if t1.has_key?(k)
v[0] += t1[k]
end
end
h3.each_pair do |k, v|
if h2[k]
v[1] = e[1].to_i
else
if e[1].to_i - v[1] >= 60
h2[k] = true
v[1] = e[1].to_i
end
end
end
end
elsif e[0] == "OFFLINE"
h2["id" + e[2]] = false
h3["id" + e[2]][1] = e[1].to_i
h3.each_pair do |k, v|
if h2[k]
v[1] = e[1].to_i
else
if e[1].to_i - v[1] >= 60
h2[k] = true
v[1] = e[1].to_i
end
end
end
end
end
h3.values.map { |it| it[0] }
end