Skip to content

Commit efca6c2

Browse files
authored
Merge pull request #969 from GangBean/main
[GangBean] Week 8
2 parents 992376d + fcb00e0 commit efca6c2

File tree

9 files changed

+282
-0
lines changed

9 files changed

+282
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class WordDictionary {
2+
private Map<Character, WordDictionary> children;
3+
private boolean isEnd;
4+
5+
public WordDictionary() {
6+
this.children = new HashMap<>();
7+
this.isEnd = false;
8+
}
9+
10+
public void addWord(String word) {
11+
char[] arr = word.toCharArray();
12+
WordDictionary next = this;
13+
for (int i = 0; i < arr.length; i++) {
14+
char c = arr[i];
15+
next.children.putIfAbsent(c, new WordDictionary());
16+
next = next.children.get(c);
17+
}
18+
next.isEnd = true;
19+
}
20+
21+
public boolean search(String word) {
22+
// System.out.println(this);
23+
return search(word, 0);
24+
}
25+
26+
private boolean search(String word, int idx) {
27+
if (idx == word.length()) return this.isEnd;
28+
char c = word.charAt(idx);
29+
if (c == '.') {
30+
return this.children.values().stream().anyMatch(child -> child.search(word, idx+1));
31+
}
32+
if (!this.children.containsKey(c)) return false;
33+
return this.children.get(c).search(word, idx+1);
34+
}
35+
36+
public String toString() {
37+
return String.format("%s -> %s", isEnd, this.children);
38+
}
39+
}
40+
41+
/**
42+
* Your WordDictionary object will be instantiated and called as such:
43+
* WordDictionary obj = new WordDictionary();
44+
* obj.addWord(word);
45+
* boolean param_2 = obj.search(word);
46+
*/
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
/**
3+
1. understanding
4+
- dp[n] : length of longest increasing subsequence in 0...n
5+
2. space
6+
- time: O(N^2)
7+
- space: O(N)
8+
*/
9+
public int lengthOfLIS(int[] nums) {
10+
int[] dp = new int[nums.length];
11+
Arrays.fill(dp, 1);
12+
for (int i = 0; i < nums.length; i++) { // O(N)
13+
for (int j = 0; j <= i; j++) { // O(N)
14+
if (nums[j] < nums[i]) {
15+
dp[i] = Math.max(dp[j]+1, dp[i]);
16+
}
17+
}
18+
}
19+
return Arrays.stream(dp).max().orElse(1);
20+
}
21+
}
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
/**
3+
1. understanding
4+
- [a] -> [a,b] -> [a,b,c] -> [a] -> [a,b] -> [a,b,c] -> [b] -> [b]
5+
2. complexity
6+
- time: O(N)
7+
- space: O(N)
8+
*/
9+
public int lengthOfLongestSubstring(String s) {
10+
int left = 0;
11+
int right = 0;
12+
int len = s.length();
13+
HashSet charSet = new HashSet();
14+
int ret = (len==0)?0:1;
15+
while (left<len && right<len) {
16+
if (left == right) {
17+
right++;
18+
charSet = new HashSet();
19+
charSet.add(s.charAt(left));
20+
} else {
21+
if (!charSet.contains(s.charAt(right))) {
22+
charSet.add(s.charAt(right));
23+
int tmpLen = right - left + 1;
24+
if (tmpLen > ret) {
25+
ret = tmpLen;
26+
}
27+
right++;
28+
} else {
29+
left++;
30+
right = left;
31+
}
32+
}
33+
}
34+
35+
return ret;
36+
}
37+
}
38+

number-of-1-bits/GangBean.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
int count = 0;
4+
while (n >= 1) {
5+
count += n % 2;
6+
n /= 2;
7+
}
8+
return count;
9+
}
10+
}
11+

number-of-islands/GangBean.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
/**
3+
1. understanding
4+
- for each grid cell, if it is land, then move horizontally and vertically, to find connecting lands
5+
2. complexity
6+
- time: O(N * M) where grid is N * M matrix
7+
- space: O(1)
8+
*/
9+
public int numIslands(char[][] grid) {
10+
int ret = 0;
11+
for (int i=0; i<grid.length; i++) {
12+
for (int j=0; j<grid[0].length; j++) {
13+
if (grid[i][j] == '1') {
14+
findIsland(grid,i,j,'.');
15+
ret++;
16+
}
17+
}
18+
}
19+
return ret;
20+
}
21+
22+
private void findIsland(char[][] grid, int i, int j, char color) {
23+
if (grid[i][j] == '1') {
24+
grid[i][j] = color;
25+
}
26+
int[] dx = {0,1,0,-1};
27+
int[] dy = {1,0,-1,0};
28+
29+
for (int dir=0; dir<4; dir++) {
30+
int nx = i + dx[dir];
31+
int ny = j + dy[dir];
32+
if (0<= nx&&nx<grid.length && 0<=ny&&ny<grid[0].length && grid[nx][ny]=='1') {
33+
findIsland(grid,nx,ny,color);
34+
}
35+
}
36+
}
37+
}
38+

