Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 532d15e

Browse files
committed
1490-1912-2254-2408-2590-3253-3279-3284-3323-3329 (10)
1 parent a220e4c commit 532d15e

File tree

20 files changed

+1020
-0
lines changed

20 files changed

+1020
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class SolutionP1490 {
5+
public Node cloneTree(Node root) {
6+
if (root == null) {
7+
return null;
8+
}
9+
Node cloneRoot = new Node(root.val);
10+
List<Node> children = root.children;
11+
for (Node child : children) {
12+
Node cloneChild = cloneTree(child);
13+
cloneRoot.children.add(cloneChild);
14+
}
15+
return cloneRoot;
16+
}
17+
18+
// UT
19+
static class Node {
20+
public int val;
21+
public List<Node> children;
22+
23+
public Node() {
24+
children = new ArrayList<>();
25+
}
26+
27+
public Node(int _val) {
28+
val = _val;
29+
children = new ArrayList<>();
30+
}
31+
32+
public Node(int _val, ArrayList<Node> _children) {
33+
val = _val;
34+
children = _children;
35+
}
36+
}
37+
}
38+
/*
39+
1490ドル. 克隆 N 叉树
40+
https://leetcode.cn/problems/clone-n-ary-tree/description/
41+
42+
给定一棵 N 叉树的根节点 root ,返回该树的深拷贝(克隆)。
43+
N 叉树的每个节点都包含一个值( int )和子节点的列表( List[Node] )。
44+
class Node {
45+
public int val;
46+
public List<Node> children;
47+
}
48+
N 叉树的输入序列用层序遍历表示,每组子节点用 null 分隔(见示例)。
49+
提示:
50+
给定的 N 叉树的深度小于或等于 1000。
51+
节点的总个数在 [0, 10^4] 之间
52+
进阶:你的解决方案可以适用于克隆图问题吗?
53+
54+
层序遍历。
55+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
import java.util.List;
5+
6+
public class SolutionP1490Tests {
7+
private final SolutionP1490 solutionP1490 = new SolutionP1490();
8+
9+
@Test
10+
public void example1() {
11+
SolutionP1490.Node node1 = new SolutionP1490.Node(1);
12+
SolutionP1490.Node node2 = new SolutionP1490.Node(2);
13+
SolutionP1490.Node node3 = new SolutionP1490.Node(3);
14+
SolutionP1490.Node node4 = new SolutionP1490.Node(4);
15+
SolutionP1490.Node node5 = new SolutionP1490.Node(5);
16+
SolutionP1490.Node node6 = new SolutionP1490.Node(6);
17+
node1.children = List.of(node3, node2, node4);
18+
node3.children = List.of(node5, node6);
19+
Assertions.assertTrue(UtUtils.assertJsonEquals(node1, solutionP1490.cloneTree(node1)));
20+
}
21+
22+
@Test
23+
public void example2() {
24+
SolutionP1490.Node node1 = new SolutionP1490.Node(1);
25+
SolutionP1490.Node node2 = new SolutionP1490.Node(2);
26+
SolutionP1490.Node node3 = new SolutionP1490.Node(3);
27+
SolutionP1490.Node node4 = new SolutionP1490.Node(4);
28+
SolutionP1490.Node node5 = new SolutionP1490.Node(5);
29+
SolutionP1490.Node node6 = new SolutionP1490.Node(6);
30+
SolutionP1490.Node node7 = new SolutionP1490.Node(7);
31+
SolutionP1490.Node node8 = new SolutionP1490.Node(8);
32+
SolutionP1490.Node node9 = new SolutionP1490.Node(9);
33+
SolutionP1490.Node node10 = new SolutionP1490.Node(10);
34+
SolutionP1490.Node node11 = new SolutionP1490.Node(11);
35+
SolutionP1490.Node node12 = new SolutionP1490.Node(12);
36+
SolutionP1490.Node node13 = new SolutionP1490.Node(13);
37+
SolutionP1490.Node node14 = new SolutionP1490.Node(14);
38+
node1.children = List.of(node2, node3, node4, node5);
39+
node3.children = List.of(node6, node7);
40+
node4.children = List.of(node8);
41+
node5.children = List.of(node9, node10);
42+
node7.children = List.of(node11);
43+
node8.children = List.of(node12);
44+
node9.children = List.of(node13);
45+
node11.children = List.of(node14);
46+
Assertions.assertTrue(UtUtils.assertJsonEquals(node1, solutionP1490.cloneTree(node1)));
47+
}
48+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Comparator;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.TreeSet;
8+
9+
public class Solution1912 {
10+
static class MovieRentingSystem {
11+
Map<ShopMovie, Integer> tPrice; // 存储(shop, movie) -> price的映射
12+
Map<Integer, TreeSet<MovieEntry>> tValid; // 存储movie -> 有序的(price, shop)集合
13+
TreeSet<RentEntry> tRent; // 存储已租借的有序(price, shop, movie)集合
14+
15+
record ShopMovie(int shop, int movie) {
16+
}
17+
18+
record MovieEntry(int price, int shop) {
19+
static Comparator<MovieEntry> comparator = (o1, o2) -> {
20+
if (o1.price != o2.price) return Integer.compare(o1.price, o2.price);
21+
return Integer.compare(o1.shop, o2.shop);
22+
};
23+
}
24+
25+
record RentEntry(int price, int shop, int movie) {
26+
static Comparator<RentEntry> comparator = (o1, o2) -> {
27+
if (o1.price != o2.price) return Integer.compare(o1.price, o2.price);
28+
if (o1.shop != o2.shop) return Integer.compare(o1.shop, o2.shop);
29+
return Integer.compare(o1.movie, o2.movie);
30+
};
31+
}
32+
33+
public MovieRentingSystem(int n, int[][] entries) {
34+
tPrice = new HashMap<>();
35+
tValid = new HashMap<>();
36+
tRent = new TreeSet<>(RentEntry.comparator);
37+
for (int[] en : entries) {
38+
int shop = en[0], movie = en[1], price = en[2];
39+
tPrice.put(new ShopMovie(shop, movie), price);
40+
tValid.computeIfAbsent(movie, e -> new TreeSet<>(MovieEntry.comparator)).add(new MovieEntry(price, shop));
41+
}
42+
}
43+
44+
public List<Integer> search(int movie) {
45+
TreeSet<MovieEntry> set = tValid.get(movie);
46+
if (set == null) return new ArrayList<>();
47+
return set.stream().limit(5).map(entry -> entry.shop).toList();
48+
}
49+
50+
public void rent(int shop, int movie) {
51+
int price = tPrice.get(new ShopMovie(shop, movie));
52+
tValid.get(movie).remove(new MovieEntry(price, shop));
53+
tRent.add(new RentEntry(price, shop, movie));
54+
}
55+
56+
public void drop(int shop, int movie) {
57+
int price = tPrice.get(new ShopMovie(shop, movie));
58+
tRent.remove(new RentEntry(price, shop, movie));
59+
tValid.get(movie).add(new MovieEntry(price, shop));
60+
}
61+
62+
public List<List<Integer>> report() {
63+
return tRent.stream().limit(5).map(entry -> Arrays.asList(entry.shop, entry.movie)).toList();
64+
}
65+
}
66+
}
67+
/*
68+
1912. 设计电影租借系统
69+
https://leetcode.cn/problems/design-movie-rental-system/description/
70+
71+
你有一个电影租借公司和 n 个电影商店。你想要实现一个电影租借系统,它支持查询、预订和返还电影的操作。同时系统还能生成一份当前被借出电影的报告。
72+
所有电影用二维整数数组 entries 表示,其中 entries[i] = [shopi, moviei, pricei] 表示商店 shopi 有一份电影 moviei 的拷贝,租借价格为 pricei 。每个商店有 至多一份 编号为 moviei 的电影拷贝。
73+
系统需要支持以下操作:
74+
- Search:找到拥有指定电影且 未借出 的商店中 最便宜的 5 个 。商店需要按照 价格 升序排序,如果价格相同,则 shopi 较小 的商店排在前面。如果查询结果少于 5 个商店,则将它们全部返回。如果查询结果没有任何商店,则返回空列表。
75+
- Rent:从指定商店借出指定电影,题目保证指定电影在指定商店 未借出 。
76+
- Drop:在指定商店返还 之前已借出 的指定电影。
77+
- Report:返回 最便宜的 5 部已借出电影 (可能有重复的电影 ID),将结果用二维列表 res 返回,其中 res[j] = [shopj, moviej] 表示第 j 便宜的已借出电影是从商店 shopj 借出的电影 moviej 。res 中的电影需要按 价格 升序排序;如果价格相同,则 shopj 较小 的排在前面;如果仍然相同,则 moviej 较小 的排在前面。如果当前借出的电影小于 5 部,则将它们全部返回。如果当前没有借出电影,则返回一个空的列表。
78+
请你实现 MovieRentingSystem 类:
79+
- MovieRentingSystem(int n, int[][] entries) 将 MovieRentingSystem 对象用 n 个商店和 entries 表示的电影列表初始化。
80+
- List<Integer> search(int movie) 如上所述,返回 未借出 指定 movie 的商店列表。
81+
- void rent(int shop, int movie) 从指定商店 shop 借出指定电影 movie 。
82+
- void drop(int shop, int movie) 在指定商店 shop 返还之前借出的电影 movie 。
83+
- List<List<Integer>> report() 如上所述,返回最便宜的 已借出 电影列表。
84+
注意:测试数据保证 rent 操作中指定商店拥有 未借出 的指定电影,且 drop 操作指定的商店 之前已借出 指定电影。
85+
提示:
86+
1 <= n <= 3 * 10^5
87+
1 <= entries.length <= 10^5
88+
0 <= shopi < n
89+
1 <= moviei, pricei <= 10^4
90+
每个商店 至多 有一份电影 moviei 的拷贝。
91+
search,rent,drop 和 report 的调用 总共 不超过 10^5 次。
92+
93+
哈希表模拟 + 平衡树
94+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
import java.util.List;
5+
6+
public class Solution1912Tests {
7+
@Test
8+
public void example1() {
9+
int n = 3;
10+
int[][] entries = UtUtils.stringToInts2("[[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]");
11+
Solution1912.MovieRentingSystem movieRentingSystem = new Solution1912.MovieRentingSystem(n, entries);
12+
13+
// 返回 [1, 0, 2] ,商店 1,0 和 2 有未借出的 ID 为 1 的电影。商店 1 最便宜,商店 0 和 2 价格相同,所以按商店编号排序。
14+
Assertions.assertEquals(List.of(1, 0, 2), movieRentingSystem.search(1));
15+
16+
// 从商店 0 借出电影 1 。现在商店 0 未借出电影编号为 [2,3] 。
17+
movieRentingSystem.rent(0, 1);
18+
19+
// 从商店 1 借出电影 2 。现在商店 1 未借出的电影编号为 [1] 。
20+
movieRentingSystem.rent(1, 2);
21+
22+
// 返回 [[0, 1], [1, 2]] 。商店 0 借出的电影 1 最便宜,然后是商店 1 借出的电影 2 。
23+
List<List<Integer>> expected = UtUtils.stringToIntegerList2("[[0, 1], [1, 2]]");
24+
Assertions.assertEquals(expected, movieRentingSystem.report());
25+
26+
// 在商店 1 返还电影 2 。现在商店 1 未借出的电影编号为 [1,2] 。
27+
movieRentingSystem.drop(1, 2);
28+
29+
// 返回 [0, 1] 。商店 0 和 1 有未借出的 ID 为 2 的电影。商店 0 最便宜,然后是商店 1 。
30+
Assertions.assertEquals(List.of(0, 1), movieRentingSystem.search(2));
31+
}
32+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
import java.util.PriorityQueue;
4+
5+
public class SolutionP2254 {
6+
static class VideoSharingPlatform {
7+
PriorityQueue<Integer> idPool = new PriorityQueue<>();
8+
Map<Integer, Video> videoById = new HashMap<>();
9+
10+
public VideoSharingPlatform() {
11+
}
12+
13+
public int upload(String video) {
14+
if (idPool.isEmpty()) idPool.add(videoById.size());
15+
int id = idPool.poll();
16+
videoById.put(id, new Video(video));
17+
return id;
18+
}
19+
20+
public void remove(int videoId) {
21+
if (videoById.containsKey(videoId)) {
22+
videoById.remove(videoId);
23+
idPool.add(videoId);
24+
}
25+
}
26+
27+
public String watch(int videoId, int startMinute, int endMinute) {
28+
if (!videoById.containsKey(videoId)) return "-1";
29+
Video video = videoById.get(videoId);
30+
video.view += 1;
31+
String content = video.content;
32+
return content.substring(startMinute, Math.min(endMinute + 1, content.length()));
33+
}
34+
35+
public void like(int videoId) {
36+
if (videoById.containsKey(videoId)) {
37+
videoById.get(videoId).like += 1;
38+
}
39+
}
40+
41+
public void dislike(int videoId) {
42+
if (videoById.containsKey(videoId)) {
43+
videoById.get(videoId).dislike += 1;
44+
}
45+
}
46+
47+
public int[] getLikesAndDislikes(int videoId) {
48+
if (!videoById.containsKey(videoId)) return new int[]{-1};
49+
Video video = videoById.get(videoId);
50+
return new int[]{video.like, video.dislike};
51+
}
52+
53+
public int getViews(int videoId) {
54+
if (!videoById.containsKey(videoId)) return -1;
55+
return videoById.get(videoId).view;
56+
}
57+
58+
static class Video {
59+
String content;
60+
int id, view, like, dislike;
61+
62+
public Video(String content) {
63+
this.content = content;
64+
}
65+
}
66+
}
67+
}
68+
/*
69+
2254ドル. 设计视频共享平台
70+
https://leetcode.cn/problems/design-video-sharing-platform/description/
71+
72+
你有一个视频分享平台,用户可以上传和删除视频。每个 video 都是 字符串 类型的数字,其中字符串的第 i 位表示视频中第 i 分钟的内容。例如,第一个数字表示视频中第 0 分钟的内容,第二个数字表示视频中第 1 分钟的内容,以此类推。视频的观众也可以喜欢和不喜欢视频。该平台会跟踪每个视频的 观看次数、点赞次数 和 不喜欢次数。
73+
当视频上传时,它与最小可用整数 videoId 相关联,videoId 从 0 开始的。一旦一个视频被删除,与该视频关联的 videoId 就可以用于另一个视频。
74+
实现 VideoSharingPlatform 类:
75+
- VideoSharingPlatform() 初始化对象。
76+
- int upload(String video) 用户上传一个 video. 返回与视频相关联的videoId 。
77+
- void remove(int videoId) 如果存在与 videoId 相关联的视频,则删除该视频。
78+
- String watch(int videoId, int startMinute, int endMinute) 如果有一个视频与 videoId 相关联,则将该视频的观看次数增加 1,并返回视频字符串的子字符串,从 startMinute 开始,以 min(endMinute, video.length - 1)(含边界) 结束。否则,返回 "-1"。
79+
- void like(int videoId) 如果存在与 videoId 相关联的视频,则将与 videoId 相关联的视频的点赞数增加 1。
80+
- void dislike(int videoId) 如果存在与 videoId 相关联的视频,则将与 videoId 相关联的视频上的不喜欢次数增加 1。
81+
- int[] getLikesAndDislikes(int videoId) 返回一个长度为 2 ,下标从 0 开始 的整型数组,其中 values[0] 是与 videoId 相关联的视频上的点赞数,values[1] 是不喜欢数。如果没有与 videoId 相关联的视频,则返回 [-1]。
82+
- int getViews(int videoId) 返回与 videoId 相关联的视频的观看次数,如果没有与 videoId 相关联的视频,返回 -1。
83+
提示:
84+
1 <= video.length <= 10^5
85+
调用 upload 时所有 video.length 的总和不会超过 10^5
86+
video 由数字组成
87+
0 <= videoId <= 10^5
88+
0 <= startMinute < endMinute < 10^5
89+
startMinute < video.length
90+
调用 watch 时所有 endMinute - startMinute 的总和不会超过 10^5。
91+
所有函数 总共 最多调用 10^5 次。
92+
93+
哈希表模拟 + 优先队列
94+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /