temp push

This commit is contained in:
ackermanncedi 2024-12-02 12:44:46 +00:00
parent b0c79e1a6f
commit 4cd0263710
3 changed files with 42 additions and 0 deletions

View file

@ -0,0 +1,11 @@
load("@rules_java//java:defs.bzl", "java_binary")
package(default_visibility = ["//visibility:public"])
java_binary(
name = "houseRobber",
main_class = "cediackermann.challenges.houseRobber.Main",
srcs = glob(["*.java"]),
deps = [
],
)

View file

@ -0,0 +1,11 @@
package cediackermann.challenges.houseRobber;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(
sol.rob(new int[]{2,7,9,3,1})
);
}
}

View file

@ -0,0 +1,20 @@
package cediackermann.challenges.houseRobber;
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n == 1) {
return nums[0];
}
int[] loot = new int[n];
loot[0] = nums[0];
loot[1] = Math.max(nums[0], nums[1]);
for (int i = 2; i < n; i++) {
loot[i] = Math.max(loot[i - 1], nums[i] + loot[i - 2]);
System.out.println(loot[i]);
}
return loot[n - 1];
}
}