列表

详情


2254. 设计视频共享平台

你有一个视频分享平台,用户可以上传和删除视频。每个 video 都是 字符串 类型的数字,其中字符串的第 i 位表示视频中第 i 分钟的内容。例如,第一个数字表示视频中第 0 分钟的内容,第二个数字表示视频中第 1 分钟的内容,以此类推。视频的观众也可以喜欢和不喜欢视频。该平台会跟踪每个视频的 观看次数点赞次数 不喜欢次数

当视频上传时,它与最小可用整数 videoId 相关联,videoId0 开始的。一旦一个视频被删除,与该视频关联的 videoId 就可以用于另一个视频。

实现 VideoSharingPlatform 类:

 

示例 1:

输入
["VideoSharingPlatform", "upload", "upload", "remove", "remove", "upload", "watch", "watch", "like", "dislike", "dislike", "getLikesAndDislikes", "getViews"]
[[], ["123"], ["456"], [4], [0], ["789"], [1, 0, 5], [1, 0, 1], [1], [1], [1], [1], [1]]
输出
[null, 0, 1, null, null, 0, "456", "45", null, null, null, [1, 2], 2]

解释
VideoSharingPlatform videoSharingPlatform = new VideoSharingPlatform();
videoSharingPlatform.upload("123");          // 最小的可用 videoId 是 0,所以返回 0。
videoSharingPlatform.upload("456");          // 最小的可用 videoId 是 1,所以返回 1。
videoSharingPlatform.remove(4);              // 没有与 videoId 4 相关联的视频,所以什么都不做。
videoSharingPlatform.remove(0);              // 删除与 videoId 0 关联的视频。
videoSharingPlatform.upload("789");          // 由于与 videoId 0 相关联的视频被删除,
                                             // 0 是最小的可用 videoId,所以返回 0。
videoSharingPlatform.watch(1, 0, 5);         // 与 videoId 1 关联的视频为 "456"。
                                             // 从分钟 0 到分钟 min(5,3 - 1)= 2 的视频为 "456",因此返回 "456"。
videoSharingPlatform.watch(1, 0, 1);         // 与 videoId 1 关联的视频为 "456"。
                                             // 从分钟 0 到分钟 min(1,3 - 1)= 1 的视频为 "45",因此返回 "45"。
videoSharingPlatform.like(1);                // 增加与 videoId 1 相关的视频的点赞数。
videoSharingPlatform.dislike(1);             // 增加与 videoId 1 相关联的视频的不喜欢的数量。
videoSharingPlatform.dislike(1);             // 增加与 videoId 1 相关联的视频的不喜欢的数量。
videoSharingPlatform.getLikesAndDislikes(1); // 在与 videoId 1 相关的视频中有 1 个喜欢和 2 个不喜欢,因此返回[1,2]。
videoSharingPlatform.getViews(1);            // 与 videoId 1 相关联的视频有 2 个观看数,因此返回2。

示例 2:

输入
["VideoSharingPlatform", "remove", "watch", "like", "dislike", "getLikesAndDislikes", "getViews"]
[[], [0], [0, 0, 1], [0], [0], [0], [0]]
输出
[null, null, "-1", null, null, [-1], -1]

解释
VideoSharingPlatform videoSharingPlatform = new VideoSharingPlatform();
videoSharingPlatform.remove(0);              // 没有与 videoId 0 相关联的视频,所以什么都不做。
videoSharingPlatform.watch(0, 0, 1);         // 没有与 videoId 0 相关联的视频,因此返回 "-1"。
videoSharingPlatform.like(0);                // 没有与 videoId 0 相关联的视频,所以什么都不做。
videoSharingPlatform.dislike(0);             // 没有与 videoId 0 相关联的视频,所以什么都不做。
videoSharingPlatform.getLikesAndDislikes(0); // 没有与 videoId 0 相关联的视频,因此返回 [-1]。
videoSharingPlatform.getViews(0);            // 没有视频与 videoId 0 相关联,因此返回 -1。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class VideoSharingPlatform { public: VideoSharingPlatform() { } int upload(string video) { } void remove(int videoId) { } string watch(int videoId, int startMinute, int endMinute) { } void like(int videoId) { } void dislike(int videoId) { } vector<int> getLikesAndDislikes(int videoId) { } int getViews(int videoId) { } }; /** * Your VideoSharingPlatform object will be instantiated and called as such: * VideoSharingPlatform* obj = new VideoSharingPlatform(); * int param_1 = obj->upload(video); * obj->remove(videoId); * string param_3 = obj->watch(videoId,startMinute,endMinute); * obj->like(videoId); * obj->dislike(videoId); * vector<int> param_6 = obj->getLikesAndDislikes(videoId); * int param_7 = obj->getViews(videoId); */

java 解法, 执行用时: 100 ms, 内存消耗: 97.5 MB, 提交时间: 2023-10-22 09:43:41

class VideoSharingPlatform {

    PriorityQueue<Integer> pq = new PriorityQueue<>();
    HashMap<Integer, Video> map = new HashMap<>();


    public VideoSharingPlatform() {

    }
    
    public int upload(String video) {
        if (pq.isEmpty()) {
            pq.add(map.size());
        }
        int index = pq.poll();
        map.put(index, new Video(video));
        return index;
    }
    
    public void remove(int videoId) {
        if (map.containsKey(videoId)) {
            map.remove(videoId);
            pq.add(videoId);
        }

    }
    
    public String watch(int videoId, int startMinute, int endMinute) {
        if (map.containsKey(videoId)) {
            map.get(videoId).watchNum++;
            return map.get(videoId).name.substring(startMinute, Math.min(endMinute + 1, map.get(videoId).name.length()));
        }
        return "-1";
    }
    
    public void like(int videoId) {
        if (map.containsKey(videoId)) {
            map.get(videoId).like += 1;
        }
    }
    
    public void dislike(int videoId) {
        if (map.containsKey(videoId)) {
            map.get(videoId).dislike += 1;
        }

    }
    
    public int[] getLikesAndDislikes(int videoId) {
        if (map.containsKey(videoId)) {
            return new int[] {map.get(videoId).like, map.get(videoId).dislike};
        }
        return new int[] {-1};
    }
    
    public int getViews(int videoId) {
        if (map.containsKey(videoId)) {
            return map.get(videoId).watchNum;
        }
        return -1;
    }
}

class Video {
    public String name;
    public int id = 0;
    public int like = 0;
    public int dislike = 0;
    public int watchNum = 0;

    Video(String name) {
        this.name = name;
    }
}

/**
 * Your VideoSharingPlatform object will be instantiated and called as such:
 * VideoSharingPlatform obj = new VideoSharingPlatform();
 * int param_1 = obj.upload(video);
 * obj.remove(videoId);
 * String param_3 = obj.watch(videoId,startMinute,endMinute);
 * obj.like(videoId);
 * obj.dislike(videoId);
 * int[] param_6 = obj.getLikesAndDislikes(videoId);
 * int param_7 = obj.getViews(videoId);
 */

cpp 解法, 执行用时: 636 ms, 内存消耗: 314.6 MB, 提交时间: 2023-10-22 09:43:18

class VideoSharingPlatform {
public:
    priority_queue<int,vector<int>,greater<int> >  heap;

    unordered_map<string, int> str_to_id;
    unordered_map<int, string>id_to_str;
    unordered_map<int, int>likes, dislikes, views;
    int id;

    VideoSharingPlatform() {
        for(int i = 0; i < 100005; i++)
            heap.push(i);
    }
    
    int upload(string video) {
        id = heap.top();         // upload 的时候释放最小可用 id
        heap.pop();

        str_to_id[video] = id;
        id_to_str[id] = video;
        likes[id] = 0;
        dislikes[id] = 0;
        views[id] = 0;
        return id;
    }
    
