Python Algorithm
LeetCode
3264. Final Array State After K Multiplication Operations I
- You are given an integer array [nums], an integer k, and an integer multiplier.
- You need to perform k operations on [nums]. In each operation:
- Find the minimum value x in [nums]. If there are multiple occurrences of the minimum value, select the one that appears first.
- Replace the selected minimum value x with x ⨉ multiplier.
- Return an integer array denoting the final state of [nums] after performing all k operations.
- Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ nums[i] ≤ 100
- 1 ≤ k ≤ 10
- 1 ≤ multiplier ≤ 5
class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
for i in range(k):
min_value = min(nums)
min_index = nums.index(min_value)
# min_index 위치에 multiplier만큼 곱해주기
nums[min_index] *= multiplier
return nums
class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
for i in range(k):
min_index = nums.index(min(nums))
nums[min_index] *= multiplier
return nums
1957. Delete Characters to Make Fancy String
- A fancy string is a string where no three consecutive characters are equal.
- Given a string s, delete the minimum possible number of characters from s to make it fancy.
- Return the final string after the deletion. It can be shown that the answer will always be unique.
- Constraints
- 1 ≤ s.length ≤ 10⁵
- s consists only of lowercase English letters.
class Solution:
def makeFancyString(self, s: str) -> str:
answer = ""
prev = ""
cnt = 0
for c in s:
# same: cnt 값을 1 더해줌
# differenct: cnt 값을 1로 초기화
# cnt 값이 3보다 작으면 answer에 추가
if c == prev:
cnt += 1
else:
cnt = 1
if cnt < 3:
answer += c
prev = c
return answer
class Solution:
def makeFancyString(self, s: str) -> str:
seen = []
for c in s:
if (len(seen) >= 2) and (c == seen[-1]) and (c == seen[-2]):
continue
else:
seen.append(c)
return "".join(seen)
https://leetcode.com/problems/final-array-state-after-k-multiplication-operations-i/
https://leetcode.com/problems/delete-characters-to-make-fancy-string/
'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글
| Python - Algorithm #42 (0) | 2024.08.29 |
|---|---|
| Python - Algorithm #41 (0) | 2024.08.28 |
| Python - Algorithm #39 (0) | 2024.08.26 |
| Python - Algorithm #38 (0) | 2024.08.23 |
| Python - Algorithm #37 (0) | 2024.08.12 |