일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- component
- treenode
- 비트연산
- map
- priority_queue
- nodeJS
- count
- node.js
- server
- Navigation
- Context
- JSX
- c++
- axios
- UE5
- Callback
- DP
- BinaryTree
- bit
- state
- Props
- event
- array
- css
- React
- leetcode
- routes
- route
- queue
- MySQL
- Today
- Total
우사미 코딩
[Leetcode] 2542. Maximum Subsequence Score 본문
1. 문제링크
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
- nums1 : sum nums
- nums2 : mul nums (여기서 최소값만 사용)
2. 키 포인트
1) 배열은 nums2기준으로 정렬한다
2) priority_queue를 사용한다
pq사이즈가 k가 될 때, 숫자의 합과 최소 min으로 곱한 값 중에 최댓값을 업데이트해준다
pq사이즈는 k가 넘으면 pop해준다
3. solution
class Solution {
public:
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {
vector<pair<int,int>>v;
int size = nums1.size();
for(int i = 0; i < size; i++){
v.push_back({nums2[i], nums1[i]});
}
sort(v.begin(), v.end());
priority_queue<int, vector<int>, greater<int>> pq;
long long ans = 0, sum = 0;
for(int i = size-1; i >= 0; i--){
if(pq.size() >= k){
sum -= pq.top();
pq.pop();
}
sum += v[i].second;
pq.push(v[i].second);
if(pq.size() == k)
ans = max(ans, v[i].first * sum);
}
return ans;
}
};
'Leetcode' 카테고리의 다른 글
[Leetcode] 2090. K Radius Subarray Averages (0) | 2023.06.21 |
---|---|
[LeetCode] 450. Delete Node in a BST (0) | 2023.06.20 |
[Leetcode] 43. Multiply Strings (0) | 2023.05.28 |
[Leetcode] 322. Coin Change - DP (0) | 2023.05.28 |
[Leetcode] 210. Course Schedule II - Topological Sorting (0) | 2023.05.27 |