From 7eae7901ea8bf0428b5d7806bda8dc118524a4e0 Mon Sep 17 00:00:00 2001 From: GangBean Date: Sun, 12 Jan 2025 07:53:12 +0900 Subject: [PATCH 1/2] feat: solve valid parantheses --- valid-parentheses/GangBean.java | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 valid-parentheses/GangBean.java diff --git a/valid-parentheses/GangBean.java b/valid-parentheses/GangBean.java new file mode 100644 index 000000000..02912c132 --- /dev/null +++ b/valid-parentheses/GangBean.java @@ -0,0 +1,38 @@ +class Solution { + public boolean isValid(String s) { + /** + 1. understanding + - given string contains character which has paired characters. + - validate all given characters in input string has each pair, and valid in order. + 2. strategy + - use stack, to save left brackets, and if you encounter right bracket, then pop from stack, and compare two characters are paired. + - if not paired on any right character, or stack is empty then return false. + - all characters are iterated and stack is not empty, then return false. + 3. complexity + - time: O(N), N is the length of input string s. + - space: O(N), for stack variable memory. + */ + Stack leftBracket = new Stack<>(); + for (char c : s.toCharArray()) { + if (c == '(' || c == '{' || c == '[') { + // when character is left bracket, then push to stack. + leftBracket.push(c); + } else if (c == ')' || c == '}' || c == ']') { + if (leftBracket.isEmpty()) return false; + char left = leftBracket.pop(); + if (isPair(left, c)) continue; + return false; + } else { + throw new RuntimeException(String.format("Not valid input character: %c in input %s", c, s)); + } + } + return leftBracket.isEmpty(); + } + + private boolean isPair(char left, char right) { + return (left == '(' && right == ')') + || (left == '{' && right == '}') + || (left == '[' && right == ']'); + } +} + From 4bd04aee4bf6442377e7c174accdf9a94562749e Mon Sep 17 00:00:00 2001 From: GangBean Date: Sun, 12 Jan 2025 09:07:31 +0900 Subject: [PATCH 2/2] feat: solve container with most water --- container-with-most-water/GangBean.java | 45 +++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 container-with-most-water/GangBean.java diff --git a/container-with-most-water/GangBean.java b/container-with-most-water/GangBean.java new file mode 100644 index 000000000..f1c6e9934 --- /dev/null +++ b/container-with-most-water/GangBean.java @@ -0,0 +1,45 @@ +class Solution { + public int maxArea(int[] height) { + /** + 1. understanding + - with two pair of line, each can contain "min(line1,line2) * (distance)" mount of water + - find maximum amount of water can contain + 2. strategy + - brute force + - for each pair of lines, calculate amount and update maximum amount. + - it can takes O(N), where N is the count of lines. + - N is 10^5 at most, so it can takes 10^10, which can takes about 10 seconds + - you need to swap out unnecessary calculation + - so, let's find a unnecessary calculation in this problem. + 3. complexity + - time: O(N) + - space: O(1) + */ + int l = 0; + int r = height.length - 1; + int maxAmount = amountOf(height, l, r); // Math.min(height[l], height[r], r-l); + while (l < r) { // O(N) + maxAmount = Math.max(maxAmount, amountOf(height, l, r)); + if (height[l] < height[r]) { + l++; + } else if (height[l] > height[r]) { + r--; + } else { + int nextLeftAmount = amountOf(height, l+1, r); + int nextRightAmount = amountOf(height, l, r-1); + if (nextLeftAmount < nextRightAmount) { + r--; + } else { + l++; + } + } + } + + return maxAmount; + } + + private int amountOf(int[] height, int left, int right) { + return (Math.min(height[left], height[right]) * (right - left)); + } +} +