Skip to content

Commit d1c27a4

Browse files
committed
1120-1167-1181-1197-1214-1256-1257-1669 (8)
1 parent 3457031 commit d1c27a4

16 files changed

+513
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Solution1120 {
2+
private double max;
3+
4+
public double maximumAverageSubtree(TreeNode root) {
5+
max = 0;
6+
dfs(root);
7+
return max;
8+
}
9+
10+
// [总和, 个数]
11+
private int[] dfs(TreeNode node) {
12+
if (node == null) {
13+
return new int[]{0, 0};
14+
}
15+
int[] left = dfs(node.left);
16+
int[] right = dfs(node.right);
17+
// 5e3 * 1e5
18+
int sum = node.val + left[0] + right[0];
19+
int cnt = 1 + left[1] + right[1];
20+
max = Math.max(max, (double) sum / cnt);
21+
return new int[]{sum, cnt};
22+
}
23+
}
24+
/*
25+
$1120. 子树的最大平均值
26+
https://leetcode.cn/problems/maximum-average-subtree/
27+
28+
给你一棵二叉树的根节点 root,找出这棵树的 每一棵 子树的 平均值 中的 最大 值。
29+
子树是树中的任意节点和它的所有后代构成的集合。
30+
树的平均值是树中节点值的总和除以节点数。
31+
提示:
32+
树中的节点数介于 1 到 5000之间。
33+
每个节点的值介于 0 到 100000 之间。
34+
如果结果与标准答案的误差不超过 10^-5,那么该结果将被视为正确答案。
35+
36+
递归
37+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.PriorityQueue;
2+
3+
public class Solution1167 {
4+
public int connectSticks(int[] sticks) {
5+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6+
for (int x : sticks) {
7+
minHeap.add(x);
8+
}
9+
int res = 0;
10+
while (minHeap.size() > 1) {
11+
int cost = minHeap.remove() + minHeap.remove();
12+
res += cost;
13+
minHeap.add(cost);
14+
}
15+
return res;
16+
}
17+
}
18+
/*
19+
$1167. 连接棒材的最低费用
20+
https://leetcode.cn/problems/minimum-cost-to-connect-sticks/
21+
22+
你有一些长度为正整数的棍子。这些长度以数组 sticks 的形式给出, sticks[i] 是 第i个 木棍的长度。
23+
你可以通过支付 x + y 的成本将任意两个长度为 x 和 y 的棍子连接成一个棍子。你必须连接所有的棍子,直到剩下一个棍子。
24+
返回以这种方式将所有给定的棍子连接成一个棍子的 最小成本 。
25+
提示:
26+
1 <= sticks.length <= 10^4
27+
1 <= sticks[i] <= 10^4
28+
29+
贪心/堆
30+
*/
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
public class Solution1181 {
8+
public List<String> beforeAndAfterPuzzles(String[] phrases) {
9+
int n = phrases.length;
10+
String[][] sp = new String[n][2];
11+
for (int i = 0; i < n; i++) {
12+
String[] s = phrases[i].split(" ");
13+
sp[i] = new String[]{s[0], s[s.length - 1]};
14+
}
15+
Set<String> m = new HashSet<>();
16+
for (int i = 0; i < n; i++) {
17+
for (int j = 0; j < n; j++) {
18+
if (i == j) {
19+
continue;
20+
}
21+
if (sp[i][0].equals(sp[j][1])) {
22+
m.add(phrases[j] + phrases[i].substring(sp[i][0].length()));
23+
}
24+
}
25+
}
26+
List<String> ret = new ArrayList<>(m);
27+
Collections.sort(ret);
28+
return ret;
29+
}
30+
}
31+
/*
32+
$1181. 前后拼接
33+
https://leetcode.cn/problems/before-and-after-puzzle/
34+
35+
给你一个「短语」列表 phrases,请你帮忙按规则生成拼接后的「新短语」列表。
36+
「短语」(phrase)是仅由小写英文字母和空格组成的字符串。「短语」的开头和结尾都不会出现空格,「短语」中的空格不会连续出现。
37+
「前后拼接」(Before and After puzzles)是合并两个「短语」形成「新短语」的方法。我们规定拼接时,第一个短语的最后一个单词 和 第二个短语的第一个单词 必须相同。
38+
返回每两个「短语」 phrases[i] 和 phrases[j](i != j)进行「前后拼接」得到的「新短语」。
39+
注意,两个「短语」拼接时的顺序也很重要,我们需要同时考虑这两个「短语」。另外,同一个「短语」可以多次参与拼接,但「新短语」不能再参与拼接。
40+
请你按字典序排列并返回「新短语」列表,列表中的字符串应该是 不重复的 。
41+
提示:
42+
1 <= phrases.length <= 100
43+
1 <= phrases[i].length <= 100
44+
45+
哈希表 + 排序
46+
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.HashSet;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
import java.util.Set;
5+
6+
public class Solution1197 {
7+
private static final int[][] DIRECTIONS = {{-2, -1}, {-2, 1}, {2, -1}, {2, 1}, {-1, -2}, {-1, 2}, {1, -2}, {1, 2}};
8+
9+
public int minKnightMoves(int x, int y) {
10+
Queue<int[]> queue = new LinkedList<>();
11+
queue.add(new int[]{0, 0});
12+
Set<Integer> visited = new HashSet<>();
13+
visited.add(0);
14+
int step = 0;
15+
while (!queue.isEmpty()) {
16+
int size = queue.size();
17+
for (int i = 0; i < size; i++) {
18+
int[] cur = queue.remove();
19+
if (cur[0] == x && cur[1] == y) {
20+
return step;
21+
}
22+
23+
for (int[] dir : DIRECTIONS) {
24+
int nextM = cur[0] + dir[0];
25+
int nextN = cur[1] + dir[1];
26+
int key = nextM * 1000 + nextN;
27+
if (!visited.contains(key)) {
28+
visited.add(key);
29+
queue.add(new int[]{nextM, nextN});
30+
}
31+
}
32+
}
33+
step++;
34+
}
35+
return step;
36+
}
37+
}
38+
/*
39+
$1197. 进击的骑士
40+
https://leetcode.cn/problems/minimum-knight-moves/
41+
42+
一个坐标可以从 -infinity 延伸到 +infinity 的 无限大的 棋盘上,你的 骑士 驻扎在坐标为 [0, 0] 的方格里。
43+
骑士的走法和中国象棋中的马相似,走 “日” 字:即先向左(或右)走 1 格,再向上(或下)走 2 格;或先向左(或右)走 2 格,再向上(或下)走 1 格。
44+
每次移动,他都可以按图示八个方向之一前进。
45+
返回 骑士前去征服坐标为 [x, y] 的部落所需的最小移动次数 。本题确保答案是一定存在的。
46+
提示:
47+
-300 <= x, y <= 300
48+
0 <= |x| + |y| <= 300
49+
50+
BFS / 数学
51+
相似题目: 688. 骑士在棋盘上的概率
52+
https://leetcode.cn/problems/knight-probability-in-chessboard/
53+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1120Tests {
5+
private final Solution1120 solution1120 = new Solution1120();
6+
7+
@Test
8+
public void example1() {
9+
TreeNode root = TreeNode.buildTreeNode("[5,6,1]");
10+
double expected = 6.00000;
11+
Assertions.assertEquals(expected, solution1120.maximumAverageSubtree(root));
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1167Tests {
5+
private final Solution1167 solution1167 = new Solution1167();
6+
7+
@Test
8+
public void example1() {
9+
int[] sticks = {2, 4, 3};
10+
int expected = 14;
11+
Assertions.assertEquals(expected, solution1167.connectSticks(sticks));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] sticks = {1, 8, 3, 5};
17+
int expected = 30;
18+
Assertions.assertEquals(expected, solution1167.connectSticks(sticks));
19+
}
20+
21+
@Test
22+
public void example3() {
23+
int[] sticks = {5};
24+
int expected = 0;
25+
Assertions.assertEquals(expected, solution1167.connectSticks(sticks));
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
import java.util.List;
5+
6+
public class Solution1181Tests {
7+
private final Solution1181 solution1181 = new Solution1181();
8+
9+
@Test
10+
public void example1() {
11+
String[] phrases = {"writing code", "code rocks"};
12+
List<String> expected = List.of("writing code rocks");
13+
Assertions.assertEquals(expected, solution1181.beforeAndAfterPuzzles(phrases));
14+
}
15+
16+
@Test
17+
public void example2() {
18+
String[] phrases = {"mission statement",
19+
"a quick bite to eat",
20+
"a chip off the old block",
21+
"chocolate bar",
22+
"mission impossible",
23+
"a man on a mission",
24+
"block party",
25+
"eat my words",
26+
"bar of soap"};
27+
List<String> expected = List.of("a chip off the old block party",
28+
"a man on a mission impossible",
29+
"a man on a mission statement",
30+
"a quick bite to eat my words",
31+
"chocolate bar of soap");
32+
Assertions.assertEquals(expected, solution1181.beforeAndAfterPuzzles(phrases));
33+
}
34+
35+
@Test
36+
public void example3() {
37+
String[] phrases = {"a", "b", "a"};
38+
List<String> expected = List.of("a");
39+
Assertions.assertEquals(expected, solution1181.beforeAndAfterPuzzles(phrases));
40+
}
41+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution1197Tests {
5+
private final Solution1197 solution1197 = new Solution1197();
6+
7+
@Test
8+
public void example1() {
9+
int x = 2;
10+
int y = 1;
11+
int expected = 1;
12+
Assertions.assertEquals(expected, solution1197.minKnightMoves(x, y));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int x = 5;
18+
int y = 5;
19+
int expected = 4;
20+
Assertions.assertEquals(expected, solution1197.minKnightMoves(x, y));
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
public class Solution1214 {
5+
private int target;
6+
private Set<Integer> valSet;
7+
private boolean res;
8+
9+
public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
10+
this.target = target;
11+
valSet = new HashSet<>();
12+
res = false;
13+
14+
dfs1(root1);
15+
dfs2(root2);
16+
return res;
17+
}
18+
19+
private void dfs1(TreeNode node) {
20+
if (node == null) {
21+
return;
22+
}
23+
valSet.add(node.val);
24+
dfs1(node.left);
25+
dfs1(node.right);
26+
}
27+
28+
private void dfs2(TreeNode node) {
29+
if (node == null) {
30+
return;
31+
}
32+
if (valSet.contains(target - node.val)) {
33+
res = true;
34+
}
35+
dfs2(node.left);
36+
dfs2(node.right);
37+
}
38+
}
39+
/*
40+
$1214. 查找两棵二叉搜索树之和
41+
https://leetcode.cn/problems/two-sum-bsts/
42+
43+
给出两棵二叉搜索树的根节点 root1 和 root2 ,请你从两棵树中各找出一个节点,使得这两个节点的值之和等于目标值 Target。
44+
如果可以找到返回 True,否则返回 False。
45+
提示:
46+
每棵树上节点数在 [1, 5000] 范围内。
47+
-10^9 <= Node.val, target <= 10^9
48+
49+
DFS + HashSet 暴力匹配
50+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution1256 {
2+
public String encode(int num) {
3+
return Integer.toBinaryString(num + 1).substring(1);
4+
}
5+
}
6+
/*
7+
$1256. 加密数字
8+
https://leetcode.cn/problems/encode-number/
9+
10+
给你一个非负整数 num ,返回它的「加密字符串」。
11+
加密的过程是把一个整数用某个未知函数进行转化,你需要从下表推测出该转化函数:
12+
```
13+
n f(n)
14+
0 ""
15+
1 "0"
16+
2 "1"
17+
3 "00"
18+
4 "01"
19+
5 "10"
20+
6 "11"
21+
7 "000"
22+
```
23+
提示:
24+
0 <= num <= 10^9
25+
26+
规律便是 (num+1) 转换为二进制后去掉第一位即可
27+
*/

0 commit comments

Comments
 (0)