Skip to content

Commit 3abac88

Browse files
authored
Create main.cpp
1 parent aff2065 commit 3abac88

File tree

1 file changed

+15
-0
lines changed
  • 10 - Mathematics Problems/02 - Maximum Money

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
// Function to calculate the maximum money the thief can rob
4+
int maximizeMoney(int N , int K) {
5+
// If there are no houses, the thief cannot rob any money
6+
if (N == 0) return 0;
7+
8+
// If there is only one house, the maximum money is the amount in that house
9+
if (N == 1) return K;
10+
11+
// For more than one house, calculate the maximum money the thief can rob
12+
// The thief can rob every alternate house, so the number of houses he can rob is ceil(N / 2)
13+
return (N + 1) / 2 * K; // This is equivalent to ceil(N / 2) * K
14+
}
15+
};

0 commit comments

Comments
 (0)