This commit is contained in:
kkunkka
2025-05-26 11:32:01 +08:00
parent 628ae27ce6
commit b667278d9a
3 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package com.dota.binarySearch._1482;
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
if (m * k > bloomDay.length) {
return -1;
}
int l = 1, r = 0;
for (int i : bloomDay) {
r = Math.max(r, i);
}
while (l < r) {
int mid = l + (r - l) / 2;
if (check(bloomDay, m, k, mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return check(bloomDay, m, k, r)?r:-1;
}
boolean check(int[] bloomDay, int m, int k, int day) {
int cnt = 0;
int sum = 0;
for (int b : bloomDay) {
if (b <= day) {
cnt++;
} else {
cnt = 0;
}
if (cnt == k) {
sum++;
if(sum>=k) return true;
cnt = 0;
}
}
return sum >= m;
}
}

View File

@@ -0,0 +1,24 @@
package com.dota.binarySearch._2594;
class Solution {
public long repairCars(int[] ranks, int cars) {
long l = 1, r = 100L * cars * cars;
while (l < r) {
long mid = l + (r - l) / 2;
if (check(mid, ranks, cars)) {
r = mid;
} else {
l = mid + 1;
}
}
return r;
}
boolean check(long t, int[] ranks, int cars) {
int sum = 0;
for (int r : ranks) {
sum += (int) Math.sqrt( t / r);
}
return sum >= cars;
}
}

View File

@@ -0,0 +1,50 @@
package com.dota.binarySearch._3048;
class Solution {
public int earliestSecondToMarkIndices(int[] nums, int[] changeIndices) {
var book = new int[nums.length + 1];
var cnt = 0;
for (int changeIndex : changeIndices) {
if (book[changeIndex] == 0) {
book[changeIndex] = 1;
cnt++;
}
}
if (cnt < nums.length) {
return -1;
}
int l = 1, r = changeIndices.length;
while (l < r) {
int mid = l + (r - l) / 2;
if(check(nums,changeIndices,mid)) {
r = mid;
} else {
l = mid + 1;
}
}
return check(nums, changeIndices,r)?r:-1;
}
boolean check(int[] nums, int[] changeIndices, int s) {
var book = new int[nums.length + 1];
var sum = nums.length;
for (int i = s-1; i >=0 ; i--) {
if (book[changeIndices[i]] == 0) {
s--;
s = Math.min(s,i);
s-=nums[changeIndices[i]-1];
if (s<0) {
return false;
}
book[changeIndices[i]] = 1;
sum--;
}
}
return sum==0;
}
public static void main(String[] args) {
new Solution().earliestSecondToMarkIndices(new int[]{0,2,3,0}, new int[]{2,4,1,3,3,3,3,3,3,2,1});
}
}