Skip to content

Commit 76747eb

Browse files
committed
feat: add solutions to lc problem: No.1996
No.1996.The Number of Weak Characters in the Game
1 parent 2338ce7 commit 76747eb

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,96 @@
5252

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

55+
按攻击力从大到小排序,攻击力相同则按防御力从小到大排序。
56+
57+
遍历,护遍历过的角色的防御的最大值 mx。
58+
59+
对于当前角色 p,如果 p 的防御小于 mx,说明前面有防御比 p 高的角色,记作 q。根据上面的排序规则,q 的攻击是大于或等于 p 的攻击的,如果 q 和 p 攻击相同,仍然根据上面的排序规则,q 的防御不会超过 p,矛盾,因此 q 的攻击必然大于 p,于是 q 的攻防均高于 p,p 是一个弱角色。
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
5864

5965
<!-- 这里可写当前语言的特殊实现逻辑 -->
6066

6167
```python
62-
68+
class Solution:
69+
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
70+
properties.sort(key=lambda x: (-x[0], x[1]))
71+
ans = mx = 0
72+
for _, d in properties:
73+
if mx > d:
74+
ans += 1
75+
mx = max(mx, d)
76+
return ans
6377
```
6478

6579
### **Java**
6680

6781
<!-- 这里可写当前语言的特殊实现逻辑 -->
6882

6983
```java
84+
class Solution {
85+
86+
public int numberOfWeakCharacters(int[][] properties) {
87+
Arrays.sort(
88+
properties,
89+
(a, b) -> {
90+
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
91+
}
92+
);
93+
int ans = 0, mx = 0;
94+
for (int[] p : properties) {
95+
if (mx > p[1]) {
96+
++ans;
97+
}
98+
mx = Math.max(mx, p[1]);
99+
}
100+
return ans;
101+
}
102+
}
103+
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int numberOfWeakCharacters(vector<vector<int>> &properties) {
112+
sort(properties.begin(), properties.end(), [&](vector<int> &a, vector<int> &b)
113+
{ return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
114+
int ans = 0, mx = 0;
115+
for (auto &p : properties)
116+
{
117+
if (mx > p[1]) ++ans;
118+
else mx = p[1];
119+
}
120+
return ans;
121+
}
122+
};
123+
```
70124
125+
### **Go**
126+
127+
```go
128+
func numberOfWeakCharacters(properties [][]int) int {
129+
sort.Slice(properties, func(i, j int) bool {
130+
if properties[i][0] == properties[j][0] {
131+
return properties[i][1] < properties[j][1]
132+
}
133+
return properties[i][0] > properties[j][0]
134+
})
135+
ans, mx := 0, 0
136+
for _, p := range properties {
137+
if mx > p[1] {
138+
ans++
139+
} else {
140+
mx = p[1]
141+
}
142+
}
143+
return ans
144+
}
71145
```
72146

73147
### **...**

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,76 @@
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
56+
properties.sort(key=lambda x: (-x[0], x[1]))
57+
ans = mx = 0
58+
for _, d in properties:
59+
if mx > d:
60+
ans += 1
61+
mx = max(mx, d)
62+
return ans
5563
```
5664

5765
### **Java**
5866

5967
```java
68+
class Solution {
69+
public int numberOfWeakCharacters(int[][] properties) {
70+
Arrays.sort(properties, (a, b) -> {
71+
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
72+
});
73+
int ans = 0, mx = 0;
74+
for (int[] p : properties) {
75+
if (mx > p[1]) {
76+
++ans;
77+
}
78+
mx = Math.max(mx, p[1]);
79+
}
80+
return ans;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
int numberOfWeakCharacters(vector<vector<int>> &properties) {
91+
sort(properties.begin(), properties.end(), [&](vector<int> &a, vector<int> &b)
92+
{ return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
93+
int ans = 0, mx = 0;
94+
for (auto &p : properties)
95+
{
96+
if (mx > p[1]) ++ans;
97+
else mx = p[1];
98+
}
99+
return ans;
100+
}
101+
};
102+
```
60103
104+
### **Go**
105+
106+
```go
107+
func numberOfWeakCharacters(properties [][]int) int {
108+
sort.Slice(properties, func(i, j int) bool {
109+
if properties[i][0] == properties[j][0] {
110+
return properties[i][1] < properties[j][1]
111+
}
112+
return properties[i][0] > properties[j][0]
113+
})
114+
ans, mx := 0, 0
115+
for _, p := range properties {
116+
if mx > p[1] {
117+
ans++
118+
} else {
119+
mx = p[1]
120+
}
121+
}
122+
return ans
123+
}
61124
```
62125

63126
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int numberOfWeakCharacters(vector<vector<int>> &properties) {
4+
sort(properties.begin(), properties.end(), [&](vector<int> &a, vector<int> &b)
5+
{ return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; });
6+
int ans = 0, mx = 0;
7+
for (auto &p : properties)
8+
{
9+
if (mx > p[1]) ++ans;
10+
else mx = p[1];
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func numberOfWeakCharacters(properties [][]int) int {
2+
sort.Slice(properties, func(i, j int) bool {
3+
if properties[i][0] == properties[j][0] {
4+
return properties[i][1] < properties[j][1]
5+
}
6+
return properties[i][0] > properties[j][0]
7+
})
8+
ans, mx := 0, 0
9+
for _, p := range properties {
10+
if mx > p[1] {
11+
ans++
12+
} else {
13+
mx = p[1]
14+
}
15+
}
16+
return ans
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int numberOfWeakCharacters(int[][] properties) {
3+
Arrays.sort(properties, (a, b) -> {
4+
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
5+
});
6+
int ans = 0, mx = 0;
7+
for (int[] p : properties) {
8+
if (mx > p[1]) {
9+
++ans;
10+
}
11+
mx = Math.max(mx, p[1]);
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
3+
properties.sort(key=lambda x: (-x[0], x[1]))
4+
ans = mx = 0
5+
for _, d in properties:
6+
if mx > d:
7+
ans += 1
8+
mx = max(mx, d)
9+
return ans

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy