根据我的要求,为歌曲播放站设计课程的数据结构是什么?

时间:2017-03-04 08:11:01

标签: algorithm data-structures framework-design

我必须为“度量”系统提出类和数据结构设计,以确定*乐队的顶级歌曲。

该类应具有两个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";

请指教!

3 个答案:

答案 0 :(得分:2)

如果我理解正确,你需要维护一个字典,其中key是band name,value是优先级队列。优先级队列中的每个对象将具有“歌曲名称”和“播放计数”属性,并且优先级队列需要按“播放计数”属性排序。每次播放一首歌曲时,增加它的播放次数并堆叠队列。

执行上述操作有点复杂,基于编程语言,实现方法可能会有很大差异。你不应该这样做,除非一个乐队的歌曲数量可能很大,这是不太可能的。

无论如何,这是实际的实施细节。这些问题的教科书答案总是优先排队。

答案 1 :(得分:0)

如果保留在内存中,这看起来像是哈希表,关联数组或字典的作业。如果保存在持久存储中,数据库将是最简单的方法。

答案 2 :(得分:0)

您需要两个存储空间。

  1. bandname映射到其顶部songname的字典。我们称之为topBandSong
  2. (bandname, songname)映射到此歌曲播放次数的字典。我们称之为bandSongPopularity
  3. 这个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 + "\000" + 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 + "\000" + 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。