I don't know how to solve this problem.

Asked 2 years ago, Updated 2 years ago, 39 views

The problem is that there are three boxes called ab c. A is a box that can hold 1 kg. B is a box that can hold 1.3 kg. C is a box that can hold 1.6 kg. At this time, the values of boxes a, b, and c are 3,000 won, 4,000 won, and 5,000 won, respectively. So 2.2kg of apples (e.g. random weight) is to use boxes a, b, c to get the minimum cost of each box when you have to use boxes a, b, c to get the apples. How do we approach that?

It's not Baekjun or programmers, but the company has to make a program, but I can't think of the answer.

algorithm

2022-09-21 11:44

1 Answers

A dynamic programming approach is most likely to be approached for the kind of problem-solving that is difficult to conclude unless you have tried all the cases.

It refers to a programming technique that obtains the result values of each subroutine and reaches its final purpose By storing and reusing values obtained from repeated subroutines, the key is to reduce execution time.

There's a way to use a recursion There is also a way to record and retrieve previous tasks by circling a typical iteration. Or both could be used. And you can solve one problem in many ways. However, no matter how it is implemented, it can be solved by remembering that subroutine and value reuse are the core of the DP.

Below is the pseudo-code using recursion.

input = 2.2

kgs = [1, 1.3, 1.6]
wons = [3000, 4000, 5000]

function getMinCost (target) {
    // Exception handling when input is 0.
    // You don't need to block it outside of the function in advance.
    if target == 0
        return 0

    minCost = INFINITY

    for (i = 0; i < kgs.length; i++) {
        rest = target - kgs[i]
        minCost = MIN(minCost, wons[i] + (rest > 0 ? getMinCost(rest) : 0))
    }

    return minCost
}

print getMinCost(input)

Currently, the above code is not enough to call it an optimized algorithm. Due to the nature of the DP problem, there is no special harm, so the number of cases is checked, and the above code is also focused on handling all cases, but there is no magic using the DP characteristics.

Memoization based on target values can help you get answers more efficiently. That's what DP is all about.

I believe that you'll make a memo for yourself, and I'll get going.


2022-09-21 11:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.