    void remove(int videoId) {
        if (id_to_str.count(videoId) < 1)
            return;
        string video = id_to_str[videoId];
        id_to_str.erase(videoId);
        str_to_id.erase(video);
        likes.erase(videoId);
        dislikes.erase(videoId);
        views.erase(videoId);

        heap.push(videoId);            // remove 的时候收回 id
    }
    
    string watch(int videoId, int startMinute, int endMinute) {
        if (id_to_str.count(videoId) < 1)
            return "-1";
        string video = id_to_str[videoId];
        views[videoId] ++;
        return video.substr(startMinute, min(endMinute, (int)(video.size()) - 1) - startMinute + 1);
    }
    
    void like(int videoId) {
        if (id_to_str.count(videoId) < 1)
            return;
        likes[videoId] ++;
    }
    
    void dislike(int videoId) {
        if (id_to_str.count(videoId) < 1)
            return;
        dislikes[videoId] ++;
    }
    
    vector<int> getLikesAndDislikes(int videoId) {
        if (id_to_str.count(videoId) < 1)
            return vector<int>{-1};
        return vector<int>{likes[videoId], dislikes[videoId]};
    }
    
    int getViews(int videoId) {
        if (id_to_str.count(videoId) < 1)
            return -1;
        return views[videoId];
    }
};

/**
 * Your VideoSharingPlatform object will be instantiated and called as such:
 * VideoSharingPlatform* obj = new VideoSharingPlatform();
 * int param_1 = obj->upload(video);
 * obj->remove(videoId);
 * string param_3 = obj->watch(videoId,startMinute,endMinute);
 * obj->like(videoId);
 * obj->dislike(videoId);
 * vector<int> param_6 = obj->getLikesAndDislikes(videoId);
 * int param_7 = obj->getViews(videoId);
 */

python3 解法, 执行用时: 336 ms, 内存消耗: 69.4 MB, 提交时间: 2023-10-22 09:42:53

import heapq

class VideoSharingPlatform:

    def __init__(self):
        self.dct = dict()
        self.id = list()
        self.likes = dict()
        self.dislikes = dict()
        self.view = dict()

    def upload(self, video: str) -> int:
        if not self.id:
            i = len(self.dct)
            self.dct[i] = video
            return i
        i = heapq.heappop(self.id)
        self.dct[i] = video
        return i

    def remove(self, videoId: int) -> None:
        if videoId in self.dct:
            del self.dct[videoId]
            heapq.heappush(self.id, videoId)
            if videoId in self.likes:
                del self.likes[videoId]
            if videoId in self.dislikes:
                del self.dislikes[videoId]
            if videoId in self.view:
                del self.view[videoId]
        return

    def watch(self, videoId: int, startMinute: int, endMinute: int) -> str:
        if videoId in self.dct:
            self.view[videoId] = self.view.get(videoId, 0) + 1
            return self.dct[videoId][startMinute: endMinute+1]
        return "-1"

    def like(self, videoId: int) -> None:
        if videoId in self.dct:
            self.likes[videoId] = self.likes.get(videoId, 0) + 1
        return

    def dislike(self, videoId: int) -> None:
        if videoId in self.dct:
            self.dislikes[videoId] = self.dislikes.get(videoId, 0) + 1
        return

    def getLikesAndDislikes(self, videoId: int):
        if videoId in self.dct:
            return [self.likes.get(videoId, 0), self.dislikes.get(videoId, 0)]
        return [-1]

    def getViews(self, videoId: int) -> int:
        if videoId in self.dct:
            return self.view.get(videoId, 0)
        return -1

