Approach
Breadth-first search
For Design Video Sharing Platform, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 60 lines of Java from the credited upstream file 2254.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class VideoSharingPlatform {2 public int upload(String video) {3 final int videoId = getVideoId();4 videoIdToVideo.put(videoId, video);5 return videoId;6 }7 8 public void remove(int videoId) {9 if (videoIdToVideo.containsKey(videoId)) {10 usedIds.offer(videoId);11 videoIdToVideo.remove(videoId);12 videoIdToViews.remove(videoId);13 videoIdToLikes.remove(videoId);14 videoIdToDislikes.remove(videoId);15 }16 }17 18 public String watch(int videoId, int startMinute, int endMinute) {19 if (!videoIdToVideo.containsKey(videoId))20 return "-1";21 videoIdToViews.merge(videoId, 1, Integer::sum);22 final String video = videoIdToVideo.get(videoId);23 return video.substring(startMinute, Math.min(endMinute + 1, video.length()));24 }25 26 public void like(int videoId) {27 if (videoIdToVideo.containsKey(videoId))28 videoIdToLikes.merge(videoId, 1, Integer::sum);29 }30 31 public void dislike(int videoId) {32 if (videoIdToVideo.containsKey(videoId))33 videoIdToDislikes.merge(videoId, 1, Integer::sum);34 }35 36 public int[] getLikesAndDislikes(int videoId) {37 return videoIdToVideo.containsKey(videoId)38 ? new int[] {videoIdToLikes.getOrDefault(videoId, 0),39 videoIdToDislikes.getOrDefault(videoId, 0)}40 : new int[] {-1};41 }42 43 public int getViews(int videoId) {44 return videoIdToVideo.containsKey(videoId) ? videoIdToViews.getOrDefault(videoId, 0) : -1;45 }46 47 private int currVideoId = 0;48 private Queue<Integer> usedIds = new PriorityQueue<>();49 private Map<Integer, String> videoIdToVideo = new HashMap<>();50 private Map<Integer, Integer> videoIdToViews = new HashMap<>();51 private Map<Integer, Integer> videoIdToLikes = new HashMap<>();52 private Map<Integer, Integer> videoIdToDislikes = new HashMap<>();53 54 private int getVideoId() {55 if (usedIds.isEmpty())56 return currVideoId++;57 return usedIds.poll();58 }59}60