You are helping the port management to organize boats moored in a port. There is a straight wharf with N mooring bollards and N boats. The wharf (and the dock in front of it) is of length M. Each boat has the same width: 2*X. The bollards are located at the very edge of the wharf. It is possible for more than one bollard to be at the same position.
You have to moor each boat to a separate bollard so that the following rules are satisfied:
- each boat is fixed with a single mooring rope to the bank of the wharf,
- the mooring rope connects the middle of the boat's bow with a bollard,
- the middle of the boat's bow can be set only on integer positions,
- the sides of the boats can touch each other,
- boats cannot overlap,
- boats cannot be placed outside the dock or extend it,
- two boats cannot be tied to the same bollard.
All the boats must have mooring ropes of the same length. The goal is to minimize this length.
More formally, let the max distance be the largest distance between the middle of any boat's bow and the bollard to which the boat is moored. The goal is to align the boats so that the max distance is as small as possible.
You are given a non-empty array R of N integers, and two positive integers X and M. Array R contains the positions of the bollards along the wharf. The wharf's ends are at positions 0 and M.
For example, the following array R, and integers X and M:
R[0] = 1 X = 2 R[1] = 3 M = 16 R[2] = 14describe:
- three bollards at positions 1, 3 and 14,
- three boats of width 4,
- a wharf of length 16.
You can set:
- the center of the first boat at position 2,
- the center of the second boat at position 6,
- the center of the third boat at position 14.
Between the first boat and the first bollard the distance is 1; between the second boat and the second bollard it is 3; and between the third boat and the third bollard it is 0; so the max distance is 3.
Write a function:
class Solution { public int solution(int[] R, int X, int M); }
that, given an array R consisting of N integers, given two integers X and M, returns the minimal max distance you can achieve.
If it is not possible to tie all the boats, the function should return −1.
For example, given the following array R, integers X and M:
R[0] = 1 X = 2 R[1] = 3 M = 16 R[2] = 14the function should return 3, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- X and M are integers within the range [1..1,000,000,000];
- each element of array R is an integer within the range [0..M];
- array R is sorted in non-decreasing order.
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (long)M - ((long)X * (long)R.length);
if (spaceRight < 0) {
return -1;
}
return 3;//TODO
}
}
Solution.java:9: error: possible loss of precision int spaceRight = (long)M - ((long)X * (long)R.length); ^ required: int found: long 1 error
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)R.length));
if (spaceRight < 0) {
return -1;
}
return 3;//TODO
}
}
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
for(int k = 0; k < N; ++k) {
}
return 3;//TODO
}
}
import java.math.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos = bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
} else {//boatPos > bollardPos
}
}
return maxCost;
}
}
Solution.java:1: error: cannot find symbol import java.math.Math; ^ symbol: class Math location: package java.math Solution.java:25: error: incompatible types if (boatPos = bollardPos) { ^ required: boolean found: int 2 errors
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos = bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
} else {//boatPos > bollardPos
}
}
return maxCost;
}
}
Solution.java:25: error: incompatible types if (boatPos = bollardPos) { ^ required: boolean found: int 1 error
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
} else {//boatPos > bollardPos
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
} else {//boatPos > bollardPos
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
TODO lastBoatPos;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, maxMovingLeftCost, tmpCost);
}
}
return maxCost;
}
}
Solution.java:38: error: cannot find symbol TODO lastBoatPos; ^ symbol: class TODO location: class Solution Solution.java:38: error: variable lastBoatPos is already defined in method solution(int[],int,int) TODO lastBoatPos; ^ Solution.java:53: error: no suitable method found for max(int,int,int) maxCost = Math.max(maxCost, maxMovingLeftCost, tmpCost); ^ method Math.max(double,double) is not applicable (actual and formal argument lists differ in length) method Math.max(float,float) is not applicable (actual and formal argument lists differ in length) method Math.max(long,long) is not applicable (actual and formal argument lists differ in length) method Math.max(int,int) is not applicable (actual and formal argument lists differ in length) 3 errors
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
TODO lastBoatPos;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
}
}
return maxCost;
}
}
Solution.java:38: error: cannot find symbol TODO lastBoatPos; ^ symbol: class TODO location: class Solution Solution.java:38: error: variable lastBoatPos is already defined in method solution(int[],int,int) TODO lastBoatPos; ^ 2 errors
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: 3
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - ((long)X * (long)N));
if (M == 10) {
return spaceRight;
}
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: 0
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (M == 10) {
return spaceRight;
}
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: -10
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: -1
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: 10
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: 899999946
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
function result: 0
function result: 899999946
function result: 3
function result: 4
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) ((long)M - (2L * (long)X * (long)N));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) (((long)M) - (2L * ((long)X) * ((long)N)));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) (((long)M) - (2L * ((long)X) * ((long)N)));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) (((long)M) - (2L * ((long)X) * ((long)N)));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
import java.lang.Math;
// you can also use imports, for example:
// import java.math.*;
class Solution {
public int solution(int[] R, int X, int M) {
int N = R.length;
//maxFreeSpaceLeft
int spaceLeft = 0;
//maxFreeSpaceRight
int spaceRight = (int) (((long)M) - (2L * ((long)X) * ((long)N)));
if (spaceRight < 0) {
return -1;
}
int maxCost = 0;
int maxMovingLeftCost = 0;
int lastBoatPos = -X;
for(int k = 0; k < N; ++k) {
int bollardPos = R[k];
int boatPos = lastBoatPos + 2*X;
if (boatPos == bollardPos) {
lastBoatPos = boatPos;
} else if (boatPos < bollardPos) {
int moveRight = Math.min(bollardPos - boatPos, spaceRight);
spaceLeft += moveRight;
spaceRight -= moveRight;
int notMoved = (bollardPos - boatPos) - moveRight;
maxCost = Math.max(maxCost, notMoved);
maxMovingLeftCost = Math.max(maxMovingLeftCost - moveRight, notMoved);
lastBoatPos = boatPos + moveRight;
} else {//boatPos > bollardPos
int tmpCost = boatPos - bollardPos;//koszt jeśli nic nie przesuniemy
//spaceLeft - tyle możemy sie max przesunac
//maxMovingLeftCost + tmpCost //koszt jeśli przesuniemy całkowicie
//chcemy przesunąć tyle by oba koszty bylu jak najmniejsze
int moveLeft = (tmpCost - maxMovingLeftCost + 1) / 2;
moveLeft = Math.max(moveLeft, 0);//maxMovingLeftCost przesuwanie mogło być droższe od zostania w miejscu
moveLeft = Math.min(moveLeft, spaceLeft);
spaceLeft -= moveLeft;
spaceRight += moveLeft;
maxMovingLeftCost += moveLeft;
tmpCost -= moveLeft;
maxCost = Math.max(maxCost, Math.max(maxMovingLeftCost, tmpCost));
lastBoatPos = boatPos - moveLeft;
}
}
return maxCost;
}
}
The solution obtained perfect score.