Untitled
Anonymous
Plain Text
09/05/2025 11:08 PM
1.8 KB
27
Indexable
Never
public class Main
{
public static void main(String[] args) {
//first of all you search for the biggest tower then you make a binary search where you
// see if the data still goes through and if it
// be it l working the r not wroking then we do mid m = (r+l)/2
//in case it works l = m+1 if not r = m and use a while(l < r) and when it ends we do a
//l-1 to get the actual value
public static int maxStorm(int width, int[][] towers, int maxJump, int maxEnergy){
int maxTower = -1;
for(int i = 0; i < towers.length; i++){
if (towers[i][0] > maxTower){
maxTower = towers[i][0];
}
}
int lower = 0;
int upper = maxTower;
while(lower < upper){
int m = (upper + lower) / 2;
if(verifyConection(m, width, maxJump, maxEnergy, towers)){
lower = m + +1;
} else {
upper = m;
}
}
return lower - 1;
}
public static boolean verifyConection(int level, int width, int maxJump, int maxEnergy,
int[][] towers){
int sum = 0;
int currCoordinate = 0;
for(int i = ; i < towers.length; i++){
if(towers[i][1] < level){
continue;
}
if(towers[i][0] - currCoordinate > maxJump){
return false;
}
if(currCoordinate == 0){
sum+= towers[i][0] * towers[i][0];
} else {
sum += (towers[i-1][0] - towers[i][0]) * (towers[i-1][0] - towers[i][0]);
}
currCoordinate = towers[i][0];
if(sum > maxEnergy){
return false;
}
}
return true;
}
}
}Editor is loading...
Leave a Comment