Skip to content

Commit 0a6a8d3

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

3 files changed

+251
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
## 317. Shortest Distance from All Buildings
2+
3+
### Question
4+
You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
5+
6+
* Each 0 marks an empty land which you can pass by freely.
7+
* Each 1 marks a building which you cannot pass through.
8+
* Each 2 marks an obstacle which you cannot pass through.
9+
10+
```
11+
Example:
12+
13+
Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
14+
15+
1 - 0 - 2 - 0 - 1
16+
| | | | |
17+
0 - 0 - 0 - 0 - 0
18+
| | | | |
19+
0 - 0 - 1 - 0 - 0
20+
21+
Output: 7
22+
23+
Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2),
24+
the point (1,2) is an ideal empty land to build a house, as the total
25+
travel distance of 3+3+1=7 is minimal. So return 7.
26+
```
27+
28+
Note:
29+
* There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
30+
31+
32+
33+
### Solutions
34+
* Method 1: BFS
35+
```Java
36+
class Solution {
37+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
38+
public int shortestDistance(int[][] grid) {
39+
Set<Integer> buildings = new HashSet<>();
40+
int height = grid.length, width = grid[0].length;
41+
for(int i = 0; i < grid.length; i++){
42+
for(int j = 0; j < grid[0].length; j++){
43+
if(grid[i][j] == 1) buildings.add(i * width + j);
44+
}
45+
}
46+
int min = Integer.MAX_VALUE;
47+
int count = buildings.size();
48+
for(int i = 0; i < height; i++){
49+
LABEL:
50+
for(int j = 0; j < width; j++){
51+
if(grid[i][j] != 0) continue;
52+
Queue<int[]> q = new LinkedList<>();
53+
q.offer(new int[]{i, j});
54+
Set<Integer> visited = new HashSet<>();
55+
int reach = 0;
56+
int step = 0;
57+
int temp = 0;
58+
Set<Integer> check = new HashSet<>(buildings);
59+
while(!q.isEmpty() && reach != count){
60+
int size = q.size();
61+
step++;
62+
for(int k = 0; k < size; k++){
63+
int[] cur = q.poll();
64+
if(check.contains(cur[0] * width + cur[1])){
65+
temp += step - 1;
66+
if(temp >= min) continue LABEL;
67+
check.remove(cur[0] * width + cur[1]);
68+
reach++;
69+
continue;
70+
}
71+
int tx = 0, ty = 0;
72+
for(int d = 0; d < 4; d++){
73+
tx = cur[0] + dir[d][0];
74+
ty = cur[1] + dir[d][1];
75+
if(tx >= 0 && tx < height && ty >= 0 && ty < width && !visited.contains(tx * width + ty) && grid[tx][ty] != 2){
76+
visited.add(tx * width + ty);
77+
q.offer(new int[]{tx, ty});
78+
}
79+
}
80+
}
81+
}
82+
if(reach == count) min = Math.min(min, temp);
83+
}
84+
}
85+
return min == Integer.MAX_VALUE ? -1: min;
86+
}
87+
}
88+
```
89+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
## 428. Serialize and Deserialize N-ary Tree
2+
3+
### Question
4+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
5+
6+
Design an algorithm to serialize and deserialize an N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that an N-ary tree can be serialized to a string and this string can be deserialized to the original tree structure.
7+
8+
For example, you may serialize the following 3-ary tree
9+
![Imgur](https://i.imgur.com/xjO7yXS.png)
10+
11+
as [1 [3[5 6] 2 4]]. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
12+
13+
Note:
14+
* N is in the range of [1, 1000]
15+
* Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
16+
17+
18+
### Solutions
19+
* Method 1: recursion
20+
```Java
21+
/*
22+
// Definition for a Node.
23+
class Node {
24+
public int val;
25+
public List<Node> children;
26+
27+
public Node() {}
28+
29+
public Node(int _val,List<Node> _children) {
30+
val = _val;
31+
children = _children;
32+
}
33+
};
34+
*/
35+
class Codec {
36+
private static final String split = ",";
37+
private static final String start = "[";
38+
private static final String end = "]";
39+
private static final String NULL = "#";
40+
// Encodes a tree to a single string.
41+
public String serialize(Node root) {
42+
StringBuilder sb = new StringBuilder();
43+
serialize(root, sb);
44+
return sb.toString();
45+
}
46+
private void serialize(Node node, StringBuilder sb){
47+
if(node == null){
48+
sb.append(NULL + split);
49+
return;
50+
}
51+
sb.append(node.val);
52+
if(node.children != null && node.children.size() > 0){
53+
sb.append(start);
54+
for(Node n : node.children){
55+
serialize(n, sb);
56+
}
57+
sb.append(end);
58+
}
59+
sb.append(split);
60+
}
61+
private int index = 0;
62+
private char[] arr;
63+
// Decodes your encoded data to tree.
64+
public Node deserialize(String data) {
65+
if(data.equals(NULL + split)) return null;
66+
this.arr = data.toCharArray();
67+
return deserialize().get(0);
68+
}
69+
private List<Node> deserialize(){
70+
Node node = null;
71+
List<Node> list = new ArrayList<>();
72+
while(index < arr.length){
73+
char c = arr[index];
74+
if(Character.isDigit(c)){
75+
int num = 0;
76+
while(index < arr.length && Character.isDigit(arr[index])){
77+
num = num * 10 + arr[index++] - '0';
78+
}
79+
index--;
80+
node = new Node(num, new ArrayList<>());
81+
list.add(node);
82+
}else if(("" + c).equals(start)){
83+
index++;
84+
node.children = deserialize();
85+
}else if(("" + c).equals(end)){
86+
index++;
87+
return list;
88+
}
89+
index++;
90+
}
91+
return list;
92+
}
93+
}
94+
95+
// Your Codec object will be instantiated and called as such:
96+
// Codec codec = new Codec();
97+
// codec.deserialize(codec.serialize(root));
98+
```
99+

leetcode/767. Reorganize String.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## 767. Reorganize String
2+
3+
### Question
4+
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the same.
5+
6+
If possible, output any possible result. If not possible, return the empty string.
7+
8+
```
9+
Example 1:
10+
11+
Input: S = "aab"
12+
Output: "aba"
13+
14+
Example 2:
15+
16+
Input: S = "aaab"
17+
Output: ""
18+
```
19+
20+
Note:
21+
* S will consist of lowercase letters and have length in range [1, 500].
22+
23+
### Solutions
24+
* Method 1: PriorityQueue
25+
```Java
26+
class Solution {
27+
private static final class Node{
28+
char c;
29+
int f;
30+
public Node(char c, int f){
31+
this.c = c;
32+
this.f = f;
33+
}
34+
}
35+
public String reorganizeString(String S) {
36+
char[] arr = S.toCharArray();
37+
int[] table = new int[26];
38+
for(char c : arr){
39+
if(++table[c - 'a'] > (S.length() + 1)/ 2) return "";
40+
}
41+
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> {
42+
return b.f - a.f;
43+
});
44+
for(int i = 0; i < 26; i++){
45+
if(table[i] > 0){
46+
pq.offer(new Node((char)(i + 'a'), table[i]));
47+
}
48+
}
49+
StringBuilder sb = new StringBuilder();
50+
while(pq.size() >= 2){
51+
Node first = pq.poll();
52+
Node second = pq.poll();
53+
sb.append(first.c);
54+
sb.append(second.c);
55+
if(--first.f > 0) pq.offer(first);
56+
if(--second.f > 0) pq.offer(second);
57+
}
58+
if(!pq.isEmpty()) sb.append(pq.poll().c);
59+
return sb.toString();
60+
}
61+
}
62+
```
63+

0 commit comments

Comments
 (0)