temp push
This commit is contained in:
parent
b0c79e1a6f
commit
4cd0263710
3 changed files with 42 additions and 0 deletions
11
cediackermann/challenges/houseRobber/BUILD
Normal file
11
cediackermann/challenges/houseRobber/BUILD
Normal 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 = [
|
||||||
|
],
|
||||||
|
)
|
||||||
11
cediackermann/challenges/houseRobber/Main.java
Normal file
11
cediackermann/challenges/houseRobber/Main.java
Normal 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})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
20
cediackermann/challenges/houseRobber/Solution.java
Normal file
20
cediackermann/challenges/houseRobber/Solution.java
Normal 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];
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue