diff --git a/Java/SolutionA.java b/Java/SolutionA.java new file mode 100644 index 0000000..41c7548 --- /dev/null +++ b/Java/SolutionA.java @@ -0,0 +1,19 @@ +package Java; + +public class SolutionA { + public int maxProfit(int[] prices) { + int finalSell = 0; + int finalBuy = Integer.MIN_VALUE; + int firstSell = 0; + int firstBuy = Integer.MIN_VALUE; + + for (int quote : prices) { + finalSell = Math.max(finalSell, finalBuy + quote); + finalBuy = Math.max(finalBuy, firstSell - quote); + firstSell = Math.max(firstSell, firstBuy + quote); + firstBuy = Math.max(firstBuy, -quote); + } + + return finalSell; + } +} \ No newline at end of file diff --git a/Java/SolutionB.java b/Java/SolutionB.java new file mode 100644 index 0000000..e687f7e --- /dev/null +++ b/Java/SolutionB.java @@ -0,0 +1,21 @@ +package Java; + +public class SolutionB { + public int minPatches(int[] nums, int n) { + int patches = 0; + long smallestUncovered = 1; + int cursor = 0; + + while (smallestUncovered <= n) { + if (cursor < nums.length && nums[cursor] <= smallestUncovered) { + smallestUncovered += nums[cursor]; + cursor++; + } else { + patches++; + smallestUncovered *= 2; + } + } + + return patches; + } +} \ No newline at end of file diff --git a/Java/SolutionD.java b/Java/SolutionD.java new file mode 100644 index 0000000..8abecfb --- /dev/null +++ b/Java/SolutionD.java @@ -0,0 +1,13 @@ +public class SolutionD { + public String shortestPalindrome(String s) { + String t = new StringBuilder(s).reverse().toString(); + + for (int i = 0; i < t.length(); i++) { + if (s.startsWith(t.substring(i))) { + return t.substring(0, i) + s; + } + } + + return t + s; + } +} \ No newline at end of file