짬뽕얼큰하게의 맨땅에 헤딩 :: '2025/04 글 목록

'2025/04'에 해당되는 글 2건

Leetcode Problem:

Summary

  • Given a 2D array of questions where each question has a point value and a brainpower value, find the maximum points that can be earned by solving the questions in order.

Approach

  • Dynamic Programming: The problem can be solved using dynamic programming
  • The idea is to maintain a memoization table to store the maximum points that can be earned for each question
  • The function iterates over the questions in reverse order and for each question, it calculates the maximum points that can be earned by either solving the current question or skipping it.

Complexity

  • O(N) where N is the number of questions, as each question is visited once.

Solution Code:


class Solution {
public:
    long long memo[200001] = {0};
    long long mostPoints(vector>& questions) {
        int N = questions.size();
        for(int i = N - 1; i >= 0; i--){
            int point = questions[i][0];
            int brainpower = questions[i][1];
            memo[i] = max(point + memo[i + brainpower + 1], memo[i+1]);
        }
        return memo[0];
    }
};



'알고리즘' 카테고리의 다른 글

Put Marbles in Bags  (0) 2025.04.01
Partition Labels  (0) 2025.03.30
Apply Operations to Maximize Score  (0) 2025.03.29
Maximum Number of Points From Grid Queries  (0) 2025.03.28
Minimum Index of a Valid Split  (0) 2025.03.28
블로그 이미지

짬뽕얼큰하게

,

Put Marbles in Bags

알고리즘 2025. 4. 1. 01:21

Leetcode Problem:

Summary

  • The problem requires finding the difference between the maximum and minimum scores of marble distributions given an array of weights and the number of bags.

Approach

  • The solution uses a prefix sum array to efficiently calculate the sum of weights for each subarray
  • It then sorts the prefix sum array and iterates through it to calculate the minimum and maximum scores by adding the weights at specific indices
  • The difference between the maximum and minimum scores is then returned.

Complexity

  • O(n log n) due to the sorting of the prefix sum array, where n is the number of weights.

Solution Code:


class Solution {
public:
    long long putMarbles(vector& weights, int k) {
        if(weights.size() == k) return 0;
        vector prefixSum;
        long long sum = weights[0];
        for(int i = 1 ; i < weights.size(); i++){
            sum += weights[i];
            prefixSum.push_back(sum);
            sum -= weights[i-1];
        }
        sort(prefixSum.begin(), prefixSum.end());

        long long maxScore = weights[0] + weights[weights.size() - 1];
        long long minScore = weights[0] + weights[weights.size() - 1];
        for(int i = 0; i < k - 1; i++){
            minScore += prefixSum[i];
            maxScore += prefixSum[prefixSum.size() - 1 - i];
        }

        return maxScore - minScore;
    }
};

'알고리즘' 카테고리의 다른 글

Solving Questions With Brainpower  (0) 2025.04.01
Partition Labels  (0) 2025.03.30
Apply Operations to Maximize Score  (0) 2025.03.29
Maximum Number of Points From Grid Queries  (0) 2025.03.28
Minimum Index of a Valid Split  (0) 2025.03.28
블로그 이미지

짬뽕얼큰하게

,