일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 자연어처리
- 투포인터
- codeup
- SQL
- GenerativeAI
- 코드업
- 슬라이딩윈도우
- 니트코드
- nlp
- dfs
- array
- 리트코드
- 생성형AI
- two-pointer
- 파이썬기초100제
- LeetCode
- Stack
- gcp
- 파이썬알고리즘
- Greedy
- GenAI
- Python
- 알고리즘
- heap
- sql코테
- stratascratch
- 파이썬
- slidingwindow
- Python3
- 릿코드
Archives
- Today
- Total
Tech for good
[Leetcode/Heap] 3264. Final Array State After K Multiplication Operations I 본문
IT/Computer Science
[Leetcode/Heap] 3264. Final Array State After K Multiplication Operations I
Diana Kang 2025. 4. 7. 22:40

class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
n = len(nums)
# Build a heap of (value, index) for index tracking
heap = [(nums[i], i) for i in range(n)]
heapq.heapify(heap)
for _ in range(k):
while True:
val, idx = heapq.heappop(heap)
# Check if the heap value matches the current nums[idx]
if val == nums[idx]:
nums[idx] *= multiplier
# Push updated value back to heap
heapq.heappush(heap, (nums[idx], idx))
break # Exit inner while loop for next operation
return nums
- heap = [(nums[i], i) for i in range(n)]
- Build a list of tuples (value, index) for each element.
- This helps us track which value came from which index in nums.
- heapq.heapify(heap)
- Turn the list into a min-heap, where the smallest value (by value) is always on top.
- We build a min-heap where each element is a tuple (value, index):
heap = [(2, 0), (1, 1), (3, 2), (5, 3), (6, 4)]
## After heapq.heapify(heap) → heap structure internally is:
[(1, 1), (2, 0), (3, 2), (5, 3), (6, 4)] # (The heap ensures the smallest value is always at the top.)
'IT > Computer Science' 카테고리의 다른 글
[Leetcode/Heap] 1642. Furthest Building You Can Reach (0) | 2025.04.09 |
---|---|
[Leetcode/Heap] 451. Sort Characters By Frequency (0) | 2025.04.07 |
[Leetcode/Sort, Heap] 2974. Minimum Number Game (0) | 2025.04.06 |
[Leetcode/Heap] 2558. Take Gifts From the Richest Pile (0) | 2025.04.04 |
[Leetcode/Heap] 2099. Find Subsequence of Length K With the Largest Sum (0) | 2025.04.04 |