Skip to content

Commit 2cf10f4

Browse files
committed
update
1 parent 4cd9bd3 commit 2cf10f4

File tree

8 files changed

+714
-521
lines changed

8 files changed

+714
-521
lines changed

README.md

+519-519
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public List<Integer> rightSideView(TreeNode root) {
18+
List<Integer> res = new ArrayList<>();
19+
20+
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
21+
22+
if (root != null) {
23+
queue.offer(root);
24+
}
25+
26+
while (!queue.isEmpty()) {
27+
int levelCount = queue.size();
28+
for (int i = 0; i < levelCount; i++) {
29+
TreeNode node = queue.poll();
30+
if (i == levelCount - 1) {
31+
res.add(node.val);
32+
}
33+
for (TreeNode child: new TreeNode[]{node.left, node.right}) {
34+
if (child != null) {
35+
queue.offer(child);
36+
}
37+
}
38+
}
39+
}
40+
return res;
41+
}
42+
}
43+
44+
// time O(n), due to traverse
45+
// space O(n)
46+
// using tree and bfs

[A]tree/[A]tree-bfs/199-binary-tree-right-side-view.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
2323
return res
2424

2525
# time O(n), due to traverse
26-
# space O(n), due to queue's size (tree diameter or last level)
26+
# space O(n)
2727
# using tree and bfs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode buildTree(int[] preorder, int[] inorder) {
18+
Map<Integer, Integer> inValToInIdx = new HashMap<>();
19+
for (int i = 0; i < inorder.length; i++) {
20+
inValToInIdx.put(inorder[i], i);
21+
}
22+
return build(preorder, inValToInIdx, 0, preorder.length - 1, 0, inorder.length - 1, preorder.length);
23+
}
24+
25+
public TreeNode build(int[] preorder, Map<Integer, Integer> inValToInIdx, int preLeft, int preRight, int inLeft, int inRight, int length) {
26+
if (length < 1) {
27+
return null;
28+
}
29+
TreeNode node = new TreeNode(preorder[preLeft]);
30+
Integer inIdx = inValToInIdx.get(preorder[preLeft]);
31+
int leftLength = inIdx - inLeft;
32+
int rightLength = length - leftLength - 1;
33+
node.left = build(preorder, inValToInIdx, preLeft + 1, preLeft + leftLength, inLeft, inIdx - 1, leftLength);
34+
node.right = build(preorder, inValToInIdx, preLeft + leftLength + 1, preRight, inIdx + 1, inRight, rightLength);
35+
return node;
36+
}
37+
}
38+
39+
// time O(n), due to O(1) find in hashmap but recursion n times
40+
// space O(n), due to hashmap or building tree or recursion
41+
// using tree and divide and conquer and re-build tree (top-down)
42+
/*
43+
preorder: first value is root
44+
inorder: every value before root is left subtree, after root is right subtree
45+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public String longestPalindrome(String s) {
3+
int resLen = 1;
4+
int resIdx = 0;
5+
6+
for (int i = 0; i < s.length(); i++) {
7+
int[] cur = valid(s, i, i);
8+
if (cur[1] > resLen) {
9+
resIdx = cur[0];
10+
resLen = cur[1];
11+
}
12+
cur = valid(s, i, i + 1);
13+
if (cur[1] > resLen) {
14+
resIdx = cur[0];
15+
resLen = cur[1];
16+
}
17+
}
18+
return s.substring(resIdx, resIdx + resLen);
19+
}
20+
21+
public int[] valid(String s, int left, int right) {
22+
int[] res = new int[]{left, 1};
23+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
24+
res[0] = left;
25+
res[1] = right - left + 1;
26+
left -= 1;
27+
right += 1;
28+
}
29+
return res;
30+
}
31+
}
32+
33+
// time O(n**2), traversal costs O(n), and each time need to using two pointers to expand
34+
// space O(1)
35+
// using array and two pointers opposite direction and expand type
36+
/*
37+
1. this is not optimal solution
38+
*/

[H]array/[H]array-two-pointers-opposite-direction/5-longest-palindromic-substring.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ def longestPalindrome(self, s: str) -> str:
3232

3333
# time O(n**2), traversal costs O(n), and each time need to using two pointers to expand
3434
# space O(1)
35-
# using array and two pointers opposite direction and expand type
35+
# using array and two pointers opposite direction and expand type
36+
'''
37+
1. this is not optimal solution
38+
'''
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public List<List<Integer>> subsets(int[] nums) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
dfs(res, nums, new ArrayList<>(), 0);
5+
return res;
6+
}
7+
8+
public void dfs(List<List<Integer>> res, int[] nums, List<Integer> path, int idx) {
9+
if (idx == nums.length) {
10+
res.add(new ArrayList<>(path));
11+
return;
12+
}
13+
14+
dfs(res, nums, path, idx + 1);
15+
16+
path.add(nums[idx]);
17+
dfs(res, nums, path, idx + 1);
18+
path.remove(path.size() - 1);
19+
return;
20+
}
21+
}
22+
23+
// time O(n*(2**n)), due to each element can take or not take (2**n), and n for copy list
24+
// space O(n), due to recursion stack, not counting output here
25+
// using dfs and backtracking and subset
26+
/*
27+
1. type: subset
28+
2. duplicate elements: no
29+
3. selectable repeatedly: no
30+
*/
31+
/*
32+
this approach focus on considering each element:
33+
for cur element,
34+
we don't take, then goto next element
35+
or we take, then goto next element
36+
till no more element to be considered, we record path
37+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int uniquePaths(int m, int n) {
3+
int[][] dp = new int[m][n];
4+
5+
for (int i = 0; i < m; i++) {
6+
for (int j = 0; j < n; j++) {
7+
if (i == 0 && j == 0) {
8+
dp[i][j] = 1;
9+
} else if (i == 0) {
10+
dp[i][j] = dp[i][j - 1];
11+
} else if (j == 0) {
12+
dp[i][j] = dp[i - 1][j];
13+
} else {
14+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
15+
}
16+
}
17+
}
18+
return dp[m - 1][n - 1];
19+
}
20+
}
21+
22+
// time O(nm), due to grids' size
23+
// space O(nm), due to list
24+
// using dynamic programming and 2D

0 commit comments

Comments
 (0)