일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 사이드
- realclass
- 프로젝트
- 파비최
- 스탭퍼
- 잡생각
- 매일
- Daily Challenge
- 링피트
- 영어공부
- 쓰릴오브파이트
- 리얼 클래스
- 미드시청
- 영어원서읽기
- 월간
- 개발자
- 뭐든
- English
- 화상영어
- 괜찮음
- 3줄정리
- Writing
- 만화도
- FIT XR
- 운동
- Problem Solving
- leetcode
- 읽기
- 30분
- 10분
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.14 Today's Challenge 본문
https://leetcode.com/problems/maximize-score-after-n-operations/
Maximize Score After N Operations - LeetCode
Can you solve this real interview question? Maximize Score After N Operations - You are given nums, an array of positive integers of size 2 * n. You must perform n operations on this array. In the ith operation (1-indexed), you will: * Choose two elements,
leetcode.com
꽤 복잡한 문제였음
class Solution:
def maxScore(self, nums: List[int]) -> int:
max_states = 1 << len(nums)
final_mask = max_states - 1
dp = [0] * max_states
for state in range(final_mask, -1, -1):
if state == final_mask:
dp[state] = 0
continue
numbers_taken = bin(state).count('1')
pairs_formed = numbers_taken // 2
if numbers_taken % 2:
continue
for fi in range(len(nums)):
for si in range(fi+1, len(nums)):
if (state >> fi & 1) == 1 or (state >> si & 1) == 1:
continue
current_score = (pairs_formed+1) * math.gcd(nums[fi], nums[si])
state_after_picking_curr_pair = state | (1 << fi) | (1 << si)
remaining_score = dp[state_after_picking_curr_pair]
dp[state] = max(dp[state], current_score + remaining_score)
return dp[0]
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.16 Today's Challenge (0) | 2023.05.16 |
---|---|
2023.05.15 Today's Challenge (1) | 2023.05.15 |
2023.05.13 Today's Challenge (1) | 2023.05.13 |
2023.05.12 Today's Challenge (0) | 2023.05.12 |
2023.05.11 Today's Challenge (0) | 2023.05.11 |
Comments