ZoranPandovski / al-go-rithms

:musical_note: Algorithms written in different programming languages - https://zoranpandovski.github.io/al-go-rithms/
Creative Commons Zero v1.0 Universal
1.33k stars 1.95k forks source link

Create MERGE.cpp #3421

Open shubhgoel03 opened 1 year ago

shubhgoel03 commented 1 year ago

Approach: This problem can be solved using dynamic programming. Let’s define the states of the DP first. DP[l][r] will be the minimum cost of merging the subarray arr[l…r] into one. Given a list of N integers, the task is to merge all the elements of the list into one with the minimum possible cost. The rule for merging is as follows: Choose any two adjacent elements of the list with values say X and Y and merge them into a single element with value (X + Y) paying a total cost of (X + Y). The time complexity of the approach will be O(N3) as there are N * N states in total and each state takes O(N) time to solve in general.

Fixes issue: #[Mention the issue number it fixes or add the details of the changes if it doesn't has a specific issue.]

Changes: /The issue is not specific. I can't fix that/