Music Playlist
Examples
Example 1:
Input: ["MusicPlaylist", "add", "add", "add", "getAll", "remove", "getAll", "removeAll", "getAll"]
Output: [null, null, null, null, ["song1", "song2", "song1"], true, ["song1", "song1"], 2, []]
Explanation:
Example 1:
Input: ["MusicPlaylist", "add", "add", "add", "getAll", "remove", "getAll", "removeAll", "getAll"]
Output: [null, null, null, null, ["song1", "song2", "song1"], true, ["song1", "song1"], 2, []]
Explanation:
Design a data structure that tracks music listening history with timestamps.
Implement the MusicPlaylist class:
MusicPlaylist() Initializes the playlist object.void add(String song, int timestamp) Adds a record that the song was listened to at the given timestamp.List<String> getAll() Returns all songs in the order they were listened to (sorted by timestamp). If multiple songs have the same timestamp, return them in the order they were added.boolean remove(String song, int timestamp) Removes the listening record for the specific song at the specific timestamp. Returns true if the record existed and was removed, false otherwise.int removeAll(String song) Removes all listening records for the given song. Returns the number of records removed.MusicPlaylist playlist = new MusicPlaylist(); playlist.add("song1", 1); // Listen to song1 at time 1 playlist.add("song2", 2); // Listen to song2 at time 2 playlist.add("song1", 3); // Listen to song1 again at time 3 playlist.getAll(); // Returns ["song1", "song2", "song1"] (sorted by timestamp) playlist.remove("song2", 2); // Remove song2 at time 2, returns true playlist.getAll(); // Returns ["song1", "song1"] playlist.removeAll("song1"); // Remove all song1 records, returns 2 playlist.getAll(); // Returns []
1 <= song.length <= 1000 <= timestamp <= 10^910^4 calls will be made to add, getAll, remove, and removeAll.