From 25d018e5aaa3740901d987cbce0fa2cd4e61ece1 Mon Sep 17 00:00:00 2001 From: phoenix Date: Mon, 29 Jun 2026 12:56:15 -0400 Subject: [PATCH] TrappingRain and Streams --- .../TrappingRainWater.java | 49 +++++++++++++++++++ src/practice/streams/Streams.java | 46 +++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/codecademy/javaInterviewProblems/TrappingRainWater.java create mode 100644 src/practice/streams/Streams.java diff --git a/src/codecademy/javaInterviewProblems/TrappingRainWater.java b/src/codecademy/javaInterviewProblems/TrappingRainWater.java new file mode 100644 index 0000000..17bdc9a --- /dev/null +++ b/src/codecademy/javaInterviewProblems/TrappingRainWater.java @@ -0,0 +1,49 @@ +package codecademy.javaInterviewProblems; + +public class TrappingRainWater { + + static int naiveSolution(int[] heights) { + + int sum = 0; + int leftMax = 0, leftIndex = 0; + int rightMax = 0, rightIndex = heights.length - 1; + + for (int i = 0; i < heights.length; i++) { + int leftBound = 0; + int rightBound = 0; + + for (int j = 0; j <= i; j++) { + leftBound = Math.max(leftBound, heights[j]); + } + + for (int j = i; j < heights.length; j++) { + rightBound = Math.max(rightBound, heights[j]); + } + + sum += Math.min(leftBound, rightBound) - heights[i]; + } + + return sum; + } + + static void main(String[] args) { + int[] heights = {7, 0, 9, 5, 9}; + System.out.println(naiveSolution(heights)); + System.out.println(optimizedSolution(heights)); + } + + public static int optimizedSolution(int[] heights) { + int totalWater = 0, leftMax = 0, rightMax = 0; + int leftIndex = 0, rightIndex = heights.length - 1; + + while (leftIndex < rightIndex) { + leftMax = Math.max(leftMax, heights[leftIndex]); + rightMax = Math.max(rightMax, heights[rightIndex]); + totalWater += Math.min(leftMax, rightMax) - heights[leftIndex]; + if (leftMax <= rightMax) leftIndex++; + else rightIndex--; + } + + return totalWater; + } +} diff --git a/src/practice/streams/Streams.java b/src/practice/streams/Streams.java new file mode 100644 index 0000000..2d08de4 --- /dev/null +++ b/src/practice/streams/Streams.java @@ -0,0 +1,46 @@ +package practice.streams; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +public class Streams { + + + static Employee[] employees = { + new Employee(1, "John", 10000), + new Employee(2, "Jane", 12000), + new Employee(3, "Bob", 15000) + }; + + + static void main(String[] args) { + + Stream.of(employees).forEach(e -> System.out.println(e.name)); + + Stream.of(employees).filter(e -> e.salary > 10000).forEach(e -> System.out.println(e.id + " " + e.name + " " + e.salary)); + + List empList = Arrays.asList(employees); + System.out.println(empList.stream()); + + Stream.of(employees[0], employees[1], employees[2]); + Stream.Builder builder = Stream.builder(); + builder.accept(employees[0]); + builder.accept(employees[1]); + builder.accept(employees[2]); + builder.build().forEach(e -> System.out.println(e.id + " " + e.name + " " + e.salary)); + } + + static class Employee { + int id; + String name; + double salary; + + public Employee(int id, String name, double salary) { + this.id = id; + this.name = name; + this.salary = salary; + } + } + +}