Skip to content

Commit 61c42d7

Browse files
committed
feat: add solutions to lc problem: No.1996
No.1996.The Number of Weak Characters in the Game
1 parent 917feae commit 61c42d7

File tree

10 files changed

+172
-120
lines changed

10 files changed

+172
-120
lines changed

solution/1900-1999/1994.The Number of Good Subsets/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010

1111
<ul>
1212
<li>比方说,如果&nbsp;<code>nums = [1, 2, 3, 4]</code>&nbsp;:
13-
1413
<ul>
1514
<li><code>[2, 3]</code>&nbsp;,<code>[1, 2, 3]</code>&nbsp;和&nbsp;<code>[1, 3]</code>&nbsp;是 <strong>好</strong>&nbsp;子集,乘积分别为&nbsp;<code>6 = 2*3</code>&nbsp;,<code>6 = 2*3</code>&nbsp;和&nbsp;<code>3 = 3</code>&nbsp;。</li>
1615
<li><code>[1, 4]</code> 和&nbsp;<code>[4]</code>&nbsp;不是 <strong>好</strong>&nbsp;子集,因为乘积分别为&nbsp;<code>4 = 2*2</code> 和&nbsp;<code>4 = 2*2</code>&nbsp;。</li>
1716
</ul>
1817
</li>
19-
2018
</ul>
2119

2220
<p>请你返回 <code>nums</code>&nbsp;中不同的&nbsp;<strong>好</strong>&nbsp;子集的数目对<em>&nbsp;</em><code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;的结果。</p>

solution/1900-1999/1994.The Number of Good Subsets/README_EN.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
<ul>
1010
<li>For example, if <code>nums = [1, 2, 3, 4]</code>:
11-
1211
<ul>
1312
<li><code>[2, 3]</code>, <code>[1, 2, 3]</code>, and <code>[1, 3]</code> are <strong>good</strong> subsets with products <code>6 = 2*3</code>, <code>6 = 2*3</code>, and <code>3 = 3</code> respectively.</li>
1413
<li><code>[1, 4]</code> and <code>[4]</code> are not <strong>good</strong> subsets with products <code>4 = 2*2</code> and <code>4 = 2*2</code> respectively.</li>
1514
</ul>
1615
</li>
17-
1816
</ul>
1917

2018
<p>Return <em>the number of different <strong>good</strong> subsets in </em><code>nums</code><em> <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>.</p>

solution/1900-1999/1996.The Number of Weak Characters in the Game/README.md

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
按攻击力从大到小排序,攻击力相同则按防御力从小到大排序。
55+
**方法一:排序 + 遍历**
5656

57-
遍历,维护遍历过的角色的防御的最大值 mx
57+
我们可以将所有角色按照攻击力降序排序,防御力升序排序
5858

59-
对于当前角色 p,如果 p 的防御小于 mx,说明前面有防御比 p 高的角色,记作 q。根据上面的排序规则,q 的攻击是大于或等于 p 的攻击的,如果 q 和 p 攻击相同,仍然根据上面的排序规则,q 的防御不会超过 p,矛盾,因此 q 的攻击必然大于 p,于是 q 的攻防均高于 p,p 是一个弱角色。
59+
然后遍历所有角色,对于当前角色,如果其防御力小于之前的最大防御力,则说明其为弱角色,答案加一,否则更新最大防御力。
60+
61+
遍历结束后,即可得到答案。
62+
63+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为角色数量。
6064

6165
<!-- tabs:start -->
6266

@@ -69,10 +73,9 @@ class Solution:
6973
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
7074
properties.sort(key=lambda x: (-x[0], x[1]))
7175
ans = mx = 0
72-
for _, d in properties:
73-
if mx > d:
74-
ans += 1
75-
mx = max(mx, d)
76+
for _, x in properties:
77+
ans += x < mx
78+
mx = max(mx, x)
7679
return ans
7780
```
7881

@@ -83,51 +86,30 @@ class Solution:
8386
```java
8487
class Solution {
8588
public int numberOfWeakCharacters(int[][] properties) {
86-
Arrays.sort(properties, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]; });
89+
Arrays.sort(properties, (a, b) -> b[0] - a[0] == 0 ? a[1] - b[1] : b[0] - a[0]);
8790
int ans = 0, mx = 0;
88-
for (int[] p : properties) {
89-
if (mx > p[1]) {
91+
for (var x : properties) {
92+
if (x[1] < mx) {
9093
++ans;
9194
}
92-
mx = Math.max(mx, p[1]);
95+
mx = Math.max(mx, x[1]);
9396
}
9497
return ans;
9598
}
9699
}
97100
```
98101

99-
### **TypeScript**
100-
101-
```ts
102-
function numberOfWeakCharacters(properties: number[][]): number {
103-
properties.sort((a, b) => (a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]));
104-
105-
let ans = 0;
106-
let max = 0;
107-
for (let [, b] of properties) {
108-
if (b < max) {
109-
ans++;
110-
} else {
111-
max = b;
112-
}
113-
}
114-
return ans;
115-
}
116-
```
117-
118102
### **C++**
119103

120104
```cpp
121105
class Solution {
122106
public:
123107
int numberOfWeakCharacters(vector<vector<int>>& properties) {
124-
sort(properties.begin(), properties.end(), [&](vector<int>& a, vector<int>& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
108+
sort(properties.begin(), properties.end(), [&](auto& a, auto& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
125109
int ans = 0, mx = 0;
126-
for (auto& p : properties) {
127-
if (mx > p[1])
128-
++ans;
129-
else
130-
mx = p[1];
110+
for (auto& x : properties) {
111+
ans += x[1] < mx;
112+
mx = max(mx, x[1]);
131113
}
132114
return ans;
133115
}
@@ -137,25 +119,66 @@ public:
137119
### **Go**
138120
139121
```go
140-
func numberOfWeakCharacters(properties [][]int) int {
122+
func numberOfWeakCharacters(properties [][]int) (ans int) {
141123
sort.Slice(properties, func(i, j int) bool {
142-
if properties[i][0] == properties[j][0] {
143-
return properties[i][1] < properties[j][1]
124+
a, b := properties[i], properties[j]
125+
if a[0] == b[0] {
126+
return a[1] < b[1]
144127
}
145-
return properties[i][0] > properties[j][0]
128+
return a[0] > b[0]
146129
})
147-
ans, mx := 0, 0
148-
for _, p := range properties {
149-
if mx > p[1] {
130+
mx := 0
131+
for _, x := range properties {
132+
if x[1] < mx {
150133
ans++
151134
} else {
152-
mx = p[1]
135+
mx = x[1]
153136
}
154137
}
155-
return ans
138+
return
139+
}
140+
```
141+
142+
### **TypeScript**
143+
144+
```ts
145+
function numberOfWeakCharacters(properties: number[][]): number {
146+
properties.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
147+
let ans = 0;
148+
let mx = 0;
149+
for (const [, x] of properties) {
150+
if (x < mx) {
151+
ans++;
152+
} else {
153+
mx = x;
154+
}
155+
}
156+
return ans;
156157
}
157158
```
158159

160+
### **JavaScript**
161+
162+
```js
163+
/**
164+
* @param {number[][]} properties
165+
* @return {number}
166+
*/
167+
var numberOfWeakCharacters = function (properties) {
168+
properties.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
169+
let ans = 0;
170+
let mx = 0;
171+
for (const [, x] of properties) {
172+
if (x < mx) {
173+
ans++;
174+
} else {
175+
mx = x;
176+
}
177+
}
178+
return ans;
179+
};
180+
```
181+
159182
### **...**
160183

161184
```

solution/1900-1999/1996.The Number of Weak Characters in the Game/README_EN.md

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ class Solution:
5555
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
5656
properties.sort(key=lambda x: (-x[0], x[1]))
5757
ans = mx = 0
58-
for _, d in properties:
59-
if mx > d:
60-
ans += 1
61-
mx = max(mx, d)
58+
for _, x in properties:
59+
ans += x < mx
60+
mx = max(mx, x)
6261
return ans
6362
```
6463

@@ -67,51 +66,30 @@ class Solution:
6766
```java
6867
class Solution {
6968
public int numberOfWeakCharacters(int[][] properties) {
70-
Arrays.sort(properties, (a, b) -> { return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]; });
69+
Arrays.sort(properties, (a, b) -> b[0] - a[0] == 0 ? a[1] - b[1] : b[0] - a[0]);
7170
int ans = 0, mx = 0;
72-
for (int[] p : properties) {
73-
if (mx > p[1]) {
71+
for (var x : properties) {
72+
if (x[1] < mx) {
7473
++ans;
7574
}
76-
mx = Math.max(mx, p[1]);
75+
mx = Math.max(mx, x[1]);
7776
}
7877
return ans;
7978
}
8079
}
8180
```
8281

83-
### **TypeScript**
84-
85-
```ts
86-
function numberOfWeakCharacters(properties: number[][]): number {
87-
properties.sort((a, b) => (a[0] != b[0] ? b[0] - a[0] : a[1] - b[1]));
88-
89-
let ans = 0;
90-
let max = 0;
91-
for (let [, b] of properties) {
92-
if (b < max) {
93-
ans++;
94-
} else {
95-
max = b;
96-
}
97-
}
98-
return ans;
99-
}
100-
```
101-
10282
### **C++**
10383

10484
```cpp
10585
class Solution {
10686
public:
10787
int numberOfWeakCharacters(vector<vector<int>>& properties) {
108-
sort(properties.begin(), properties.end(), [&](vector<int>& a, vector<int>& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
88+
sort(properties.begin(), properties.end(), [&](auto& a, auto& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
10989
int ans = 0, mx = 0;
110-
for (auto& p : properties) {
111-
if (mx > p[1])
112-
++ans;
113-
else
114-
mx = p[1];
90+
for (auto& x : properties) {
91+
ans += x[1] < mx;
92+
mx = max(mx, x[1]);
11593
}
11694
return ans;
11795
}
@@ -121,25 +99,66 @@ public:
12199
### **Go**
122100
123101
```go
124-
func numberOfWeakCharacters(properties [][]int) int {
102+
func numberOfWeakCharacters(properties [][]int) (ans int) {
125103
sort.Slice(properties, func(i, j int) bool {
126-
if properties[i][0] == properties[j][0] {
127-
return properties[i][1] < properties[j][1]
104+
a, b := properties[i], properties[j]
105+
if a[0] == b[0] {
106+
return a[1] < b[1]
128107
}
129-
return properties[i][0] > properties[j][0]
108+
return a[0] > b[0]
130109
})
131-
ans, mx := 0, 0
132-
for _, p := range properties {
133-
if mx > p[1] {
110+
mx := 0
111+
for _, x := range properties {
112+
if x[1] < mx {
134113
ans++
135114
} else {
136-
mx = p[1]
115+
mx = x[1]
137116
}
138117
}
139-
return ans
118+
return
119+
}
120+
```
121+
122+
### **TypeScript**
123+
124+
```ts
125+
function numberOfWeakCharacters(properties: number[][]): number {
126+
properties.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
127+
let ans = 0;
128+
let mx = 0;
129+
for (const [, x] of properties) {
130+
if (x < mx) {
131+
ans++;
132+
} else {
133+
mx = x;
134+
}
135+
}
136+
return ans;
140137
}
141138
```
142139

140+
### **JavaScript**
141+
142+
```js
143+
/**
144+
* @param {number[][]} properties
145+
* @return {number}
146+
*/
147+
var numberOfWeakCharacters = function (properties) {
148+
properties.sort((a, b) => (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
149+
let ans = 0;
150+
let mx = 0;
151+
for (const [, x] of properties) {
152+
if (x < mx) {
153+
ans++;
154+
} else {
155+
mx = x;
156+
}
157+
}
158+
return ans;
159+
};
160+
```
161+
143162
### **...**
144163

145164
```

solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class Solution {
22
public:
33
int numberOfWeakCharacters(vector<vector<int>>& properties) {
4-
sort(properties.begin(), properties.end(), [&](vector<int>& a, vector<int>& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
4+
sort(properties.begin(), properties.end(), [&](auto& a, auto& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
55
int ans = 0, mx = 0;
6-
for (auto& p : properties) {
7-
if (mx > p[1])
8-
++ans;
9-
else
10-
mx = p[1];
6+
for (auto& x : properties) {
7+
ans += x[1] < mx;
8+
mx = max(mx, x[1]);
119
}
1210
return ans;
1311
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
func numberOfWeakCharacters(properties [][]int) int {
1+
func numberOfWeakCharacters(properties [][]int) (ans int) {
22
sort.Slice(properties, func(i, j int) bool {
3-
if properties[i][0] == properties[j][0] {
4-
return properties[i][1] < properties[j][1]
3+
a, b := properties[i], properties[j]
4+
if a[0] == b[0] {
5+
return a[1] < b[1]
56
}
6-
return properties[i][0] > properties[j][0]
7+
return a[0] > b[0]
78
})
8-
ans, mx := 0, 0
9-
for _, p := range properties {
10-
if mx > p[1] {
9+
mx := 0
10+
for _, x := range properties {
11+
if x[1] < mx {
1112
ans++
1213
} else {
13-
mx = p[1]
14+
mx = x[1]
1415
}
1516
}
16-
return ans
17+
return
1718
}

0 commit comments

Comments
 (0)