Skip to content

Commit acb701a

Browse files
committed
一刷649
1 parent 2105f56 commit acb701a

File tree

5 files changed

+125
-56
lines changed

5 files changed

+125
-56
lines changed

README.adoc

+8-8
Original file line numberDiff line numberDiff line change
@@ -4568,14 +4568,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
45684568
//|{doc_base_url}/0648-replace-words.adoc[题解]
45694569
//|Medium
45704570
//|
4571-
//
4572-
//|{counter:codes}
4573-
//|{leetcode_base_url}/dota2-senate/[649. Dota2 Senate^]
4574-
//|{source_base_url}/_0649_Dota2Senate.java[Java]
4575-
//|{doc_base_url}/0649-dota2-senate.adoc[题解]
4576-
//|Medium
4577-
//|
4578-
//
4571+
4572+
|{counter:codes}
4573+
|{leetcode_base_url}/dota2-senate/[649. Dota2 Senate^]
4574+
|{source_base_url}/_0649_Dota2Senate.java[Java]
4575+
|{doc_base_url}/0649-dota2-senate.adoc[题解]
4576+
|Medium
4577+
|
4578+
45794579
//|{counter:codes}
45804580
//|{leetcode_base_url}/2-keys-keyboard/[650. 2 Keys Keyboard^]
45814581
//|{source_base_url}/_0650_2KeysKeyboard.java[Java]

docs/0649-dota2-senate.adoc

