Python Algorithm
LeetCode
3019. Number of Changing Keys
- You are given a 0-indexed string s typed by a user. Changing a key is defined as using a key different from the last used key. For example, s = "ab" has a change of a key while s = "bBBb" does not have any.
- Return the number of times the user had to change the key.
- Note: Modifiers like shift or caps lock won't be counted in changing the key that is if a user typed the letter 'a' and then the letter 'A' then it will not be considered as a changing of key.
- Constraints
- 1 ≤ s.length ≤ 100
- s consists of only upper case and lower case English letters.
class Solution:
def countKeyChanges(self, s: str) -> int:
answer = 0
# 문자열 lower, for문 사용해서 앞에 문자와 뒤에 문자 비교
s = s.lower()
for i in range(len(s) - 1):
if s[i] != s[i+1]:
answer += 1
return answer
class Solution:
def countKeyChanges(self, s: str) -> int:
answer = 0
s = s.lower()
for i in range(len(s)-1):
if s[i] != s[i+1]:
answer += 1
return answer
3238. Find the Number of Winning Players
- You are given an integer n representing the number of players in a game and a 2D array [pick] where pick[i] = [xi, yi] represents that the player xᵢ picked a ball of color yᵢ.
- Player i wins the game if they pick strictly more than i balls of the same color. In other words,
- Player 0 wins if they pick any ball.
- Player 1 wins if they pick at least two balls of the same color.
- ...
- Player i wins if they pick at leasti + 1 balls of the same color.
- Return the number of players who win the game.
- Note that multiple players can win the game.
- Constraints
- 2 ≤ n ≤ 10
- 1 ≤ pick.length ≤ 100
- pick[i].length == 2
- 0 ≤ xᵢ ≤ n - 1
- 0 ≤ yᵢ ≤ 10
class Solution:
def winningPlayerCount(self, n: int, pick: List[List[int]]) -> int:
answer = 0
# key: 플레이어 번호, value: 색깔별 공 개수
d = defaultdict(Counter)
for player, ball_color in pick:
d[player][ball_color] += 1
for player, ball_counter in d.items():
if player + 1 <= max(ball_counter.values()):
answer += 1
return answer
class Solution:
def winningPlayerCount(self, n: int, pick: List[List[int]]) -> int:
answer = 0
d = defaultdict(Counter)
for p, c in pick:
d[p][c] += 1
for key, value in d.items():
for i in value:
if value[i] >= key + 1:
answer += 1
break
return answer
https://leetcode.com/problems/number-of-changing-keys/
https://leetcode.com/problems/find-the-number-of-winning-players/
'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글
| Python - Algorithm #34 (0) | 2024.08.07 |
|---|---|
| Python - Algorithm #33 (0) | 2024.08.06 |
| Python - Algorithm #31 (0) | 2024.08.02 |
| Python - Algorithm #30 (0) | 2024.08.01 |
| Python - Algorithm #29 (0) | 2024.07.31 |