일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Generator
- 프로그래머스
- data science
- Protocol
- Convert Sorted List to Binary Search Tree
- 파이썬
- iterator
- 밴픽
- Substring with Concatenation of All Words
- 운영체제
- Python Code
- Decorator
- attribute
- kaggle
- Python Implementation
- shiba
- concurrency
- Class
- LeetCode
- 315. Count of Smaller Numbers After Self
- 715. Range Module
- 43. Multiply Strings
- Python
- 시바견
- t1
- 30. Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- Regular Expression
- DWG
- 컴퓨터의 구조
Archives
- Today
- Total
Scribbling
LeetCode: 1996. The Number of Weak Characters in the Game 본문
Computer Science/Algorithms & Data Structures
LeetCode: 1996. The Number of Weak Characters in the Game
focalpoint 2022. 10. 21. 23:52I found this problem is pretty similar to 'Russian Doll' prob (https://leetcode.com/problems/russian-doll-envelopes/).
Thought process for this problem is:
0> Sort the properties.
1> Let's just assume that attack strictly increases (e.g. 3, 4, 5, ...):
then what we should do is to count how many defense values have larger values on their right side.
And that's exactly what monotonicly desc stack does.
2> Now handle the cases with the same attacks (e.g. (3, 3), (3, 4)):
our logic would return 1 in [(3, 3), (3, 4)]
we can easily tweak the logic by changing the order of the array to [(3, 4), (3, 3)].
with the order, our logic would return 0.
class Solution:
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
properties.sort(key=lambda x: (x[0], -x[1]))
ret = 0
# monotonicly desc stack
stack = []
for a, d in properties:
while stack and stack[-1] < d:
stack.pop()
ret += 1
stack.append(d)
return ret
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
Rabin-Karp Algorithm (2) | 2022.11.09 |
---|---|
Python Sorted Containers (0) | 2022.10.26 |
Range Checking Algorithm (check if there's a point in a given range) (0) | 2022.08.16 |
Monotonic Stack (0) | 2022.08.08 |
LeetCode: 839. Similar String Groups (0) | 2022.05.25 |