Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 잇츠디모
- python sorted
- 코틀린기초
- python zip_longest
- binary search
- 파이썬 알고리즘
- 알고리즘풀이
- leetcode 풀기
- 릿코드풀기
- 파이썬 릿코드
- LeetCode
- 파이썬알고리즘
- python 릿코드
- 파이썬 프로그래머스
- 알고리즘풀기
- python priority queue
- 릿코드
- 릿코드 파이썬
- 파이썬릿코드
- python Leetcode
- 상가수익률계산기
- 파이썬알고리즘풀기
- leetcode풀기
- 파이썬 알고리즘 풀기
- 릿코드 풀기
- 파이썬릿코드풀기
- python 알고리즘
- 릿코드풀이
- leetcode풀이
- python xor
Archives
- Today
- Total
소프트웨어에 대한 모든 것
2233. Maximum Product After K Increments 본문
반응형
문제)
2233. Maximum Product After K Increments
You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.
Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7. Note that you should maximize the product before taking the modulo.
Example 1:
Input: nums = [0,4], k = 5
Output: 20
Explanation: Increment the first number 5 times.
Now nums = [5, 4], with a product of 5 * 4 = 20.
It can be shown that 20 is maximum product possible, so we return 20.
Note that there may be other ways to increment nums to have the maximum product.
Example 2:
Input: nums = [6,3,3,2], k = 2
Output: 216
Explanation: Increment the second number 1 time and increment the fourth number 1 time.
Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
It can be shown that 216 is maximum product possible, so we return 216.
Note that there may be other ways to increment nums to have the maximum product.
Constraints:
- 1 <= nums.length, k <= 105
- 0 <= nums[i] <= 106
솔루션1)
- heapq 자료 구조 사용
class Solution:
def maximumProduct(self, nums: List[int], k: int) -> int:
heapq.heapify(nums)
for i in range(k):
n = heapq.heappop(nums)
heapq.heappush(nums, n+1)
return reduce(lambda a, b: (a*b) % (10**9+7), nums, 1)
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
57. Insert Interval (0) | 2023.01.17 |
---|---|
100. Same Tree (0) | 2023.01.10 |
520. Detect Capital (0) | 2023.01.02 |
1834. Single-Threaded CPU (0) | 2023.01.02 |
290. Word Pattern (0) | 2023.01.01 |
Comments