From 192e80f66f9b5d99a09b38f9215eda2e410ab0f2 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 22 Nov 2025 17:18:51 +0900 Subject: [PATCH 1/5] best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/chjung99.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/chjung99.java diff --git a/best-time-to-buy-and-sell-stock/chjung99.java b/best-time-to-buy-and-sell-stock/chjung99.java new file mode 100644 index 0000000000..db64def6cd --- /dev/null +++ b/best-time-to-buy-and-sell-stock/chjung99.java @@ -0,0 +1,23 @@ +class Solution { + public int maxProfit(int[] prices) { + int N = prices.length; + int[] rangeMaxAfter = new int[N]; + int maxValue = prices[N-1]; + + for (int i = N-1; i >= 0; i--){ + if (maxValue < prices[i]){ + maxValue = prices[i]; + } + rangeMaxAfter[i] = maxValue; + } + + int answer = rangeMaxAfter[0] - prices[0]; + + for (int i = 0; i < N; i++){ + answer = Math.max(answer, rangeMaxAfter[i] - prices[i]); + } + + return answer; + } +} + From 987da6f598ff356b564354a4fdbd0ffa4360b490 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 22 Nov 2025 17:19:08 +0900 Subject: [PATCH 2/5] linked-list-cycle --- linked-list-cycle/chjung99.java | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 linked-list-cycle/chjung99.java diff --git a/linked-list-cycle/chjung99.java b/linked-list-cycle/chjung99.java new file mode 100644 index 0000000000..24ad14c820 --- /dev/null +++ b/linked-list-cycle/chjung99.java @@ -0,0 +1,37 @@ +import java.util.*; + +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + Set visit = new HashSet<>(); + public boolean hasCycle(ListNode head) { + return dfs(head, visit); + } + public boolean dfs(ListNode head, Set visit){ + Deque stack = new ArrayDeque<>(); + if (head != null){ + visit.add(head); + stack.push(head); + } + while (!stack.isEmpty()){ + ListNode cur = stack.pop(); + if (cur.next == null) continue; + if (visit.contains(cur.next)) { + return true; + } + visit.add(cur.next); + stack.push(cur.next); + } + return false; + } +} + From 0051f105028137311b773cee008d607daa71f192 Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 22 Nov 2025 17:19:47 +0900 Subject: [PATCH 3/5] maximum-depth-of-binary-tree --- maximum-depth-of-binary-tree/chjung99.java | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 maximum-depth-of-binary-tree/chjung99.java diff --git a/maximum-depth-of-binary-tree/chjung99.java b/maximum-depth-of-binary-tree/chjung99.java new file mode 100644 index 0000000000..0311c525dc --- /dev/null +++ b/maximum-depth-of-binary-tree/chjung99.java @@ -0,0 +1,51 @@ +import java.util.*; + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + class Data{ + TreeNode node; + int depth; + public Data(TreeNode node, int depth){ + this.node = node; + this.depth = depth; + } + } + public int maxDepth(TreeNode root) { + return bfs(root); + } + public int bfs(TreeNode root) { + int depth = 0; + Queue q = new ArrayDeque<>(); + if (root != null){ + q.add(new Data(root, 1)); + } + while (!q.isEmpty()){ + Data cur = q.poll(); + if (cur.depth > depth){ + depth = cur.depth; + } + if (cur.node.left != null) { + q.add(new Data(cur.node.left, cur.depth + 1)); + } + if (cur.node.right != null) { + q.add(new Data(cur.node.right, cur.depth + 1)); + } + } + return depth; + } +} + From 2dcb820c1d28509700dcde13575181c87bea0dea Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 22 Nov 2025 17:20:10 +0900 Subject: [PATCH 4/5] reverse-bits --- reverse-bits/chjung99.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 reverse-bits/chjung99.java diff --git a/reverse-bits/chjung99.java b/reverse-bits/chjung99.java new file mode 100644 index 0000000000..249bd1a796 --- /dev/null +++ b/reverse-bits/chjung99.java @@ -0,0 +1,21 @@ +class Solution { + public int reverseBits(int n) { + Integer[] rev = new Integer[32]; + for (int i = 0; i < 32; i++){ + rev[i] = 0; + } + int ret = 0; + int ptr = 0; + int fac = 1; + while (n != 0){ + rev[ptr++] = n % 2; + n = (int) (n/2); + } + for (int i = 0; i < 32; i++){ + ret += rev[31-i] * fac; + fac *= 2; + } + return ret; + } +} + From f5663dc0f3dd44c8310fc06fd91f28cb1daa517d Mon Sep 17 00:00:00 2001 From: chjung99 Date: Sat, 22 Nov 2025 17:20:33 +0900 Subject: [PATCH 5/5] valid-palindrome --- valid-palindrome/chjung99.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-palindrome/chjung99.java diff --git a/valid-palindrome/chjung99.java b/valid-palindrome/chjung99.java new file mode 100644 index 0000000000..cbedf94129 --- /dev/null +++ b/valid-palindrome/chjung99.java @@ -0,0 +1,19 @@ +class Solution { + public boolean isPalindrome(String s) { + String filterd = s.toLowerCase(); + StringBuilder filterdBuilder = new StringBuilder(); + + for (int i = 0; i < filterd.length(); i++){ + Character c = filterd.charAt(i); + if (Character.isLetterOrDigit(c)){ + filterdBuilder.append(c.toString()); + } + } + + String ori = filterdBuilder.toString(); + String rev = new StringBuilder(ori).reverse().toString(); + + return rev.equals(ori); + } +} +