Problem
You have a video sharing platform where users can upload and delete videos.
Each video
is a string of digits, where the ith
digit of the string represents the content of the video at minute i
. For example, the first digit represents the content at minute 0
in the video, the second digit represents the content at minute 1
in the video, and so on. Viewers of videos can also like and dislike videos. Internally, the platform keeps track of the number of views, likes, and dislikes on each video.
When a video is uploaded, it is associated with the smallest available integer
videoId
starting from 0
. Once a video is deleted, the videoId
associated with that video can be reused for another video.
Implement the VideoSharingPlatform
class:
VideoSharingPlatform()
Initializes the object.int upload(String video)
The user uploads avideo
. Return thevideoId
associated with the video.void remove(int videoId)
If there is a video associated withvideoId
, remove the video.String watch(int videoId, int startMinute, int endMinute)
If there is a video associated withvideoId
, increase the number of views on the video by1
and return the substring of the video string starting atstartMinute
and ending atmin(endMinute, video.length - 1``)
(inclusive). Otherwise, return"-1"
.void like(int videoId)
Increases the number of likes on the video associated withvideoId
by1
if there is a video associated withvideoId
.void dislike(int videoId)
Increases the number of dislikes on the video associated withvideoId
by1
if there is a video associated withvideoId
.int[] getLikesAndDislikes(int videoId)
Return a 0-indexed integer arrayvalues
of length2
wherevalues[0]
is the number of likes andvalues[1]
is the number of dislikes on the video associated withvideoId
. If there is no video associated withvideoId
, return[-1]
.int getViews(int videoId)
Return the number of views on the video associated withvideoId
, if there is no video associated withvideoId
, return-1
.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= video.length <= 10^5
- The sum of
video.length
over all calls toupload
does not exceed105
video
consists of digits.0 <= videoId <= 10^5
0 <= startMinute < endMinute < 10^5
startMinute < video.length
- The sum of
endMinute - startMinute
over all calls towatch
does not exceed105
. - At most
105
calls in total will be made to all functions.
Solution
Method 1 – Hash Maps, Min Heap, and Struct for Video Info
Intuition
We need to efficiently manage video uploads, removals, and queries for views, likes, and dislikes, while reusing the smallest available videoId. A min heap can track available ids, and a hash map can store video info.
Approach
- Use a min heap to track available videoIds. If empty, use the next sequential id.
- Use a hash map to store video info: content, views, likes, dislikes for each videoId.
- For
upload
, assign the smallest available id, store the video, and return the id. - For
remove
, delete the video and add its id to the heap. - For
watch
, if the video exists, increment views and return the substring; else return “-1”. - For
like
/dislike
, increment the respective counter if the video exists. - For
getLikesAndDislikes
, return [likes, dislikes] or [-1] if not found. - For
getViews
, return views or -1 if not found.
Code
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(log n)
for upload/remove (heap),O(1)
for other operations. - 🧺 Space complexity:
O(n)
, wheren
is the number of videos stored.