C++
Java
Python
Python3
C
C#
JavaScript
TypeScript
PHP
Swift
Kotlin
Dart
Go
Ruby
Scala
Rust
Racket
Erlang
Elixir
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
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)