일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 매일
- 운동
- leetcode
- 리얼 클래스
- 만화도
- 영어공부
- 영어원서읽기
- 쓰릴오브파이트
- 프로젝트
- 괜찮음
- Writing
- 미드시청
- FIT XR
- Problem Solving
- realclass
- 링피트
- 30분
- 개발자
- 파비최
- 화상영어
- 3줄정리
- 사이드
- 읽기
- 10분
- 뭐든
- 잡생각
- 스탭퍼
- 월간
- Daily Challenge
- English
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.05.24 Today's Challenge 본문
https://leetcode.com/problems/maximum-subsequence-score/
Maximum Subsequence Score - LeetCode
Can you solve this real interview question? Maximum Subsequence Score - You are given two 0-indexed integer arrays nums1 and nums2 of equal length n and a positive integer k. You must choose a subsequence of indices from nums1 of length k. For chosen indic
leetcode.com
오늘도 힙을 이용해서 푸는 문제
class Solution:
def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:
pairs = [(a, b) for a, b in zip(nums1, nums2)]
pairs.sort(key=lambda x: -x[1])
tkh = [x[0] for x in pairs[:k]]
tks = sum(tkh)
heapq.heapify(tkh)
answer = tks * pairs[k-1][1]
for i in range(k, len(nums1)):
tks -= heapq.heappop(tkh)
tks += pairs[i][0]
heapq.heappush(tkh, pairs[i][0])
answer = max(answer, tks * pairs[i][1])
return answer
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.05.26 Today's Challenge (0) | 2023.05.26 |
---|---|
2023.05.25 Today's Challenge (0) | 2023.05.25 |
2023.05.23 Today's Challenge (0) | 2023.05.23 |
2023.05.22 Today's Challenge (0) | 2023.05.22 |
2023.05.21 Today's Challenge (1) | 2023.05.21 |
Comments