We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aff2065 commit 3abac88Copy full SHA for 3abac88
10 - Mathematics Problems/02 - Maximum Money/main.cpp
@@ -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