-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1996-TheNumberOfWeakCharactersInTheGame.go
More file actions
67 lines (57 loc) · 2.69 KB
/
1996-TheNumberOfWeakCharactersInTheGame.go
File metadata and controls
67 lines (57 loc) · 2.69 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
// 1996. The Number of Weak Characters in the Game
// You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense.
// You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.
// A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels.
// More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei.
// Return the number of weak characters.
// Example 1:
// Input: properties = [[5,5],[6,3],[3,6]]
// Output: 0
// Explanation: No character has strictly greater attack and defense than the other.
// Example 2:
// Input: properties = [[2,2],[3,3]]
// Output: 1
// Explanation: The first character is weak because the second character has a strictly greater attack and defense.
// Example 3:
// Input: properties = [[1,5],[10,4],[4,3]]
// Output: 1
// Explanation: The third character is weak because the second character has a strictly greater attack and defense.
// Constraints:
// 2 <= properties.length <= 10^5
// properties[i].length == 2
// 1 <= attacki, defensei <= 10^5
import "fmt"
import "sort"
func numberOfWeakCharacters(properties [][]int) int {
sort.Slice(properties, func(i, j int) bool {
if properties[i][0] != properties[j][0] { return properties[i][0] > properties[j][0] } // 攻击越强越靠前
return properties[i][1] < properties[j][1] // 防御越弱越靠前
})
res, mx := 0, -1
for _, v := range properties {
if v[1] < mx { // 如果认为角色 i 弱于 存在的另一个角色 j ,那么 attackj > attacki 且 defensej > defensei
res++
} else {
mx = v[1]
}
}
return res
}
func main() {
// Example 1:
// Input: properties = [[5,5],[6,3],[3,6]]
// Output: 0
// Explanation: No character has strictly greater attack and defense than the other.
fmt.Println(numberOfWeakCharacters([][]int{{5,5},{6,3},{3,6}})) // 0
// Example 2:
// Input: properties = [[2,2],[3,3]]
// Output: 1
// Explanation: The first character is weak because the second character has a strictly greater attack and defense.
fmt.Println(numberOfWeakCharacters([][]int{{2,2},{3,3}})) // 1
// Example 3:
// Input: properties = [[1,5],[10,4],[4,3]]
// Output: 1
// Explanation: The third character is weak because the second character has a strictly greater attack and defense.
fmt.Println(numberOfWeakCharacters([][]int{{1,5},{10,4},{4,3}})) // 1
}