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
51 changes: 51 additions & 0 deletions design-add-and-search-words-data-structure/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class WordDictionary {
Node rootNode;
public WordDictionary() {
this.rootNode = new Node();
}

public void addWord(String word) {
Node node = rootNode;

for (int i = 0; i < word.length(); i++) {
node = node.childNode.computeIfAbsent(word.charAt(i), key -> new Node());
}
node.isEndOfWord = true;
}

public boolean search(String word) {
return dfs(rootNode, word, 0);
}

public boolean dfs(Node node, String word, int depth) {
if (depth == word.length()) return node.isEndOfWord;
if (word.charAt(depth) == '.') {
for (Node nextNode: node.childNode.values()) {
if (dfs(nextNode, word, depth + 1)) return true;
}
return false;
} else {
Node nextNode = node.childNode.getOrDefault(word.charAt(depth), null);
if (nextNode == null) return false;
return dfs(nextNode, word, depth+1);
}
}

static class Node {
Map<Character, Node> childNode;
boolean isEndOfWord;

public Node() {
this.childNode = new HashMap<>();
this.isEndOfWord = false;
}
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/

30 changes: 30 additions & 0 deletions house-robber-ii/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public int rob(int[] nums) {
int n = nums.length;
if (n==1) {
return nums[0];
}
return Math.max(
robLinear(Arrays.copyOfRange(nums, 1, n)),
robLinear(Arrays.copyOfRange(nums, 0, n-1))
);
}

public int robLinear(int[] nums) {
int n = nums.length;
int[] dp = new int[n];

dp[0] = nums[0];
if (n > 1) {
dp[1] = Math.max(nums[0], nums[1]);
}

for (int i = 2; i < n; i++) {
dp[i] = Math.max(dp[i-2] + nums[i], dp[i-1]);
}

return dp[n-1];
}
}


17 changes: 17 additions & 0 deletions house-robber/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int rob(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
dp[0] = nums[0];
if (n >= 2) {
dp[1] = Math.max(nums[0], nums[1]);
}

for (int i=2; i<n; i++){
dp[i] = Math.max(dp[i-2]+nums[i], dp[i-1]);
}
return dp[n-1];
}
}


57 changes: 57 additions & 0 deletions implement-trie-prefix-tree/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Trie {
Node rootNode = new Node();

public Trie() {

}

public void insert(String word) {
Node node = this.rootNode;

for (int i = 0; i < word.length(); i++) {
node = node.childNode.computeIfAbsent(word.charAt(i), key -> new Node());
}
node.isEndOfWord = true;
}

public boolean search(String word) {
Node node = this.rootNode;

for (int i = 0; i < word.length(); i++) {
node = node.childNode.getOrDefault(word.charAt(i), null);
if (node == null) return false;
}
return node.isEndOfWord;
}

public boolean startsWith(String prefix) {
Node node = this.rootNode;

for (int i = 0; i < prefix.length(); i++) {
node = node.childNode.getOrDefault(prefix.charAt(i), null);
if (node == null) return false;
}
return true;
}

static class Node {
Map<Character, Node> childNode = new HashMap<>();
boolean isEndOfWord = false;

public Node(){

}
}
}

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/




56 changes: 56 additions & 0 deletions number-of-islands/chjung99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Solution {
int n;
int m;
int[] dx = new int[]{1, 0, -1, 0};
int[] dy = new int[]{0, 1, 0, -1};

public int numIslands(char[][] grid) {
n = grid.length;
m = grid[0].length;
boolean[][] visit = new boolean[n][m];
int cnt = 0;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++){
if (!visit[i][j] && grid[i][j] == '1') {
cnt ++;
bfs(i, j, grid, visit);
}
}
}
return cnt;
}

public void bfs(int cx, int cy, char[][] grid, boolean[][] visit) {
Deque<Point> deque = new ArrayDeque<>();
visit[cx][cy] = true;
deque.add(new Point(cx, cy));

while (!deque.isEmpty()) {
Point cur = deque.poll();
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];

if (outOfRange(nx, ny) || visit[nx][ny] || grid[nx][ny] == '0') continue;
deque.add(new Point(nx,ny));
visit[nx][ny] = true;
}
}
}

public boolean outOfRange(int x, int y) {
return ((x < 0 || x >= n) || (y < 0 || y >= m));
}

static class Point{
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
}