reverse-linked-list/GangBean.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
ListNode curr = head;
14+
ListNode prev = null;
15+
while (curr != null) {
16+
ListNode next = curr.next;
17+
curr.next = prev;
18+
prev = curr;
19+
curr = next;
20+
}
21+
22+
return prev;
23+
}
24+
}
25+

set-matrix-zeroes/GangBean.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
/**
3+
1. understanding
4+
- iterate over all cells, if value is 0, then add row and col to sweep target set.
5+
- for each row target set, and col target set, sweep all it's value to 0
6+
2. complexity
7+
- time: O(m * n)
8+
- space: O(m + n)
9+
*/
10+
public void setZeroes(int[][] matrix) {
11+
Set<Integer> rows = new HashSet<>(); // O(m)
12+
Set<Integer> cols = new HashSet<>(); // O(n)
13+
14+
for (int row = 0; row < matrix.length; row++) { // O(m)
15+
for (int col = 0; col < matrix[row].length; col++) { // O(n)
16+
if (matrix[row][col] == 0) { // O(m * n)
17+
rows.add(row);
18+
cols.add(col);
19+
}
20+
}
21+
}
22+
23+
for (int row: rows) { // O(m)
24+
int col = 0;
25+
while (col < matrix[row].length) { // O(n)
26+
matrix[row][col] = 0;
27+
col++;
28+
}
29+
}
30+
31+
for (int col: cols) { // O(n)
32+
int row = 0;
33+
while (row < matrix.length) { // O(m)
34+
matrix[row][col] = 0;
35+
row++;
36+
}
37+
}
38+
}
39+
}
40+

spiral-matrix/GangBean.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
/**
3+
1. understanding
4+
- 3 x 3 = N x M
5+
- (1,2,3) -> (6,9) // (8,7) -> (4) // (5)
6+
- upper: step M count, N -= 1 // right: step N count, M -= 1 // bottom: step M count, N -= 1 // left: step N count, M -= 1
7+
2. complexity:
8+
- time: O(N * M)
9+
- space: O(1)
10+
*/
11+
public List<Integer> spiralOrder(int[][] matrix) {
12+
int r = 0;
13+
int c = -1;
14+
int dir = 1;
15+
int N = matrix.length;
16+
int M = matrix[0].length;
17+
List<Integer> ret = new ArrayList<>();
18+
19+
while (0 < N && 0 < M) {
20+
for (int i = 0; i < M; i++) {
21+
c += dir;
22+
ret.add(matrix[r][c]);
23+
}
24+
N -= 1;
25+
for (int i = 0; i < N; i++) {
26+
r += dir;
27+
ret.add(matrix[r][c]);
28+
}
29+
M -= 1;
30+
31+
dir *= -1;
32+
}
33+
34+
return ret;
35+
}
36+
}
37+

unique-paths/GangBean.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.math.BigDecimal;
2+
3+
class Solution {
4+
/**
5+
1. understanding
6+
- To reach destination, you have to move bottom direction in m-1 times, and move to right direction in n-1 times.
7+
- in example 2, [(DDR), (DRD), (RDD)] is the paths.
8+
- so, the number of paths are combinations of (m-1) D and (n-1) R
9+
- (m+n-2)!/(m-1)!(n-1)!, where ! means factorial, n! = 1*2*...*n
10+
- factorial[n]: n!
11+
2. complexity
12+
- time: O(m+n)
13+
- space: O(m+n)
14+
*/
15+
public int uniquePaths(int m, int n) {
16+
BigDecimal[] dp = new BigDecimal[m+n];
17+
Arrays.fill(dp, BigDecimal.ONE);
18+
for (int num = 2; num < m+n; num++) {
19+
dp[num] = dp[num-1].multiply(new BigDecimal(num));
20+
}
21+
return dp[m+n-2].divide(dp[m-1]).divide(dp[n-1]).intValue();
22+
}
23+
}
24+

0 commit comments

Comments
 (0)