add
This commit is contained in:
parent
e9f2979324
commit
fdbb317969
70
5.leetcode/src/com/fanxb/common/Q1705.java
Normal file
70
5.leetcode/src/com/fanxb/common/Q1705.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
|
||||||
|
public class Q1705 {
|
||||||
|
public int eatenApples(int[] apples, int[] days) {
|
||||||
|
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(a -> a + days[a]));
|
||||||
|
int res = 0, i = 0;
|
||||||
|
//第一阶段,苹果还在长
|
||||||
|
for (; i < days.length; i++) {
|
||||||
|
//移除过期的苹果
|
||||||
|
removeOutDate(days, priorityQueue, i);
|
||||||
|
//加入新苹果
|
||||||
|
if (apples[i] > 0) {
|
||||||
|
priorityQueue.add(i);
|
||||||
|
}
|
||||||
|
//取一个苹果吃
|
||||||
|
Integer index = priorityQueue.peek();
|
||||||
|
if (index != null) {
|
||||||
|
if (apples[index] == 1) {
|
||||||
|
//吃完的苹果移出队列
|
||||||
|
priorityQueue.poll();
|
||||||
|
} else {
|
||||||
|
apples[index]--;
|
||||||
|
}
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//第二阶段,吃剩余的苹果
|
||||||
|
for (; ; i++) {
|
||||||
|
//移除过期苹果
|
||||||
|
removeOutDate(days, priorityQueue, i);
|
||||||
|
Integer index = priorityQueue.peek();
|
||||||
|
if (index == null) {
|
||||||
|
//没有苹果说明吃完了
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (apples[index] == 1) {
|
||||||
|
//吃完的苹果移出队列
|
||||||
|
priorityQueue.poll();
|
||||||
|
} else {
|
||||||
|
apples[index]--;
|
||||||
|
}
|
||||||
|
res++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void removeOutDate(int[] days, PriorityQueue<Integer> queue, int day) {
|
||||||
|
Integer index;
|
||||||
|
while ((index = queue.peek()) != null) {
|
||||||
|
if ((index + days[index] - 1) < day) {
|
||||||
|
queue.poll();
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(new Q1705().eatenApples(new int[]{1, 2, 3, 5, 2}, new int[]{3, 2, 1, 4, 2}));
|
||||||
|
}
|
||||||
|
}
|
40
5.leetcode/src/com/fanxb/common/Q825.java
Normal file
40
5.leetcode/src/com/fanxb/common/Q825.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Q825 {
|
||||||
|
public int numFriendRequests(int[] ages) {
|
||||||
|
int res = 0;
|
||||||
|
Arrays.sort(ages);
|
||||||
|
//二分查找100的分界点
|
||||||
|
int index100 = search(ages, 100, 0, ages.length - 1);
|
||||||
|
for (int i = search(ages, 15, 0, index100); i <= index100; i++) {
|
||||||
|
int startIndex = search(ages, Double.valueOf(Math.ceil(0.5 * ages[i] + 7)).intValue(), 0, i);
|
||||||
|
res += i - startIndex;
|
||||||
|
}
|
||||||
|
for (int i = index100; i < ages.length; i++) {
|
||||||
|
int startIndex = search(ages, Double.valueOf(Math.ceil(0.5 * ages[i] + 7)).intValue(), index100, i);
|
||||||
|
res += i - startIndex;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int search(int[] ages, int target, int initL, int initR) {
|
||||||
|
int l = initL, r = initR, mid = (l + r) / 2;
|
||||||
|
while (l < r) {
|
||||||
|
if (ages[mid] < target) {
|
||||||
|
l = mid + 1;
|
||||||
|
} else {
|
||||||
|
r = mid;
|
||||||
|
}
|
||||||
|
mid = (l + r) / 2;
|
||||||
|
}
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(new Q825().numFriendRequests(new int[]{16, 16}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user