我必须为"公制"系统提出一个类和数据结构设计,以确定一个 * band的顶部歌曲。
课程应该有两个Web服务调用
void play(String bandname, String songname);/*This method has to play song with the requested brandname and songname. Also have to keep track of the song payed count to support the below method.*/
String topSong(String bandname);/* This method has to play mostly played song under the requested brand*/
Sample inputs:
BrandName:"Lady Gaga", Song : "Pokerface";
BrandName:"Lady Gaga", Song : "Pokerface";
BrandName:"Lady Gaga", Song : "Alejandro";
BrandName:"Bruno Mars",Song : "Treasure";
请建议!
如果我正确理解,则需要维护一个字典,其中键是band名称和值是优先级队列。优先队列中的每个对象都将具有"歌曲名称"one_answers" play count"属性,优先队列需要按" play count"属性进行排序。每次播放歌曲时,都会播放播放数并堆积队列。
做上述内容有点复杂,基于编程语言,实现方法可能会差异很大。除非乐队的歌曲数量很大,否则您不应该这样做,这不太可能。
无论如何,这是实际的实施细节。此类问题的教科书答案始终是优先的队列。
如果保存在内存中,这看起来像是哈希表,关联数组或字典的作业。如果保留在持久的商店中,数据库将是最简单的方法。
您需要两个存储。
- 映射
bandname
的字典映射到其顶部songname
。我们称其为topBandSong
。 - 将
(bandname, songname)
映射到这首歌播放的次数的字典。我们称其为bandSongPopularity
。
使用此topSong
非常简单(抛开对乐队尚未了解的情况):
Map<String, String> topBandSong = new HashMap();
String topSong(String bandname) {
return topBandSong.get(bandname);
}
play
功能必须更新两个地图。这真的很容易:
Map<String, BigInteger> bandSongPopularity = new HashMap();
void play(String bandname, String songname) {
/* First update bandSongPopularity */
String k = bandname + " 00" + songname;
BigInteger popularity = bandSongPopularity.get(k);
if (popularity == null) {
popularity = BigInteger.valueOf(1);
}
else {
popularity = popularity.add(BigInteger.valueOf(1));
}
bandSongPopularity.put(k, popularity);
/* then update topBandSong */
String top = topSong(bandname);
if (top == null) {
topBandSong.put(bandname, songname);
}
else {
String topK = bandname + " 00" + top;
BigInteger topPopularity = bandSongPopularity.get(topK);
/* topPopularity can not be NULL */
if (popularity.compareTo(topPopularity) > 0) {
topBandSong.put(bandname, songname);
}
}
/* Finally really play this song. But this is left as an exercise ;-) */
}
复杂性是您选择的基本字典之一,即树或哈希。
请注意,代码保留字符编号0。