1387. Sort Integers by The Power Value
The power of an integer x
is defined as the number of steps needed to transform x
into 1
using the following steps:
- if
x
is even thenx = x / 2
- if
x
is odd thenx = 3 * x + 1
For example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).
Given three integers lo
, hi
and k
. The task is to sort all integers in the interval [lo, hi]
by the power value in ascending order, if two or more integers have the same power value sort them by ascending order.
Return the k-th
integer in the range [lo, hi]
sorted by the power value.
Notice that for any integer x
(lo <= x <= hi)
it is guaranteed that x
will transform into 1
using these steps and that the power of x
is will fit in 32 bit signed integer.
Example 1:
Input: lo = 12, hi = 15, k = 2 Output: 13 Explanation: The power of 12 is 9 (12 --> 6 --> 3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1) The power of 13 is 9 The power of 14 is 17 The power of 15 is 17 The interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13. Notice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.
Constraints:
1 <= lo <= hi <= 1000
1 <= k <= hi - lo + 1
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public int getKth(int lo, int hi, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
int ap = getPower(a);
int bp = getPower(b);
return (ap == bp) ? a - b : ap - bp;
}
});
while (lo <= hi) {
pq.add(lo++);
}
while (k-- > 1) {
pq.poll();
}
return pq.peek();
}
public int getPower(Integer num) {
if (num == 1)
return 1;
if (map.containsKey(num))
return map.get(num);
int power = 1 + ((num & 1) == 1 ? getPower(num * 3 + 1) : getPower(num >> 1));
map.put(num, power);
return power;
}
}
https://blog.csdn.net/ted_cs/article/details/82713706
1161. Maximum Level Sum of a Binary Tree
Given the root
of a binary tree, the level of its root is 1
, the level of its children is 2
, and so on.
Return the smallest level X
such that the sum of all the values of nodes at level X
is maximal.
Example 1:
Input: [1,7,0,7,-8,null,null] Output: 2 Explanation: Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return the level with the maximum sum which is level 2.
Note:
- The number of nodes in the given tree is between
1
and10^4
. -10^5 <= node.val <= 10^5
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxLevelSum(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int cursum = 0;
int max = cursum;
int level = 0;
int minlevel = 0;
while (!q.isEmpty()) {
int levelnode = q.size(); //每层的结点个数
level++;
cursum = 0;
for (int i = 0; i < levelnode; i++) {
TreeNode curNode = q.poll();
cursum += curNode.val;
if (curNode.left != null) {
q.offer(curNode.left);
}
if (curNode.right != null) {
q.offer(curNode.right);
}
}
if (max < cursum) {
max = cursum;
minlevel = level;
}
}
return minlevel;
}
}
1043. Partition Array for Maximum Sum
Given an integer array A
, you partition the array into (contiguous) subarrays of length at most K
. After partitioning, each subarray has their values changed to become the maximum value of that subarray.
Return the largest sum of the given array after partitioning.
Example 1:
Input: A = [1,15,7,9,2,5,10], K = 3 Output: 84 Explanation: A becomes [15,15,15,9,10,10,10]
Note:
1 <= K <= A.length <= 500
0 <= A[i] <= 10^6
class Solution {
public int maxSumAfterPartitioning(int[] A, int K) {
int[] dp = new int[A.length + 1];
for (int i = 1; i <= A.length; i++) {
int cur = A[i - 1];
for (int j = i - 1, k = 1; j >= 0 && k <= K; j--, k++) {
cur = Math.max(cur, A[j]);
dp[i] = Math.max(dp[i], dp[j] + cur * k);
}
}
return dp[A.length];
}
}
841. Keys and Rooms
There are N
rooms and you start in room 0
. Each room has a distinct number in 0, 1, 2, ..., N-1
, and each room may have some keys to access the next room.
Formally, each room i
has a list of keys rooms[i]
, and each key rooms[i][j]
is an integer in [0, 1, ..., N-1]
where N = rooms.length
. A key rooms[i][j] = v
opens the room with number v
.
Initially, all the rooms start locked (except for room 0
).
You can walk back and forth between rooms freely.
Return true
if and only if you can enter every room.
Example 1:
Input: [[1],[2],[3],[]] Output: true Explanation: We start in room 0, and pick up key 1. We then go to room 1, and pick up key 2. We then go to room 2, and pick up key 3. We then go to room 3. Since we were able to go to every room, we return true.
Example 2:
Input: [[1,3],[3,0,1],[2],[0]] Output: false Explanation: We can't enter the room with number 2.
Note:
1 <= rooms.length <= 1000
0 <= rooms[i].length <= 1000
- The number of keys in all rooms combined is at most
3000
.
DFS
class Solution {
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
Set<Integer> visited = new HashSet<>();
dfs(rooms, visited, 0);
return visited.size() == rooms.size();
}
public void dfs(List<List<Integer>> rooms, Set<Integer> visited, int room) {
if (visited.contains(room))
return;
visited.add(room);
for (int r : rooms.get(room)) {
dfs(rooms, visited, r);
}
}
}
1306. Jump Game III
Given an array of non-negative integers arr
, you are initially positioned at start
index of the array. When you are at index i
, you can jump to i + arr[i]
or i - arr[i]
, check if you can reach to any index with value 0.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [4,2,3,0,3,1,2], start = 5 Output: true Explanation: All possible ways to reach at index 3 with value 0 are: index 5 -> index 4 -> index 1 -> index 3 index 5 -> index 6 -> index 4 -> index 1 -> index 3
Example 2:
Input: arr = [4,2,3,0,3,1,2], start = 0 Output: true Explanation: One possible way to reach at index 3 with value 0 is: index 0 -> index 4 -> index 1 -> index 3
Constraints:
1 <= arr.length <= 5 * 10^4
0 <= arr[i] < arr.length
0 <= start < arr.length
DFS:
class Solution {
public boolean canReach(int[] arr, int start) {
boolean[] visited = new boolean[arr.length];
return dfs(arr, visited, start);
}
public boolean dfs(int[] arr, boolean[] visited, int cur) {
if (cur < 0 || cur >= arr.length || visited[cur]) {
return false;
}
if (arr[cur] == 0)
return true;
visited[cur] = true;
return dfs(arr, visited, cur + arr[cur]) || dfs(arr, visited, cur - arr[cur]);
}
}
1361. Validate Binary Tree Nodes
You have n
binary tree nodes numbered from 0
to n - 1
where node i
has two children leftChild[i]
and rightChild[i]
, return true
if and only if all the given nodes form exactly one valid binary tree.
If node i
has no left child then leftChild[i]
will equal -1
, similarly for the right child.
Note that the nodes have no values and that we only use the node numbers in this problem.
Example 1:
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1] Output: true
Example 2:
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1] Output: false
Example 3:
Input: n = 2, leftChild = [1,0], rightChild = [-1,-1] Output: false
Example 4:
Input: n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1] Output: false
Constraints:
1 <= n <= 10^4
leftChild.length == rightChild.length == n
-1 <= leftChild[i], rightChild[i] <= n - 1
图的性质 + BFS
https://leetcode.com/problems/validate-binary-tree-nodes/discuss/556286/Java-Solution-using-BFS
class Solution {
public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
int[] degree = new int[n];
for (int i = 0; i < leftChild.length; i++) {
if (leftChild[i] != -1)
degree[leftChild[i]]++;
if (rightChild[i] != -1)
degree[rightChild[i]]++;
}
int root = 0; int zero = 0; int one = 0;
for (int i = 0; i < n; i++) {
if (degree[i] == 0) {
zero++;
root = i;
}
if (degree[i] == 1) {
one++;
}
}
//排除多个根结点, 排除环
if (zero != 1 || (zero + one) != n) {
return false;
}
Queue<Integer> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int index = q.poll();
n--;
if (leftChild[index] != -1) {
q.offer(leftChild[index]);
}
if (rightChild[index] != -1) {
q.offer(rightChild[index]);
}
}
return n == 0;
}
}
1267. Count Servers that Communicate
You are given a map of a server center, represented as a m * n
integer matrix grid
, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.
Example 1:
Input: grid = [[1,0],[0,1]] Output: 0 Explanation: No servers can communicate with others.
Example 2:
Input: grid = [[1,0],[1,1]] Output: 3 Explanation: All three servers can communicate with at least one other server.
Example 3:
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] Output: 4 Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1
1.暴力求解
class Solution {
public int countServers(int[][] grid) {
int[] row = new int[grid.length];
int[] col = new int[grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
row[i]++;
col[j]++;
}
}
}
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
if (row[i] > 1 || col[j] > 1)
count++;
}
}
}
return count;
}
}
2. 递归
class Solution {
boolean[][] visited;
public int countServers(int[][] grid) {
visited = new boolean[grid.length][grid[0].length];
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
int val = dfs(grid, i, j);
if (val > 1) {
count += val;
}
}
visited[i][j] = true;
}
}
return count;
}
public int dfs(int[][] grid, int i, int j) {
if (visited[i][j] || grid[i][j] == 0)
return 0;
visited[i][j] = true;
int count = 1;
for (int x = 0; x < grid.length; x++) {
count += dfs(grid, x, j);
}
for (int y = 0; y < grid[0].length; y++) {
count += dfs(grid, i, y);
}
return count;
}
}