-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1996.py
61 lines (50 loc) · 1.7 KB
/
1996.py
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
# [ LeetCode ] 1996. The Number of Weak Characters in the Game
def solution(properties: list[list[int]]) -> int:
properties.sort(key=lambda x: (-x[0], x[1]))
maximum_defense: int = properties[0][1]
answer: int = 0
for _, defense in properties:
if defense < maximum_defense:
answer += 1
else:
maximum_defense = defense
return answer
def another_solution(properties: list[list[int]]) -> int:
maximum_attack: int = 0
for attack, _ in properties:
if attack > maximum_attack:
maximum_attack = attack
bucket: list[int] = [ 0 for _ in range(maximum_attack+2) ]
for attack, defense in properties:
if bucket[attack] < defense:
bucket[attack] = defense
for idx in range(maximum_attack+1, 0, -1):
if bucket[idx] > bucket[idx-1]:
bucket[idx-1] = bucket[idx]
answer: int = 0
for attack, defense in properties:
if defense < bucket[attack+1]:
answer += 1
return answer
if __name__ == "__main__":
cases: list[dict[str, list[list[int]] | int]] = [
{
"input": { "properties": [[5, 5],[6, 3],[3, 6]] },
"output": 0
},
{
"input": { "properties": [[2, 2],[3, 3]] },
"output": 1
},
{
"input": { "properties": [[1, 5],[10, 4],[4, 3]] },
"output": 1
},
{
"input": { "properties": [[1, 1],[2, 1],[2, 2],[1, 2]] },
"output": 1
}
]
for case in cases:
assert case["output"] == solution(**case["input"])
assert case["output"] == another_solution(**case["input"])