|
| 1 | +<h1 align="center">Shop - in Candy - Store</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Shop in Candy Store](https://www.geeksforgeeks.org/problems/shop-in-candy-store1145/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Problem Explanation |
| 10 | +The problem revolves around buying candies from a store where there is a special offer: |
| 11 | +- For every candy you buy, ( K ) candies can be chosen for free. |
| 12 | +- The objective is to calculate: |
| 13 | + 1. **Minimum cost**: The least amount you would spend if you try to minimize your cost. |
| 14 | + 2. **Maximum cost**: The most amount you would spend if you maximize your spending. |
| 15 | + |
| 16 | +The approach involves sorting the candy prices and strategically selecting which candies to buy and which to get for free. |
| 17 | + |
| 18 | +#### Example: |
| 19 | +- **Input**: |
| 20 | + `candies[] = {3, 2, 1, 4}` (cost of candies) |
| 21 | + `N = 4` (total candies) |
| 22 | + `K = 2` (for every candy bought, 2 are free) |
| 23 | +- **Output**: |
| 24 | + `Minimum Cost = 3`, `Maximum Cost = 7` |
| 25 | + |
| 26 | + |
| 27 | +### Greedy Algorithm Approach: |
| 28 | + |
| 29 | +#### Step-by-Step Explanation: |
| 30 | +1. **Sort the Candies Array**: |
| 31 | + - Sorting ensures that candies are arranged from cheapest to most expensive. This simplifies minimizing and maximizing costs. |
| 32 | + - Example: After sorting, `candies[] = {1, 2, 3, 4}`. |
| 33 | + |
| 34 | +2. **Calculate Minimum Cost**: |
| 35 | + - Start buying the cheapest candies first. |
| 36 | + - Each time you buy a candy, ( K ) of the most expensive remaining candies are considered "free." |
| 37 | + - Example: |
| 38 | + - Buy candy ( 1 ) (cost = 1), get candies ( 4 ) and ( 3 ) free. |
| 39 | + - Buy candy ( 2 ) (cost = 2). |
| 40 | + - Total minimum cost = ( 1 + 2 = 3 ). |
| 41 | + |
| 42 | +3. **Calculate Maximum Cost**: |
| 43 | + - Start buying the most expensive candies first. |
| 44 | + - Each time you buy a candy, ( K ) of the cheapest remaining candies are considered "free." |
| 45 | + - Example: |
| 46 | + - Buy candy ( 4 ) (cost = 4), get candies ( 1 ) and ( 2 ) free. |
| 47 | + - Buy candy ( 3 ) (cost = 3). |
| 48 | + - Total maximum cost = ( 4 + 3 = 7 ). |
| 49 | + |
| 50 | +4. **Return Results**: |
| 51 | + - Combine the results into a single vector, `[Minimum Cost, Maximum Cost]`. |
| 52 | + |
| 53 | +## Problem Solution |
| 54 | +```cpp |
| 55 | +class Solution { |
| 56 | +public: |
| 57 | + vector<int> candyStore(int candies[], int N, int K) { |
| 58 | + // Sort the candies array in ascending order to find minimum cost easily |
| 59 | + sort(candies, candies + N); |
| 60 | + |
| 61 | + // Variables for calculating minimum cost |
| 62 | + int mini = 0; // Stores the minimum cost |
| 63 | + int free = N - 1; // Points to the most expensive candy available |
| 64 | + int buy = 0; // Points to the least expensive candy to buy |
| 65 | + |
| 66 | + // Calculate the minimum cost of buying candies |
| 67 | + while (buy <= free) { |
| 68 | + mini += candies[buy]; // Add the cost of the current candy being bought |
| 69 | + buy++; // Move to the next candy to buy |
| 70 | + free -= K; // Reduce the free candies available (K candies become free per purchase) |
| 71 | + } |
| 72 | + |
| 73 | + // Variables for calculating maximum cost |
| 74 | + int maxi = 0; // Stores the maximum cost |
| 75 | + free = 0; // Points to the least expensive candy available |
| 76 | + buy = N - 1; // Points to the most expensive candy to buy |
| 77 | + |
| 78 | + // Calculate the maximum cost of buying candies |
| 79 | + while (buy >= free) { |
| 80 | + maxi += candies[buy]; // Add the cost of the current candy being bought |
| 81 | + buy--; // Move to the next candy to buy (from most expensive to least expensive) |
| 82 | + free += K; // Increase the number of free candies available (from cheapest candies) |
| 83 | + } |
| 84 | + |
| 85 | + // Return the minimum and maximum costs as a vector |
| 86 | + return {mini, maxi}; |
| 87 | + } |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +## Problem Solution Explanation |
| 92 | + |
| 93 | +```cpp |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + vector<int> candyStore(int candies[], int N, int K) { |
| 97 | +``` |
| 98 | +- **Function Definition**: This defines the function `candyStore` which takes three arguments: |
| 99 | + - `candies[]`: an array of integers representing the price of each candy. |
| 100 | + - `N`: the number of candies. |
| 101 | + - `K`: the number of free candies you get for every candy you buy. |
| 102 | + |
| 103 | + **Example**: |
| 104 | + `candies[] = {3, 2, 1, 4}` |
| 105 | + `N = 4` (there are 4 candies) |
| 106 | + `K = 2` (for each candy bought, 2 candies are free) |
| 107 | +
|
| 108 | +
|
| 109 | +### Sorting the Candies Array |
| 110 | +```cpp |
| 111 | + sort(candies, candies + N); |
| 112 | +``` |
| 113 | +- **Purpose**: This line sorts the `candies` array in ascending order to make it easier to calculate the minimum and maximum cost. |
| 114 | +- **Example**: |
| 115 | + - Before sorting: `candies[] = {3, 2, 1, 4}` |
| 116 | + - After sorting: `candies[] = {1, 2, 3, 4}` |
| 117 | + |
| 118 | + |
| 119 | +### Initializing Variables for Minimum Cost Calculation |
| 120 | +```cpp |
| 121 | + int mini = 0; // Initialize variable to store the minimum cost |
| 122 | + int free = N - 1; // Pointer to the most expensive candy (start from the last element) |
| 123 | + int buy = 0; // Pointer to the cheapest candy (start from the first element) |
| 124 | +``` |
| 125 | +- **Purpose**: These variables are initialized for the minimum cost calculation: |
| 126 | + - `mini`: To keep track of the total cost when buying candies. |
| 127 | + - `free`: Points to the most expensive candy available (used to simulate receiving free candies). |
| 128 | + - `buy`: Points to the least expensive candy available (used to simulate purchasing candies). |
| 129 | + |
| 130 | + |
| 131 | +### Calculating Minimum Cost |
| 132 | +```cpp |
| 133 | + while (buy <= free) { |
| 134 | + mini += candies[buy]; // Add the price of the candy being bought |
| 135 | + buy++; // Move to the next cheapest candy to buy |
| 136 | + free -= K; // Reduce the free candies available (K candies become free) |
| 137 | + } |
| 138 | +``` |
| 139 | +- **Purpose**: This loop simulates buying candies in the most cost-efficient way: |
| 140 | + - Buy the cheapest candy (`candies[buy]`), and after each purchase, you get ( K ) free candies. |
| 141 | + - The loop continues as long as there are candies to buy (`buy <= free`). |
| 142 | +
|
| 143 | +- **Example**: |
| 144 | + - `candies[] = {1, 2, 3, 4}` (after sorting) |
| 145 | + - Initially, `buy = 0`, `free = 3` |
| 146 | + - First iteration: Buy candy 1 (cost = 1), update `mini = 1`, `buy = 1`, `free = 1` (2 candies are free now) |
| 147 | + - Second iteration: Buy candy 2 (cost = 2), update `mini = 3`, `buy = 2`, `free = -1` (no more free candies) |
| 148 | +
|
| 149 | +
|
| 150 | +### Initializing Variables for Maximum Cost Calculation |
| 151 | +```cpp |
| 152 | + int maxi = 0; // Initialize variable to store the maximum cost |
| 153 | + free = 0; // Pointer to the least expensive candy available |
| 154 | + buy = N - 1; // Pointer to the most expensive candy (start from the last element) |
| 155 | +``` |
| 156 | +- **Purpose**: These variables are initialized for the maximum cost calculation: |
| 157 | + - `maxi`: To keep track of the total cost when maximizing the number of candies bought. |
| 158 | + - `free`: Points to the least expensive candy available (used to simulate receiving free candies). |
| 159 | + - `buy`: Points to the most expensive candy available (used to simulate purchasing candies). |
| 160 | + |
| 161 | + |
| 162 | +### Calculating Maximum Cost |
| 163 | +```cpp |
| 164 | + while (buy >= free) { |
| 165 | + maxi += candies[buy]; // Add the price of the candy being bought |
| 166 | + buy--; // Move to the next most expensive candy to buy |
| 167 | + free += K; // Increase the number of free candies available (from cheapest candies) |
| 168 | + } |
| 169 | +``` |
| 170 | +- **Purpose**: This loop simulates buying candies in the most expensive way: |
| 171 | + - Buy the most expensive candy (`candies[buy]`), and after each purchase, you get ( K ) free candies. |
| 172 | + - The loop continues as long as there are candies to buy (`buy >= free`). |
| 173 | +
|
| 174 | +- **Example**: |
| 175 | + - `candies[] = {1, 2, 3, 4}` (after sorting) |
| 176 | + - Initially, `buy = 3`, `free = 0` |
| 177 | + - First iteration: Buy candy 4 (cost = 4), update `maxi = 4`, `buy = 2`, `free = 2` (2 candies are free now) |
| 178 | + - Second iteration: Buy candy 3 (cost = 3), update `maxi = 7`, `buy = 1`, `free = 4` (4 candies are free now) |
| 179 | +
|
| 180 | +
|
| 181 | +### Returning Results |
| 182 | +```cpp |
| 183 | + return {mini, maxi}; // Return the minimum and maximum costs as a vector |
| 184 | +} |
| 185 | +``` |
| 186 | +- **Purpose**: After calculating both minimum and maximum costs, the function returns these values as a vector. |
| 187 | +- **Example**: |
| 188 | + - After the calculations: |
| 189 | + - `mini = 3` |
| 190 | + - `maxi = 7` |
| 191 | + - The return value is `{3, 7}`. |
| 192 | + |
| 193 | + |
| 194 | +### Example: |
| 195 | + |
| 196 | +#### Input: |
| 197 | +- `candies[] = {3, 2, 1, 4}` |
| 198 | +- `N = 4` (total number of candies) |
| 199 | +- `K = 2` (for every candy bought, 2 candies are free) |
| 200 | + |
| 201 | +#### Execution: |
| 202 | + |
| 203 | +1. **Sorting**: |
| 204 | + - Sorted candies array: `candies[] = {1, 2, 3, 4}`. |
| 205 | + |
| 206 | +2. **Calculating Minimum Cost**: |
| 207 | + - Initially, `buy = 0` (cheapest candy), `free = 3` (most expensive candy). |
| 208 | + - First iteration: Buy candy ( 1 ), cost = 1, update `mini = 1`, `buy = 1`, `free = 1` (2 free candies). |
| 209 | + - Second iteration: Buy candy ( 2 ), cost = 2, update `mini = 3`, `buy = 2`, `free = -1`. |
| 210 | + - **Total minimum cost** = `3`. |
| 211 | + |
| 212 | +3. **Calculating Maximum Cost**: |
| 213 | + - Initially, `buy = 3` (most expensive candy), `free = 0` (cheapest candy). |
| 214 | + - First iteration: Buy candy ( 4 ), cost = 4, update `maxi = 4`, `buy = 2`, `free = 2` (2 free candies). |
| 215 | + - Second iteration: Buy candy ( 3 ), cost = 3, update `maxi = 7`, `buy = 1`, `free = 4`. |
| 216 | + - **Total maximum cost** = `7`. |
| 217 | + |
| 218 | +#### Output: |
| 219 | +- The final output is the vector `[3, 7]`, where `3` is the minimum cost and `7` is the maximum cost. |
| 220 | + |
| 221 | + |
| 222 | +### Time and Space Complexity: |
| 223 | + |
| 224 | +1. **Time Complexity**: |
| 225 | + - Sorting the array: ( O(N log N) ), where ( N ) is the number of candies. |
| 226 | + - The loops for calculating minimum and maximum cost: ( O(N) ), since each loop runs for at most ( N ) iterations. |
| 227 | + - **Overall Time Complexity** = ( O(N log N) ). |
| 228 | + |
| 229 | +2. **Space Complexity**: |
| 230 | + - The function uses a constant amount of extra space apart from the input array. No additional data structures are used beyond a few variables. |
| 231 | + - **Overall Space Complexity** = ( O(1) ). |
| 232 | + |
0 commit comments