# Your VideoSharingPlatform object will be instantiated and called as such:
# obj = VideoSharingPlatform()
# param_1 = obj.upload(video)
# obj.remove(videoId)
# param_3 = obj.watch(videoId,startMinute,endMinute)
# obj.like(videoId)
# obj.dislike(videoId)
# param_6 = obj.getLikesAndDislikes(videoId)
# param_7 = obj.getViews(videoId)

python3 解法, 执行用时: 736 ms, 内存消耗: 195.9 MB, 提交时间: 2023-10-22 09:42:11

class Video:
    def __init__(self, content: str):
        self.content = content
        self.platform: Optional['VideoSharingPlatform'] = None
        self.id = -1
        self.view = 0
        self.like = 0
        self.dislike = 0

    def register(self, platform: 'VideoSharingPlatform') -> int:
        self.platform = platform
        self.id = heappop(self.platform.idPool)
        self.platform.idByVideo[self] = self.id
        self.platform.videoById[self.id] = self
        return self.id

    def unregister(self) -> int:
        if self.platform is None:
            return -1
        self.platform.idByVideo.pop(self)
        self.platform.videoById.pop(self.id)
        heappush(self.platform.idPool, self.id)
        self.platform = None
        return self.id

    def addView(self, count=1) -> int:
        self.view += count
        return self.view

    def addLike(self, count=1) -> int:
        self.like += count
        return self.like

    def addDislike(self, count=1) -> int:
        self.dislike += count
        return self.dislike

    def __getitem__(self, index: Any) -> str:
        return self.content[index]


class VideoSharingPlatform:
    """设计电影分享平台
    - 每部电影用数字字符串s表示,s[i]表示第i分钟的电影内容
    - 用户可以对电影点赞和点踩
    - 电影平台的管理人员需要统计每部电影的观看数、点赞数、点踩数
    - 电影的id是从0全局自增的,如果电影被删除,那么被删除的电影id可以被重新使用

    所有参数量级都是1e5
    """

    def __init__(self):
        self.idPool = list(range(int(1e5 + 10)))
        self.videoById: Dict[int, Video] = dict()
        self.idByVideo: Dict[Video, int] = dict()

    def upload(self, video: str) -> int:
        """上传电影,返回电影id"""
        newVideo = Video(video)
        return newVideo.register(self)

    def remove(self, videoId: int) -> None:
        """删除电影"""
        if videoId in self.videoById:
            self.videoById[videoId].unregister()

    def watch(self, videoId: int, startMinute: int, endMinute: int) -> str:
        """看电影,如果这部电影存在,那么观看数+1并返回看的这一段电影内容(闭区间),否则返回"-1"""
        if videoId not in self.videoById:
            return '-1'
        self.videoById[videoId].addView(1)
        return self.videoById[videoId][startMinute : endMinute + 1]

    def like(self, videoId: int) -> None:
        """点赞,如果这部电影存在"""
        if videoId in self.videoById:
            self.videoById[videoId].addLike(1)

    def dislike(self, videoId: int) -> None:
        """点踩,如果这部电影存在"""
        if videoId in self.videoById:
            self.videoById[videoId].addDislike(1)

    def getLikesAndDislikes(self, videoId: int) -> List[int]:
        """返回这部电影的[点赞数,点踩数];如果这部电影不存在,返回[-1]"""
        if videoId not in self.videoById:
            return [-1]
        return [self.videoById[videoId].like, self.videoById[videoId].dislike]

    def getViews(self, videoId: int) -> int:
        """返回这部电影的观看数;如果这部电影不存在,返回-1"""
        if videoId not in self.videoById:
            return -1
        return self.videoById[videoId].view

# Your VideoSharingPlatform object will be instantiated and called as such:
# obj = VideoSharingPlatform()
# param_1 = obj.upload(video)
# obj.remove(videoId)
# param_3 = obj.watch(videoId,startMinute,endMinute)
# obj.like(videoId)
# obj.dislike(videoId)
# param_6 = obj.getLikesAndDislikes(videoId)
# param_7 = obj.getViews(videoId)

上一题