Java 代码:
public class Solution {
public int minCount(int[] coins) {
int res = 0;
for (int coin : coins) {
if ((coin % 2) == 0) {
res += coin / 2;
} else {
res += (coin + 1) / 2;
}
}
return res;
}
public static void main(String[] args) {
Solution solution = new Solution();
// int[] coins = {4, 2, 1};
int[] coins = {2, 3, 10};
int res = solution.minCount(coins);
System.out.println(res);
}
}
Java 代码:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Solution {
public int numWays(int n, int[][] relation, int k) {
// 构建邻接表
boolean[][] adj = new boolean[n][n];
for (int[] r : relation) {
adj[r[0]][r[1]] = true;
}
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
while (!queue.isEmpty() && k > 0) {
int size = queue.size();
k--;
for (int i = 0; i < size; i++) {
Integer head = queue.poll();
List<Integer> next = next(head, adj, n);
for (Integer successor : next) {
queue.add(successor);
}
}
}
int target = n - 1;
int res = 0;
while (!queue.isEmpty()) {
Integer head = queue.poll();
if (head == target) {
res++;
}
}
return res;
}
private List<Integer> next(int i, boolean[][] adj, int n) {
List<Integer> next = new ArrayList<>();
for (int j = 0; j < n; j++) {
if (adj[i][j] && j != i) {
next.add(j);
}
}
return next;
}
public static void main(String[] args) {
Solution solution = new Solution();
// int n = 5;
// int[][] relation = {{0, 2}, {2, 1}, {3, 4}, {2, 3}, {1, 4}, {2, 0}, {0, 4}};
// int k = 3;
int n = 3;
int[][] relation = {{0, 2}, {2, 1}};
int k = 2;
int res = solution.numWays(n, relation, k);
System.out.println(res);
}
}
Java 代码:
import java.util.Arrays;
public class Solution {
// 二分法,0 <= increase[i] <= 10
public int[] getTriggerTime(int[][] increase, int[][] requirements) {
int len = increase.length;
// 最开始算 1 天
int[][] preSum = new int[len + 1][3];
for (int i = 0; i < len; i++) {
for (int j = 0; j < 3; j++) {
preSum[i + 1][j] = increase[i][j] + preSum[i][j];
}
}
// 对于每一个 requirements,二分查找 满足条件的下标
int rLen = requirements.length;
int[] res = new int[rLen];
Arrays.fill(res, -1);
for (int i = 0; i < rLen; i++) {
int[] requirement = requirements[i];
// 特判,最后一天都不满足,才二分去定位
if (requirement[0] > preSum[len][0] ||
requirement[1] > preSum[len][1] ||
requirement[2] > preSum[len][2]) {
continue;
}
// 找数组里第 1 个三项指标全部大于的下标
int left = 0;
int right = len;
while (left < right) {
int mid = (left + right) >> 1;
if (requirement[0] <= preSum[mid][0] &&
requirement[1] <= preSum[mid][1] &&
requirement[2] <= preSum[mid][2]) {
// 当前以及当前右边肯定是,下一轮搜索区间是 [left, mid]
right = mid;
} else {
left = mid + 1;
}
}
res[i] = left;
}
return res;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[][] increase = {{2, 8, 4}, {2, 5, 0}, {10, 9, 8}};
int[][] requirements = {{2, 11, 3}, {15, 10, 7}, {9, 17, 12}, {8, 1, 14}};
// int[][] increase = {{0, 4, 5}, {4, 8, 8}, {8, 6, 1}, {10, 10, 0}};
// int[][] requirements = {{12, 11, 16}, {20, 2, 6}, {9, 2, 6}, {10, 18, 3}, {8, 14, 9}};
// int[][] increase = {{1, 1, 1}};
// int[][] requirements = {{0, 0, 0}};
int[] res = solution.getTriggerTime(increase, requirements);
System.out.println(Arrays.toString(res));
}
}