aboutsummaryrefslogtreecommitdiff
path: root/challenge-325/deadmarshal/java
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-325/deadmarshal/java')
-rw-r--r--challenge-325/deadmarshal/java/Ch1.java20
-rw-r--r--challenge-325/deadmarshal/java/Ch2.java21
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-325/deadmarshal/java/Ch1.java b/challenge-325/deadmarshal/java/Ch1.java
new file mode 100644
index 0000000000..f1f27e332c
--- /dev/null
+++ b/challenge-325/deadmarshal/java/Ch1.java
@@ -0,0 +1,20 @@
+public class Ch1 {
+ public static void main(String[] args) {
+ System.out.println(consecutive_one(new int[]{0, 1, 1, 0, 1, 1, 1}));
+ System.out.println(consecutive_one(new int[]{0, 0, 0, 0}));
+ System.out.println(consecutive_one(new int[]{1, 0, 1, 0, 1, 1}));
+ }
+
+ private static int consecutive_one(int[] arr) {
+ int count = 0, res = 0;
+ for (int e : arr) {
+ if (e == 1) count++;
+ else {
+ res = Math.max(res, count);
+ count = 0;
+ }
+ }
+ return Math.max(count, res);
+ }
+}
+
diff --git a/challenge-325/deadmarshal/java/Ch2.java b/challenge-325/deadmarshal/java/Ch2.java
new file mode 100644
index 0000000000..353b6f6506
--- /dev/null
+++ b/challenge-325/deadmarshal/java/Ch2.java
@@ -0,0 +1,21 @@
+import java.util.Arrays;
+import java.util.Stack;
+
+public class Ch2 {
+ public static void main(String[] args) {
+ System.out.println(Arrays.toString(final_price(new int[]{8, 4, 6, 2, 3})));
+ System.out.println(Arrays.toString(final_price(new int[]{1, 2, 3, 4, 5})));
+ System.out.println(Arrays.toString(final_price(new int[]{7, 1, 1, 5})));
+ }
+
+ private static int[] final_price(int[] arr) {
+ Stack<Integer> stack = new Stack<>();
+ for (int i = 0; i < arr.length; ++i) {
+ while (!stack.isEmpty() && arr[stack.peek()] >= arr[i])
+ arr[stack.pop()] -= arr[i];
+ stack.push(i);
+ }
+ return arr;
+ }
+}
+