|
| 1 | +<h1 align='center'>Minimum - Cost - Of - Ropes</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Minimum Cost of Ropes](https://www.geeksforgeeks.org/problems/minimum-cost-of-ropes-1587115620/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | +**Problem Statement**: |
| 12 | +You are given an array, `arr`, where each element represents the length of a rope. Your task is to **combine all ropes into one single rope with the minimum cost**. The cost to connect two ropes is the sum of their lengths, and this combined rope can be used to connect with others in subsequent steps. |
| 13 | + |
| 14 | +**Goal**: |
| 15 | +Find the minimum total cost required to combine all ropes. |
| 16 | + |
| 17 | +**Example**: |
| 18 | +1. **Input**: |
| 19 | + ``` |
| 20 | + arr = [4, 3, 2, 6] |
| 21 | + ``` |
| 22 | + - **Output**: `29` |
| 23 | + - **Explanation**: |
| 24 | + - Step 1: Combine `2 + 3 = 5`, cost = `5`. |
| 25 | + - Step 2: Combine `4 + 5 = 9`, cost = `5 + 9 = 14`. |
| 26 | + - Step 3: Combine `6 + 9 = 15`, cost = `14 + 15 = 29`. |
| 27 | + |
| 28 | +2. **Input**: |
| 29 | + ``` |
| 30 | + arr = [1, 2, 3, 4, 5] |
| 31 | + ``` |
| 32 | + - **Output**: `33` |
| 33 | + - **Explanation**: |
| 34 | + - Step 1: Combine `1 + 2 = 3`, cost = `3`. |
| 35 | + - Step 2: Combine `3 + 3 = 6`, cost = `3 + 6 = 9`. |
| 36 | + - Step 3: Combine `4 + 6 = 10`, cost = `9 + 10 = 19`. |
| 37 | + - Step 4: Combine `5 + 10 = 15`, cost = `19 + 15 = 33`. |
| 38 | + |
| 39 | +**Approach**: |
| 40 | +1. **Use a Min-Heap** (priority queue): |
| 41 | + - Insert all elements (rope lengths) into a min-heap. This ensures that the smallest elements are always at the top. |
| 42 | +2. **Combine the Two Smallest Ropes**: |
| 43 | + - Repeatedly remove the two smallest ropes from the min-heap. |
| 44 | + - Add their lengths together to get the cost for that combination and push the combined rope back into the heap. |
| 45 | + - Accumulate this cost until only one rope remains in the heap. |
| 46 | + |
| 47 | +This approach guarantees the minimum cost because it prioritizes combining the smallest lengths first, reducing the incremental costs at each step. |
| 48 | + |
| 49 | +## Problem Solution |
| 50 | +```cpp |
| 51 | +class Solution { |
| 52 | + public: |
| 53 | + |
| 54 | + long long minCost(vector<long long>& arr) { |
| 55 | + |
| 56 | + priority_queue<long long, vector<long long>, greater<long long>> pq; |
| 57 | + |
| 58 | + for(int i = 0; i < arr.size(); i++){ |
| 59 | + pq.push(arr[i]); |
| 60 | + } |
| 61 | + |
| 62 | + long long cost = 0; |
| 63 | + while(pq.size() > 1){ |
| 64 | + long long a = pq.top(); |
| 65 | + pq.pop(); |
| 66 | + |
| 67 | + long long b = pq.top(); |
| 68 | + pq.pop(); |
| 69 | + |
| 70 | + long long sum = a + b; |
| 71 | + cost += sum; |
| 72 | + |
| 73 | + pq.push(sum); |
| 74 | + } |
| 75 | + |
| 76 | + return cost; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +``` |
| 81 | +
|
| 82 | +## Problem Solution Explanation |
| 83 | +
|
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | + public: |
| 87 | + |
| 88 | + long long minCost(vector<long long>& arr) { |
| 89 | + |
| 90 | + priority_queue<long long, vector<long long>, greater<long long>> pq; |
| 91 | +``` |
| 92 | +- **`minCost` function**: Takes an array `arr` as input and returns the minimum cost to combine all ropes. |
| 93 | +- **Min-Heap Declaration**: `priority_queue` is initialized as a min-heap using `greater<long long>`. This ensures the smallest elements are at the top. |
| 94 | + |
| 95 | +```cpp |
| 96 | + for(int i = 0; i < arr.size(); i++){ |
| 97 | + pq.push(arr[i]); |
| 98 | + } |
| 99 | +``` |
| 100 | +- **Push Elements into Min-Heap**: All elements of `arr` are pushed into `pq`, building a min-heap with the initial rope lengths. |
| 101 | +
|
| 102 | +```cpp |
| 103 | + long long cost = 0; |
| 104 | + while(pq.size() > 1){ |
| 105 | + long long a = pq.top(); |
| 106 | + pq.pop(); |
| 107 | + |
| 108 | + long long b = pq.top(); |
| 109 | + pq.pop(); |
| 110 | + |
| 111 | + long long sum = a + b; |
| 112 | + cost += sum; |
| 113 | + |
| 114 | + pq.push(sum); |
| 115 | + } |
| 116 | +``` |
| 117 | +- **Initialize Total Cost**: `cost` is set to `0` and will store the cumulative cost of combining ropes. |
| 118 | +- **Loop**: While the size of the heap is greater than 1: |
| 119 | + - **Extract the Two Smallest Ropes**: `a` and `b` are the two smallest ropes. After accessing each, they are removed from `pq`. |
| 120 | + - **Calculate Combination Cost**: `sum = a + b` is the cost of combining `a` and `b`. |
| 121 | + - **Accumulate Cost**: Add `sum` to `cost`. |
| 122 | + - **Insert Combined Rope**: Push the combined rope length `sum` back into `pq` for further combinations if needed. |
| 123 | + |
| 124 | +```cpp |
| 125 | + return cost; |
| 126 | + } |
| 127 | +}; |
| 128 | +``` |
| 129 | +- **Return Total Cost**: After all ropes are combined, `cost` holds the minimum total cost. |
| 130 | + |
| 131 | +### Step 3: Example Walkthrough |
| 132 | + |
| 133 | +1. **Example 1**: |
| 134 | + - **Input**: `arr = [4, 3, 2, 6]` |
| 135 | + - **Process**: |
| 136 | + - Step 1: Heap = `[2, 3, 4, 6]` |
| 137 | + - Combine `2 + 3 = 5`, cost = `5`. |
| 138 | + - New Heap = `[4, 5, 6]` |
| 139 | + - Step 2: |
| 140 | + - Combine `4 + 5 = 9`, cost = `5 + 9 = 14`. |
| 141 | + - New Heap = `[6, 9]` |
| 142 | + - Step 3: |
| 143 | + - Combine `6 + 9 = 15`, cost = `14 + 15 = 29`. |
| 144 | + - New Heap = `[15]` |
| 145 | + - **Output**: `29` |
| 146 | + |
| 147 | +2. **Example 2**: |
| 148 | + - **Input**: `arr = [1, 2, 3, 4, 5]` |
| 149 | + - **Process**: |
| 150 | + - Step 1: Heap = `[1, 2, 3, 4, 5]` |
| 151 | + - Combine `1 + 2 = 3`, cost = `3`. |
| 152 | + - New Heap = `[3, 3, 4, 5]` |
| 153 | + - Step 2: |
| 154 | + - Combine `3 + 3 = 6`, cost = `3 + 6 = 9`. |
| 155 | + - New Heap = `[4, 5, 6]` |
| 156 | + - Step 3: |
| 157 | + - Combine `4 + 6 = 10`, cost = `9 + 10 = 19`. |
| 158 | + - New Heap = `[5, 10]` |
| 159 | + - Step 4: |
| 160 | + - Combine `5 + 10 = 15`, cost = `19 + 15 = 33`. |
| 161 | + - New Heap = `[15]` |
| 162 | + - **Output**: `33` |
| 163 | + |
| 164 | +### Step 4: Time and Space Complexity |
| 165 | + |
| 166 | +- **Time Complexity**: |
| 167 | + - Pushing elements into the min-heap initially takes **O(n log n)**, where `n` is the number of ropes. |
| 168 | + - In the main loop, for each iteration: |
| 169 | + - We remove the two smallest elements and insert their sum back, which takes **O(log n)**. |
| 170 | + - Since we perform this operation `n - 1` times, the time complexity is **O(n log n)**. |
| 171 | + - Overall time complexity: **O(n log n)**. |
| 172 | + |
| 173 | +- **Space Complexity**: |
| 174 | + - We use a min-heap `pq` that holds up to `n` elements, so the space complexity is **O(n)**. |
| 175 | + |
| 176 | +### Step 5: Additional Recommendations for Students |
| 177 | + |
| 178 | +- **Min-Heap Mastery**: Practice implementing and using min-heaps for similar greedy problems. |
| 179 | +- **Greedy Choice Property**: This problem demonstrates the greedy approach by always combining the smallest ropes first to minimize cost. |
| 180 | +- **Alternative Practice**: Try implementing this with custom heap logic to better understand heap operations if the priority queue is still challenging. |
| 181 | +- **Edge Cases**: Try testing with single or minimal ropes (e.g., `arr = [5]`), where the cost should be `0`, as no combination is needed. |
0 commit comments