Skip to content

Commit e8b6f28

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent 0a6a8d3 commit e8b6f28

File tree

3 files changed

+277
-0
lines changed

3 files changed

+277
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
## 545. Boundary of Binary Tree
2+
3+
### Question
4+
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes. (The values of the nodes may still be duplicates.)
5+
6+
Left boundary is defined as the path from root to the left-most node. Right boundary is defined as the path from root to the right-most node. If the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.
7+
8+
The left-most node is defined as a leaf node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.
9+
10+
The right-most node is also defined by the same way with left and right exchanged.
11+
12+
```
13+
Example 1
14+
15+
Input:
16+
1
17+
\
18+
2
19+
/ \
20+
3 4
21+
22+
Ouput:
23+
[1, 3, 4, 2]
24+
25+
Explanation:
26+
The root doesn't have left subtree, so the root itself is left boundary.
27+
The leaves are node 3 and 4.
28+
The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary.
29+
So order them in anti-clockwise without duplicates and we have [1,3,4,2].
30+
31+
32+
33+
Example 2
34+
35+
Input:
36+
____1_____
37+
/ \
38+
2 3
39+
/ \ /
40+
4 5 6
41+
/ \ / \
42+
7 8 9 10
43+
44+
Ouput:
45+
[1,2,4,7,8,9,10,6,3]
46+
47+
Explanation:
48+
The left boundary are node 1,2,4. (4 is the left-most node according to definition)
49+
The leaves are node 4,7,8,9,10.
50+
The right boundary are node 1,3,6,10. (10 is the right-most node).
51+
So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
52+
```
53+
54+
### Solution
55+
* Method 1: Loop find all left and right, dfs to find the leaves.
56+
```Java
57+
/**
58+
* Definition for a binary tree node.
59+
* public class TreeNode {
60+
* int val;
61+
* TreeNode left;
62+
* TreeNode right;
63+
* TreeNode(int x) { val = x; }
64+
* }
65+
*/
66+
class Solution {
67+
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
68+
// first get the left boundry.
69+
List<Integer> result = new ArrayList<>();
70+
TreeNode cur = null;
71+
if(root == null) return result;
72+
if(root.left == null && root.right == null){
73+
result.add(root.val);
74+
return result;
75+
}
76+
List<Integer> left = new ArrayList<>();
77+
cur = root.left;
78+
while(cur != null){
79+
left.add(cur.val);
80+
cur = cur.left != null ? cur.left: cur.right;
81+
}
82+
cur = root.right;
83+
List<Integer> right = new ArrayList<>();
84+
while(cur != null){
85+
right.add(cur.val);
86+
cur = cur.right != null ? cur.right: cur.left;
87+
}
88+
List<Integer> leaves = new ArrayList<>();
89+
dfs(root, leaves);
90+
result.add(root.val);
91+
result.addAll(left);
92+
for(int i = ((left.size() != 0 && left.get(left.size() - 1) == leaves.get(0)) ? 1: 0);
93+
i < (right.size() != 0 && leaves.get(leaves.size() - 1) == right.get(right.size() - 1) ? leaves.size() - 1: leaves.size());
94+
i++) result.add(leaves.get(i));
95+
for(int i = right.size() - 1; i >= 0; i--) result.add(right.get(i));
96+
return result;
97+
}
98+
private void dfs(TreeNode node, List<Integer> list){
99+
if(node.left == null && node.right == null){
100+
list.add(node.val);
101+
return;
102+
}
103+
if(node.left != null) dfs(node.left, list);
104+
if(node.right != null) dfs(node.right, list);
105+
}
106+
}
107+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
## 895. Maximum Frequency Stack
2+
3+
### Question
4+
Implement FreqStack, a class which simulates the operation of a stack-like data structure.
5+
6+
FreqStack has two functions:
7+
8+
* push(int x), which pushes an integer x onto the stack.
9+
* pop(), which removes and returns the most frequent element in the stack.
10+
* If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.
11+
12+
```
13+
Example 1:
14+
15+
Input:
16+
["FreqStack","push","push","push","push","push","push","pop","pop","pop","pop"],
17+
[[],[5],[7],[5],[7],[4],[5],[],[],[],[]]
18+
Output: [null,null,null,null,null,null,null,5,7,5,4]
19+
Explanation:
20+
After making six .push operations, the stack is [5,7,5,7,4,5] from bottom to top. Then:
21+
22+
pop() -> returns 5, as 5 is the most frequent.
23+
The stack becomes [5,7,5,7,4].
24+
25+
pop() -> returns 7, as 5 and 7 is the most frequent, but 7 is closest to the top.
26+
The stack becomes [5,7,5,4].
27+
28+
pop() -> returns 5.
29+
The stack becomes [5,7,4].
30+
31+
pop() -> returns 4.
32+
The stack becomes [5,7].
33+
```
34+
35+
36+
Note:
37+
1. Calls to FreqStack.push(int x) will be such that 0 <= x <= 10^9.
38+
2. It is guaranteed that FreqStack.pop() won't be called if the stack has zero elements.
39+
3. The total number of FreqStack.push calls will not exceed 10000 in a single test case.
40+
4. The total number of FreqStack.pop calls will not exceed 10000 in a single test case.
41+
5. The total number of FreqStack.push and FreqStack.pop calls will not exceed 150000 across all test cases.
42+
43+
### Solution
44+
* Method 1: Map + Deque
45+
* We have one map saving the value and frequency.
46+
* We have another map, key is appearance freqency and value is a deque.
47+
* We always maintain the max appearance number.
48+
* We can take the value out of the stack in the entry and update the maxCount. Meanwhile we update the value and frequency map.
49+
```Java
50+
class FreqStack {
51+
private int maxCount;
52+
private Map<Integer, Integer> freqMap; // k: val, v: freq
53+
private Map<Integer, Deque<Integer>> countMap; // k: freq, v: Stack of values that appears k key times.
54+
public FreqStack() {
55+
this.freqMap = new HashMap<>();
56+
this.countMap = new HashMap<>();
57+
}
58+
59+
public void push(int x) {
60+
freqMap.put(x, freqMap.getOrDefault(x, 0) + 1);
61+
int freq = freqMap.get(x);
62+
maxCount = Math.max(maxCount, freq);
63+
Deque<Integer> stack = countMap.getOrDefault(freq, new ArrayDeque<>());
64+
stack.push(x);
65+
countMap.put(freq, stack);
66+
}
67+
68+
public int pop() {
69+
int val = countMap.get(maxCount).pop();
70+
freqMap.put(val, freqMap.get(val) - 1);
71+
if(countMap.get(maxCount).size() == 0){
72+
countMap.remove(maxCount);
73+
maxCount--;
74+
}
75+
while(!countMap.containsKey(maxCount) && maxCount > 0){
76+
maxCount--;
77+
}
78+
return val;
79+
}
80+
}
81+
82+
/**
83+
* Your FreqStack object will be instantiated and called as such:
84+
* FreqStack obj = new FreqStack();
85+
* obj.push(x);
86+
* int param_2 = obj.pop();
87+
*/
88+
```
89+
90+
91+
### Reference
92+
1. [Java - intuition and thought process](https://leetcode.com/problems/maximum-frequency-stack/discuss/289916/Java-intuition-and-thought-process)

leetcode/909. Snakes and Ladders.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
## 909. Snakes and Ladders
2+
3+
### Question
4+
On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row. For example, for a 6 x 6 board, the numbers are written as follows:
5+
![Imgur](https://i.imgur.com/Ljq1eEe.png)
6+
7+
You start on square 1 of the board (which is always in the last row and first column). Each move, starting from square x, consists of the following:
8+
9+
* You choose a destination square S with number x+1, x+2, x+3, x+4, x+5, or x+6, provided this number is <= N*N.
10+
* (This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations, regardless of the size of the board.)
11+
* If S has a snake or ladder, you move to the destination of that snake or ladder. Otherwise, you move to S.
12+
13+
A board square on row r and column c has a "snake or ladder" if board[r][c] != -1. The destination of that snake or ladder is board[r][c].
14+
15+
Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving. (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at `3`, because you do not continue moving to `4`.)
16+
17+
Return the least number of moves required to reach square N*N. If it is not possible, return -1.
18+
19+
```
20+
Example 1:
21+
22+
Input: [
23+
[-1,-1,-1,-1,-1,-1],
24+
[-1,-1,-1,-1,-1,-1],
25+
[-1,-1,-1,-1,-1,-1],
26+
[-1,35,-1,-1,13,-1],
27+
[-1,-1,-1,-1,-1,-1],
28+
[-1,15,-1,-1,-1,-1]]
29+
Output: 4
30+
Explanation:
31+
At the beginning, you start at square 1 [at row 5, column 0].
32+
You decide to move to square 2, and must take the ladder to square 15.
33+
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
34+
You then decide to move to square 14, and must take the ladder to square 35.
35+
You then decide to move to square 36, ending the game.
36+
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.
37+
```
38+
39+
Note:
40+
1. 2 <= board.length = board[0].length <= 20
41+
2. board[i][j] is between 1 and N*N or is equal to -1.
42+
3. The board square with number 1 has no snake or ladder.
43+
4. The board square with number N*N has no snake or ladder.
44+
45+
### Solution
46+
* Method 1: bfs
47+
```Java
48+
class Solution {
49+
public int snakesAndLadders(int[][] board) {
50+
int len = board.length;
51+
if(len <= 1) return 0;
52+
boolean[] visited = new boolean[len * len + 1];
53+
Queue<Integer> q = new LinkedList<>();
54+
q.offer(1);
55+
visited[1] = true;
56+
int step = 0;
57+
while(!q.isEmpty()){
58+
int size = q.size();
59+
for(int k = 0; k < size; k++){
60+
int cur = q.poll();
61+
if(cur == len * len) return step;
62+
for(int d = 1; d <= 6; d++){
63+
int next = cur + d;
64+
int h = len - (next - 1) / len - 1; // line number
65+
int w = (len - 1 - h) % 2 == 0 ? (next - 1) % len: len - 1 - (next - 1) % len;
66+
if(w >= 0 && w < len && h >= 0 && h < len && !visited[next]){
67+
visited[next] = true;
68+
if(board[h][w] == -1) q.offer(next);
69+
else q.offer(board[h][w]);
70+
}
71+
}
72+
}
73+
step++;
74+
}
75+
return -1;
76+
}
77+
}
78+
```

0 commit comments

Comments
 (0)