Build a simple class that implements a basic in-memory service that:
- Records a song play event for a given song id
- Returns play counts for individual songs
- Computes and returns the top K trending songs
- Returns a SQL query that would compute trending songs from a database
这道题要求设计一个简单的内存音乐服务,用哈希表记录每个 song id 的播放次数,并支持单曲计数查询和按热度返回 Top K 歌曲。核心思路是用 Map<String, Integer> 维护计数;如果只需要一次性求前 K 名,可以在查询时把所有歌曲按播放次数排序,或使用最小堆将复杂度优化到 O(N log K)。另外还要求写出数据库版本的思路,通常对应对播放事件表按 song_id 分组并按 count 排序的 SQL。
正文完