|
| 1 | +<h1 align="center">Maximum - Money</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Maximum Money](https://www.geeksforgeeks.org/problems/maximum-money2855/1?itm_source=geeksforgeeks&itm_medium=article&itm_campaign=practice_card) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Problem Explanation |
| 10 | +The problem involves a **thief** who wants to rob money from a row of houses. Each house contains a fixed amount of money, denoted as `K`. However, the thief has a constraint: **he cannot rob two adjacent houses**. The task is to find the **maximum amount of money** the thief can rob while adhering to this rule. |
| 11 | + |
| 12 | +### Input |
| 13 | + |
| 14 | +- `N`: The number of houses. |
| 15 | +- `K`: The amount of money present in each house. |
| 16 | + |
| 17 | +### Output |
| 18 | + |
| 19 | +- The maximum amount of money the thief can rob, following the rule that no two adjacent houses can be robbed. |
| 20 | + |
| 21 | +### Constraints |
| 22 | + |
| 23 | +- `1 ≤ N, K ≤ 1000` |
| 24 | + |
| 25 | +### Example 1 |
| 26 | + |
| 27 | +**Input**: |
| 28 | +``` |
| 29 | +N = 5, K = 10 |
| 30 | +``` |
| 31 | + |
| 32 | +**Explanation**: |
| 33 | +- There are 5 houses, and each house contains 10 units of money. |
| 34 | +- Since the thief cannot rob two adjacent houses, the best strategy is to rob every alternate house. |
| 35 | +- The possible houses the thief can rob are: |
| 36 | + - Rob house 1 (house 1 contains 10 money). |
| 37 | + - Skip house 2. |
| 38 | + - Rob house 3 (house 3 contains 10 money). |
| 39 | + - Skip house 4. |
| 40 | + - Rob house 5 (house 5 contains 10 money). |
| 41 | + |
| 42 | +Thus, the maximum amount of money the thief can rob is: |
| 43 | +``` |
| 44 | +10 + 10 + 10 = 30 |
| 45 | +``` |
| 46 | + |
| 47 | +**Output**: |
| 48 | +``` |
| 49 | +30 |
| 50 | +``` |
| 51 | + |
| 52 | +### Example 2 |
| 53 | + |
| 54 | +**Input**: |
| 55 | +``` |
| 56 | +N = 2, K = 12 |
| 57 | +``` |
| 58 | + |
| 59 | +**Explanation**: |
| 60 | +- There are 2 houses, and each house contains 12 units of money. |
| 61 | +- Since the thief cannot rob two adjacent houses, he can only rob one of the two houses. |
| 62 | +- The possible choices are: |
| 63 | + - Rob house 1 (house 1 contains 12 money). |
| 64 | + - Or rob house 2 (house 2 contains 12 money). |
| 65 | + |
| 66 | +Thus, the maximum amount of money the thief can rob is: |
| 67 | +``` |
| 68 | +12 |
| 69 | +``` |
| 70 | + |
| 71 | +**Output**: |
| 72 | +``` |
| 73 | +12 |
| 74 | +``` |
| 75 | + |
| 76 | +### Example 3 |
| 77 | + |
| 78 | +**Input**: |
| 79 | +``` |
| 80 | +N = 6, K = 7 |
| 81 | +``` |
| 82 | + |
| 83 | +**Explanation**: |
| 84 | +- There are 6 houses, each containing 7 units of money. |
| 85 | +- The best strategy would be for the thief to rob every alternate house to maximize the money. |
| 86 | +- The possible houses the thief can rob are: |
| 87 | + - Rob house 1 (7 money). |
| 88 | + - Skip house 2. |
| 89 | + - Rob house 3 (7 money). |
| 90 | + - Skip house 4. |
| 91 | + - Rob house 5 (7 money). |
| 92 | + - Skip house 6. |
| 93 | + |
| 94 | +Thus, the maximum amount of money the thief can rob is: |
| 95 | +``` |
| 96 | +7 + 7 + 7 = 21 |
| 97 | +``` |
| 98 | + |
| 99 | +**Output**: |
| 100 | +``` |
| 101 | +21 |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +### Detailed Explanation |
| 106 | + |
| 107 | +1. **Key Insight**: |
| 108 | + - The problem essentially asks how many houses the thief can rob if he is constrained from robbing two adjacent houses. |
| 109 | + - The optimal strategy is to **rob every alternate house**, starting from either the first or the second house, depending on which gives the best result. |
| 110 | + |
| 111 | +2. **Mathematical Insight**: |
| 112 | + - The problem can be boiled down to **maximizing the number of non-adjacent houses** the thief can rob. This is simply **`ceil(N / 2)`**, because the thief can rob every alternate house. |
| 113 | + - If `N` is odd, the thief can rob `(N + 1) / 2` houses. If `N` is even, the thief can rob `N / 2` houses. |
| 114 | + |
| 115 | + - This can be expressed as: |
| 116 | + ``` |
| 117 | + max_money = ceil(N / 2) * K |
| 118 | + ``` |
| 119 | + where `K` is the money in each house. |
| 120 | +
|
| 121 | +
|
| 122 | +## Problem Solution |
| 123 | +```cpp |
| 124 | +class Solution { |
| 125 | + public: |
| 126 | + // Function to calculate the maximum money the thief can rob |
| 127 | + int maximizeMoney(int N , int K) { |
| 128 | + // If there are no houses, the thief cannot rob any money |
| 129 | + if (N == 0) return 0; |
| 130 | +
|
| 131 | + // If there is only one house, the maximum money is the amount in that house |
| 132 | + if (N == 1) return K; |
| 133 | +
|
| 134 | + // For more than one house, calculate the maximum money the thief can rob |
| 135 | + // The thief can rob every alternate house, so the number of houses he can rob is ceil(N / 2) |
| 136 | + return (N + 1) / 2 * K; // This is equivalent to ceil(N / 2) * K |
| 137 | + } |
| 138 | +}; |
| 139 | +
|
| 140 | +``` |
| 141 | + |
| 142 | +## Problem Solution Explanation |
| 143 | + |
| 144 | +1. **`class Solution {`** |
| 145 | + - This defines the class `Solution`. The class encapsulates the method `maximizeMoney` that solves the problem. In C++, we define functions within a class to solve specific problems. |
| 146 | + |
| 147 | +2. **`public:`** |
| 148 | + - The `public` keyword indicates that the methods and variables following it are accessible from outside the class. The function `maximizeMoney` is public, so it can be called on an instance of the `Solution` class. |
| 149 | + |
| 150 | +3. **`int maximizeMoney(int N, int K) {`** |
| 151 | + - This is the definition of the function `maximizeMoney` which takes two integer arguments: |
| 152 | + - `N`: The number of houses. |
| 153 | + - `K`: The amount of money in each house. |
| 154 | + - The function returns an integer, which is the maximum amount of money the thief can rob. |
| 155 | + |
| 156 | +4. **`if (N == 0) return 0;`** |
| 157 | + - This is a base case check. If there are no houses (`N == 0`), then the maximum amount of money the thief can rob is `0`. In this case, the function immediately returns `0`. |
| 158 | + |
| 159 | + **Example**: |
| 160 | + - Input: `N = 0, K = 10` |
| 161 | + - Since there are no houses (`N == 0`), the thief can't rob anything, so the output is `0`. |
| 162 | + |
| 163 | +5. **`if (N == 1) return K;`** |
| 164 | + - This is another base case check. If there is only one house (`N == 1`), the thief has no choice but to rob that house, so the maximum money he can rob is `K`, which is the money in that house. |
| 165 | + |
| 166 | + **Example**: |
| 167 | + - Input: `N = 1, K = 10` |
| 168 | + - Since there is only one house, the thief robs it, and the maximum money he can rob is `10`. |
| 169 | + |
| 170 | +6. **`return (N + 1) / 2 * K;`** |
| 171 | + - This is the general case when `N` is greater than `1`. Here, the logic is based on the observation that the thief can rob every alternate house to maximize the money, as he can't rob two adjacent houses. |
| 172 | + - **Key idea**: If the number of houses is `N`, the number of houses the thief can rob is `ceil(N / 2)`. This is because the thief can rob the 1st, 3rd, 5th, and so on houses. |
| 173 | + |
| 174 | + - **Why `(N + 1) / 2`?**: |
| 175 | + - This is an integer division that calculates the ceiling of `N / 2` (i.e., the number of houses the thief can rob). |
| 176 | + - If `N` is even, `N / 2` gives the correct number of houses the thief can rob. |
| 177 | + - If `N` is odd, `(N + 1) / 2` ensures that the thief can rob the extra house in the sequence. |
| 178 | + |
| 179 | + - Finally, we multiply this result by `K`, as each house contains `K` money. |
| 180 | + |
| 181 | + **Example 1**: |
| 182 | + - Input: `N = 5, K = 10` |
| 183 | + - The thief can rob every alternate house (house 1, 3, 5). So the number of houses robbed is `ceil(5 / 2) = 3`. |
| 184 | + - The maximum money robbed is `3 * 10 = 30`. |
| 185 | + |
| 186 | + **Example 2**: |
| 187 | + - Input: `N = 2, K = 12` |
| 188 | + - The thief can only rob one of the two houses. Thus, the number of houses robbed is `ceil(2 / 2) = 1`. |
| 189 | + - The maximum money robbed is `1 * 12 = 12`. |
| 190 | + |
| 191 | + **Example 3**: |
| 192 | + - Input: `N = 6, K = 7` |
| 193 | + - The thief can rob alternate houses (houses 1, 3, 5). Thus, the number of houses robbed is `ceil(6 / 2) = 3`. |
| 194 | + - The maximum money robbed is `3 * 7 = 21`. |
| 195 | + |
| 196 | +### Example Walkthrough |
| 197 | + |
| 198 | +Let's go through a specific example to further explain the code. |
| 199 | + |
| 200 | +**Example**: |
| 201 | +``` |
| 202 | +Input: N = 5, K = 10 |
| 203 | +``` |
| 204 | + |
| 205 | +- **Step 1**: The first condition checks if `N == 0`, but `N = 5`, so it moves to the next condition. |
| 206 | +- **Step 2**: The second condition checks if `N == 1`, but `N = 5`, so it moves to the main calculation. |
| 207 | +- **Step 3**: The expression `(N + 1) / 2` calculates the number of houses the thief can rob: |
| 208 | + - `(5 + 1) / 2 = 6 / 2 = 3` |
| 209 | +- **Step 4**: The thief can rob 3 houses, and each house contains 10 money. |
| 210 | +- **Step 5**: Multiply the number of houses by the money in each house: `3 * 10 = 30`. |
| 211 | + |
| 212 | +Thus, the function returns `30`. |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +### Summary of Code Logic: |
| 217 | + |
| 218 | +- **Base cases**: |
| 219 | + - If there are no houses (`N == 0`), return `0`. |
| 220 | + - If there is only one house (`N == 1`), return the money in that house (`K`). |
| 221 | + |
| 222 | +- **General case**: |
| 223 | + - The thief can rob every alternate house. The number of houses the thief can rob is `ceil(N / 2)`. |
| 224 | + - The total money robbed is calculated by multiplying the number of houses by the money in each house (`K`). |
| 225 | + |
| 226 | +### Time and Space Complexity: |
| 227 | + |
| 228 | +- **Time Complexity**: |
| 229 | + - The code consists of simple arithmetic operations, so the time complexity is **O(1)** (constant time). |
| 230 | + |
| 231 | +- **Space Complexity**: |
| 232 | + - The space complexity is **O(1)** because the code only uses a constant amount of space (a few variables). |
| 233 | + |
| 234 | +This solution efficiently solves the problem in constant time and space. |
0 commit comments