while preparing for an interview
Android DeveloperWhat field?
which included BASIC code
Si.No -> italian = Yes. No.the ith of whom is in seat Si. No
No two diners are sitting in the same seat
Lol who remembers the good old days of typing in BASIC programs from magazines and books?!?!
That's one of the reasons restaurants (or "cafeteria") close downA cafeteria table consists of a row of N seats, numbered from 1 to N from left to right. Social distancing guidelines require that every diner be seated such that K seats to their left and K seats to their right (or all the remaining seats to that side if there are fewer than K) remain empty.
There are currently M diners seated at the table, the ith of whom is in seat Si. No two diners are sitting in the same seat, and the social distancing guidelines are satisfied.
Determine the maximum number of additional diners who can potentially sit at the table without social distancing guidelines being violated for any new or existing diners, assuming that the existing diners cannot move and that the additional diners will cooperate to maximize how many of them can sit down.
Please take care to write a solution which runs within the time limit.
Sample test case #1
N = 10
K = 1
M = 2
S = [2, 6]
Expected Return Value = 3
Sample test case #2
N = 15
K = 2
M = 3
S = [11, 6, 14]
Expected Return Value = 1
public static long getMaxAdditionalDiners(long N, long K, int M, long[] S) {
Arrays.sort(S);
long additionalDiners = 0;
long currentSeat = 1;
int index = 0;
while (currentSeat <= N) {
// Check if current seat is in the block of an existing diner
if (index < M && currentSeat >= S[index] - K && currentSeat <= S[index] + K) {
currentSeat = S[index] + K + 1;
index++;
} else {
// Safe to place a new diner
additionalDiners++;
currentSeat += (K + 1);
}
}
return additionalDiners;
}
1≤N≤10^15
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
public long getMaxAdditionalDinersCount(long N, long K, int M, long[] S) {
long seatsPerDiner = K + 1;
long additionalDiners = 0;
// If there are no diners initially, the entire table is one large gap.
if (M == 0) {
// In a gap of length L, we can place ceil(L / seatsPerDiner) diners.
// This can be calculated using integer arithmetic as (L + K) / (K + 1).
System.out.println("N = " + N + " / " + " K = " + K);
System.out.println("seatsPerDiner = " + (K -1));
System.out.println("returning: " + (N + K) / seatsPerDiner);
return (N + K) / seatsPerDiner;
}
// Sort the array of existing diner positions to easily calculate gaps between them.
Arrays.sort(S);
// --- 1. Calculate available space before the first diner ---
// The first diner is at S[0]. They block seats down to S[0] - K.
// The available gap is from seat 1 to S[0] - K - 1.
long firstGapLength = S[0] - K - 1;
if (firstGapLength > 0) {
additionalDiners += (firstGapLength + K) / seatsPerDiner;
}
// --- 2. Calculate available space between adjacent diners ---
// Iterate through each pair of adjacent diners.
for (int i = 0; i < M - 1; i++) {
// Gap between diner at S[i] and diner at S[i+1].
// Diner at S[i] blocks seats up to S[i] + K.
// Diner at S[i+1] blocks seats down to S[i+1] - K.
// The available gap is from S[i] + K + 1 to S[i+1] - K - 1.
long betweenGapLength = (S[i+1] - K - 1) - (S[i] + K + 1) + 1;
betweenGapLength = S[i+1] - S[i] - 2 * K - 1;
if (betweenGapLength > 0) {
additionalDiners += (betweenGapLength + K) / seatsPerDiner;
}
}
// --- 3. Calculate available space after the last diner ---
// The last diner is at S[M-1]. They block seats up to S[M-1] + K.
// The available gap is from S[M-1] + K + 1 to N.
long lastGapLength = N - (S[M - 1] + K);
if (lastGapLength > 0) {
additionalDiners += (lastGapLength + K) / seatsPerDiner;
}
return additionalDiners;
}
}
I never saw a Cafeteria Table with this amount of Seatsusing a number of seats of 1_000_000_000_000_000
Clearly you haven't been to my family's partiesI never saw a Cateria Table with this amount of Seats
Even not a Cinema, Concert-Hall, Football-Stadium ;-)
EDIT:1≤N≤10^15
1≤K≤N
1≤M≤500,000
M≤N
1≤Si≤N
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?