-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweakest_chars.go
31 lines (28 loc) · 1.13 KB
/
weakest_chars.go
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
package problem1996
import "sort"
/*
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.
*/
func numberOfWeakCharacters(properties [][]int) int {
var result, max int
// Sort by attack and secondary sort by defense
sort.Slice(properties, func(i, j int) bool {
if properties[i][0] == properties[j][0] {
return properties[i][1] < properties[j][1]
}
return properties[i][0] > properties[j][0]
})
// Now that they're sorted we just need to look at the defense
for i := range properties {
if max > properties[i][1] {
result++
} else {
max = properties[i][1]
}
}
return result
}