Add romanToInteger submitions
This commit is contained in:
parent
3caa17b54b
commit
07da3627fa
9 changed files with 123 additions and 12 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
package cediackermann.challenges.houseRobber;
|
package cediackermann.challenges.houseRobber;
|
||||||
|
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public int rob(int[] nums) {
|
public int rob(int[] nums) {
|
||||||
int n = nums.length;
|
int n = nums.length;
|
||||||
|
|
@ -12,9 +11,7 @@ class Solution {
|
||||||
loot[1] = Math.max(nums[0], nums[1]);
|
loot[1] = Math.max(nums[0], nums[1]);
|
||||||
for (int i = 2; i < n; i++) {
|
for (int i = 2; i < n; i++) {
|
||||||
loot[i] = Math.max(loot[i - 1], nums[i] + loot[i - 2]);
|
loot[i] = Math.max(loot[i - 1], nums[i] + loot[i - 2]);
|
||||||
System.out.println(loot[i]);
|
|
||||||
}
|
}
|
||||||
|
return loot[n - 1];
|
||||||
return loot[n - 1];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
java_binary(
|
java_binary(
|
||||||
name = "romanToInt",
|
name = "romanToInt",
|
||||||
main_class = "cediackermann.challenges.houseRobber.Main",
|
main_class = "cediackermann.challenges.romanToInt.Main",
|
||||||
srcs = glob(["*.java"]),
|
srcs = glob(["*.java"]),
|
||||||
deps = [
|
deps = [
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import java.util.Map;
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
private final Map<String, Integer> romanLetters = new HashMap<>();
|
private final Map<String, Integer> romanLetters = new HashMap<>();
|
||||||
public Solution(){
|
|
||||||
|
public Solution() {
|
||||||
romanLetters.put("I", 1);
|
romanLetters.put("I", 1);
|
||||||
romanLetters.put("V", 5);
|
romanLetters.put("V", 5);
|
||||||
romanLetters.put("X", 10);
|
romanLetters.put("X", 10);
|
||||||
|
|
@ -14,20 +15,23 @@ class Solution {
|
||||||
romanLetters.put("D", 500);
|
romanLetters.put("D", 500);
|
||||||
romanLetters.put("M", 1000);
|
romanLetters.put("M", 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int romanToInt(String s) {
|
public int romanToInt(String s) {
|
||||||
int solution = 0;
|
int solution = 0;
|
||||||
for (int i = 0; i < s.length(); i++) {
|
for (int i = 0; i < s.length(); i++) {
|
||||||
if (i + 1 <= s.length() - 1) {
|
if (i + 1 <= s.length() - 1) {
|
||||||
if (this.romanLetters.get(Character.toString(s.charAt(i))) < this.romanLetters.get(Character.toString(s.charAt(i + 1)))) {
|
if (this.romanLetters.get(Character.toString(s.charAt(i))) < this.romanLetters
|
||||||
solution += this.romanLetters.get(Character.toString(s.charAt(i+1))) - this.romanLetters.get(Character.toString(s.charAt(i)));
|
.get(Character.toString(s.charAt(i + 1)))) {
|
||||||
|
solution += this.romanLetters.get(Character.toString(s.charAt(i + 1)))
|
||||||
|
- this.romanLetters.get(Character.toString(s.charAt(i)));
|
||||||
i++;
|
i++;
|
||||||
} else{
|
} else {
|
||||||
solution += this.romanLetters.get(Character.toString(s.charAt(i)));
|
solution += this.romanLetters.get(Character.toString(s.charAt(i)));
|
||||||
}
|
}
|
||||||
} else{
|
} else {
|
||||||
solution += this.romanLetters.get(Character.toString(s.charAt(i)));
|
solution += this.romanLetters.get(Character.toString(s.charAt(i)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return solution;
|
return solution;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
11
cediackermann/challenges/romanToInt2/BUILD
Normal file
11
cediackermann/challenges/romanToInt2/BUILD
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
load("@rules_java//java:defs.bzl", "java_binary")
|
||||||
|
|
||||||
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
|
java_binary(
|
||||||
|
name = "romanToInt2",
|
||||||
|
main_class = "cediackermann.challenges.romanToInt2.Main",
|
||||||
|
srcs = glob(["*.java"]),
|
||||||
|
deps = [
|
||||||
|
],
|
||||||
|
)
|
||||||
10
cediackermann/challenges/romanToInt2/Main.java
Normal file
10
cediackermann/challenges/romanToInt2/Main.java
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cediackermann.challenges.romanToInt2;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution sol = new Solution();
|
||||||
|
System.out.println(
|
||||||
|
sol.romanToInt("MCMXCIV"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
31
cediackermann/challenges/romanToInt2/Solution.java
Normal file
31
cediackermann/challenges/romanToInt2/Solution.java
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
package cediackermann.challenges.romanToInt2;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private final Map<Character, Integer> romanLetters = new HashMap<>();
|
||||||
|
|
||||||
|
public Solution() {
|
||||||
|
romanLetters.put('I', 1);
|
||||||
|
romanLetters.put('V', 5);
|
||||||
|
romanLetters.put('X', 10);
|
||||||
|
romanLetters.put('L', 50);
|
||||||
|
romanLetters.put('C', 100);
|
||||||
|
romanLetters.put('D', 500);
|
||||||
|
romanLetters.put('M', 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int romanToInt(String s) {
|
||||||
|
int solution = 0;
|
||||||
|
for (int i = 0; i < s.length() - 1; i++) {
|
||||||
|
if (this.romanLetters.get(s.charAt(i)) < this.romanLetters.get(s.charAt(i + 1))) {
|
||||||
|
solution -= this.romanLetters.get(s.charAt(i));
|
||||||
|
} else {
|
||||||
|
solution += this.romanLetters.get(s.charAt(i));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return solution + romanLetters.get(s.charAt(s.length() - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
11
cediackermann/challenges/romanToInt3/BUILD
Normal file
11
cediackermann/challenges/romanToInt3/BUILD
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
load("@rules_java//java:defs.bzl", "java_binary")
|
||||||
|
|
||||||
|
package(default_visibility = ["//visibility:public"])
|
||||||
|
|
||||||
|
java_binary(
|
||||||
|
name = "romanToInt3",
|
||||||
|
main_class = "cediackermann.challenges.romanToInt3.Main",
|
||||||
|
srcs = glob(["*.java"]),
|
||||||
|
deps = [
|
||||||
|
],
|
||||||
|
)
|
||||||
10
cediackermann/challenges/romanToInt3/Main.java
Normal file
10
cediackermann/challenges/romanToInt3/Main.java
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
package cediackermann.challenges.romanToInt3;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution sol = new Solution();
|
||||||
|
System.out.println(
|
||||||
|
sol.romanToInt("MCMXCIV"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
37
cediackermann/challenges/romanToInt3/Solution.java
Normal file
37
cediackermann/challenges/romanToInt3/Solution.java
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cediackermann.challenges.romanToInt3;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private final Map<Character, Integer> romanLetters = new HashMap<>();
|
||||||
|
|
||||||
|
public Solution() {
|
||||||
|
romanLetters.put('I', 1);
|
||||||
|
romanLetters.put('V', 5);
|
||||||
|
romanLetters.put('X', 10);
|
||||||
|
romanLetters.put('L', 50);
|
||||||
|
romanLetters.put('C', 100);
|
||||||
|
romanLetters.put('D', 500);
|
||||||
|
romanLetters.put('M', 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int romanToInt(String s) {
|
||||||
|
int solution = 0;
|
||||||
|
for (int i = 0; i < s.length(); i++) {
|
||||||
|
if (i + 1 <= s.length() - 1) {
|
||||||
|
if (this.romanLetters.get(s.charAt(i)) < this.romanLetters
|
||||||
|
.get(s.charAt(i + 1))) {
|
||||||
|
solution += this.romanLetters.get(s.charAt(i + 1))
|
||||||
|
- this.romanLetters.get(s.charAt(i));
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
solution += this.romanLetters.get(s.charAt(i));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
solution += this.romanLetters.get(s.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return solution;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue