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; + } +} + 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; + } +} + 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; + } +} + 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; + } +} + 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); + } +} +