+56-46
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,82 @@
11
[#0649-dota2-senate]
2-
= 649. Dota2 Senate
2+
= 649. Dota2 参议院
33

4-
{leetcode}/problems/dota2-senate/[LeetCode - Dota2 Senate^]
4+
https://leetcode.cn/problems/dota2-senate/[LeetCode - 649. Dota2 参议院 ^]
55

6-
In the world of Dota2, there are two parties: the `Radiant` and the `Dire`.
6+
Dota2 的世界里有两个阵营:`Radiant`(天辉)和 `Dire`(夜魇)
77

8-
The Dota2 senate consists of senators coming from two parties. Now the senate wants to make a decision about a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise `one` of the two rights:
8+
Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的 *一* 项:
99

10+
* *禁止一名参议员的权利*:参议员可以让另一位参议员在这一轮和随后的几轮中丧失 *所有的权利*
11+
* *宣布胜利*:如果参议员发现有权利投票的参议员都是 *同一个阵营的*,他可以宣布胜利并决定在游戏中的有关变化。
1012
11-
. `Ban one senator's right`:
13+
给你一个字符串 `senate` 代表每个参议员的阵营。字母 `+'R'+`
14+
`+'D'+`分别代表了 `Radiant`(天辉)和 `Dire`(夜魇)。然后,如果有
15+
`n` 个参议员,给定字符串的大小将是 `n`
1216

17+
以轮为基础的过程从给定顺序的第一个参议员开始到最后一个参议员结束。这一过程将持续到投票结束。所有失去权利的参议员将在过程中被跳过。
1318

14-
A senator can make another senator lose *all his rights* in this and all the following rounds.
15-
. `Announce the victory`:
19+
假设每一位参议员都足够聪明,会为自己的政党做出最好的策略,你需要预测哪一方最终会宣布胜利并在
20+
Dota2 游戏中决定改变。输出应该是 `+"Radiant"+``+"Dire"+`
1621

1722

18-
If this senator found the senators who still have rights to vote are all from *the same party*, he can announce the victory and make the decision about the change in the game.
23+
*示例 1:*
1924

25+
....
26+
输入:senate = "RD"
27+
输出:"Radiant"
28+
解释:
29+
第 1 轮时,第一个参议员来自 Radiant 阵营,他可以使用第一项权利让第二个参议员失去所有权利。
30+
这一轮中,第二个参议员将会被跳过,因为他的权利被禁止了。
31+
第 2 轮时,第一个参议员可以宣布胜利,因为他是唯一一个有投票权的人。
32+
....
2033

21-
34+
*示例 2:*
2235

23-
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the `Radiant` party and the `Dire` party respectively. Then if there are `n` senators, the size of the given string will be `n`.
36+
....
37+
输入:senate = "RDD"
38+
输出:"Dire"
39+
解释:
40+
第 1 轮时,第一个来自 Radiant 阵营的参议员可以使用第一项权利禁止第二个参议员的权利。
41+
这一轮中,第二个来自 Dire 阵营的参议员会将被跳过,因为他的权利被禁止了。
42+
这一轮中,第三个来自 Dire 阵营的参议员可以使用他的第一项权利禁止第一个参议员的权利。
43+
因此在第二轮只剩下第三个参议员拥有投票的权利,于是他可以宣布胜利
44+
....
2445

25-
The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.
46+
*提示:*
2647

27-
Suppose every senator is smart enough and will play the best strategy for his own party, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be `Radiant` or `Dire`.
48+
* `+n == senate.length+`
49+
* `1 \<= n \<= 10^4^`
50+
* `+senate[i]+``+'R'+``+'D'+`
2851
29-
*Example 1:*
3052
31-
[subs="verbatim,quotes,macros"]
32-
----
33-
*Input:* "RD"
34-
*Output:* "Radiant"
35-
*Explanation:* The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
36-
And the second senator can't exercise any rights any more since his right has been banned.
37-
And in the round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
38-
----
53+
== 思路分析
3954

40-
55+
贪心算法。优先禁止最近的对方议员投票。不停循环,直到只留下一方议员。
4156

42-
*Example 2:*
43-
44-
[subs="verbatim,quotes,macros"]
57+
[[src-0649]]
58+
[tabs]
59+
====
60+
一刷::
61+
+
62+
--
63+
[{java_src_attr}]
4564
----
46-
*Input:* "RDD"
47-
*Output:* "Dire"
48-
*Explanation:*
49-
The first senator comes from Radiant and he can just ban the next senator's right in the round 1.
50-
And the second senator can't exercise any rights anymore since his right has been banned.
51-
And the third senator comes from Dire and he can ban the first senator's right in the round 1.
52-
And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
65+
include::{sourcedir}/_0649_Dota2Senate.java[tag=answer]
5366
----
67+
--
5468
55-
56-
57-
*Note:*
58-
69+
// 二刷::
70+
// +
71+
// --
72+
// [{java_src_attr}]
73+
// ----
74+
// include::{sourcedir}/_0649_Dota2Senate_2.java[tag=answer]
75+
// ----
76+
// --
77+
====
5978

60-
. The length of the given string will in the range [1, 10,000].
6179

80+
== 参考资料
6281

63-
64-
65-
66-
67-
[[src-0649]]
68-
[{java_src_attr}]
69-
----
70-
include::{sourcedir}/_0649_Dota2Senate.java[tag=answer]
71-
----
7282

docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ include::0647-palindromic-substrings.adoc[leveloffset=+1]
13801380

13811381
// include::0648-replace-words.adoc[leveloffset=+1]
13821382

1383-
// include::0649-dota2-senate.adoc[leveloffset=+1]
1383+
include::0649-dota2-senate.adoc[leveloffset=+1]
13841384

13851385
// include::0650-2-keys-keyboard.adoc[leveloffset=+1]
13861386

logbook/202503.adoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,15 @@ endif::[]
529529
|✅ 双指针
530530

531531
|{counter:codes2503}
532-
|{leetcode_base_url}/single-number/[136. Single Number^]
532+
|{leetcode_base_url}/single-number/[136. 只出现一次的数字^]
533533
|{doc_base_url}/0136-single-number.adoc[题解]
534534
|✅ 位运算。出现两次,则异或后为 `0`,所有数字异或,最后只剩下出现一次的数字。
535535

536+
|{counter:codes2503}
537+
|{leetcode_base_url}/dota2-senate/[649. Dota2 参议院^]
538+
|{doc_base_url}/0649-dota2-senate.adoc[题解]
539+
|✅ 贪心算法。优先禁止最近的对方议员投票。不停循环,直到只留下一方议员。
540+
536541
|===
537542

538543
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _0649_Dota2Senate {
7+
// tag::answer[]
8+
9+
/**
10+
* 通过了所有的测试用例。和官方答案相比,感觉又不对。
11+
*
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-04-28 09:02:03
14+
*/
15+
public String predictPartyVictory(String senate) {
16+
Map<Character, Integer> counter = new HashMap<>();
17+
counter.put('R', 0);
18+
counter.put('D', 0);
19+
char[] chars = senate.toCharArray();
20+
for (char c : chars) {
21+
counter.put(c, counter.get(c) + 1);
22+
}
23+
int length = senate.length();
24+
for (int i = 0; counter.get('R') > 0 && counter.get('D') > 0; i++) {
25+
int baseIndex = i % length;
26+
if (chars[baseIndex] == '.') {
27+
continue;
28+
}
29+
for (int j = baseIndex + 1; j < 2 * length; j++) {
30+
int rivalIndex = j % length;
31+
if (chars[rivalIndex] == '.' || chars[baseIndex] == chars[rivalIndex]) {
32+
continue;
33+
}
34+
char rivalChar = chars[rivalIndex];
35+
chars[rivalIndex] = '.';
36+
counter.put(rivalChar, counter.get(rivalChar) - 1);
37+
break;
38+
}
39+
if (counter.get('R') <= 0 || counter.get('D') <= 0) {
40+
break;
41+
}
42+
}
43+
if (counter.get('R') <= 0) {
44+
return "Dire";
45+
} else {
46+
return "Radiant";
47+
}
48+
}
49+
50+
// end::answer[]
51+
public static void main(String[] args) {
52+
new _0649_Dota2Senate().predictPartyVictory("DRRDRDRDRDDRDRDRD");
53+
}
54+
}

0 commit comments

Comments
 (0)