Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions best-time-to-buy-and-sell-stock/chjung99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

37 changes: 37 additions & 0 deletions linked-list-cycle/chjung99.java
Original file line number Diff line number Diff line change
@@ -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<ListNode> visit = new HashSet<>();
public boolean hasCycle(ListNode head) {
return dfs(head, visit);
}
public boolean dfs(ListNode head, Set<ListNode> visit){
Deque<ListNode> 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;
}
}

51 changes: 51 additions & 0 deletions maximum-depth-of-binary-tree/chjung99.java
Original file line number Diff line number Diff line change
@@ -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<Data> 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;
}
}

21 changes: 21 additions & 0 deletions reverse-bits/chjung99.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

19 changes: 19 additions & 0 deletions valid-palindrome/chjung99.java
Original file line number Diff line number Diff line change
@@ -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);
}
}