2023年8月2日每日一题--822. 翻转卡片游戏

leetcode链接:822. 翻转卡片游戏

题目分析

方案一

看来题解之后,有种被骗了的感觉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int flipgame(int[] fronts, int[] backs) {
Set<Integer> nums = new HashSet<>();

PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o1.compareTo(o2));

for (int i = 0; i < fronts.length; i++) {
if (fronts[i] == backs[i]) {
nums.add(fronts[i]);
continue;
}
queue.add(fronts[i]);
queue.add(backs[i]);
}

while (!queue.isEmpty() && nums.contains(queue.peek())) {
queue.poll();
}
if (queue.isEmpty()) return 0;
return queue.peek();
}
}

结果

解答成功:
执行耗时:6 ms,击败了22.78% 的Java用户
内存消耗:42.4 MB,击败了70.89% 的Java用户

分析

时间复杂度:
O( n )

空间复杂度:
O( n )

官方题解

https://leetcode.cn/problems/card-flipping-game/solution/fan-zhuan-qia-pian-you-xi-by-leetcode-so-acbj/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public int flipgame(int[] fronts, int[] backs) {
Set<Integer> same = new HashSet();
for (int i = 0; i < fronts.length; ++i) {
if (fronts[i] == backs[i]) {
same.add(fronts[i]);
}
}
int res = 3000;
for (int x : fronts) {
if (x < res && !same.contains(x)){
res = x;
}
}
for (int x : backs) {
if (x < res && !same.contains(x)) {
res = x;
}
}
return res % 3000;
}
}

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/card-flipping-game/solution/fan-zhuan-qia-pian-you-xi-by-leetcode-so-acbj/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


2023年8月2日每日一题--822. 翻转卡片游戏
http://yuanql.top/2023/08/02/02_02_leetcode_每日一题/2023年8月2日每日一题--822. 翻转卡片游戏/
作者
Qingli Yuan
发布于
2023年8月2日
许可协议