Given an array of bad numbers and a range of integers, determine the longest segment of integers within the range that does not include any bad numbers.
Example
n = 6
badNumbers = [37, 7, 22, 15, 49, 60]
lower = 3
upper = 48
The segments in the range 3 to 48, inclusive, without any bad numbers are: [3, 6], [8, 14], [16, 21], [23, 36] and [38, 48].
The longest segment is [23, 36] and it is 14 elements long, thus the return value is 14
Function Description
Complete the function goodSegment in the editor below.
goodSegment has the following parameter(s):
int badNumbers[n]: an array of integers
int lower: an integer, the lower bound, inclusive
int upper: an integer, the upper bound, inclusive
Returns:
int: an integer denoting the length of longest contiguous sequence of natural numbers in the range lower to upper, inclusive, which does not include any bad numbers.
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ badNumbers[i]≤ 109
- badNumbers contains distinct elements.
- 1 ≤ lower ≤ upper ≤ 109
Input Format For Custom Testing
Input from stdin will be processed as follows and passed to the function.
The first line contains an integer, n, the number of elements in badNumbers.
Each of the next n lines contains an integer, badNumbers[i].
The next line contains an integer, lower, the lower range limit, inclusive.
The last line contains an integer, upper, the upper range limit, inclusive.
Sample Case 0
Sample Input 0
STDIN Function ----- -------- 4 → badNumbers[] size n = 4 5 → badNumbers = [ 5, 4, 2, 15 ] 4 2 15 1 → lower = 1 10 → upper = 10
Sample Output 0
5
Explanation 0
All possible segments in the range 1 to 10 having no bad numbers are [1, 1], [3, 3], and [6, 10]. The longest length segment is [6, 10] having length 5.
Sample Case 1
Sample Input 1
STDIN Function ----- -------- 4 → badNumbers[] size n = 4 8 → badNumbers = [ 8, 6, 20, 12 ] 6 20 12 1 → lower = 1 30 → upper = 30
Sample Output 1
10
Explanation 1
All possible segments in the range 1 to 30 having no bad numbers are [1, 5], [7, 7], [9, 11], [13, 19], and [21, 30]. The longest length segment is [21, 30] having length 10.
Sample Case 2
Sample Input 2
STDIN Function ----- -------- 4 → badNumbers[] size n = 4 1 → badNumbers = [ 1, 2, 3, 4 ] 2 3 4 1 → lower = 1 4 → upper = 4
Sample Output 2
0
Explanation 1
There are no segments in the range 1 to 4 having no bad numbers. The longest length segment is 0.
import java.util.Collections;
import java.util.List;
/**
* @author hunter
* @since 2023-04-04 12:43:18
*/
public class Result {
/*
* Complete the 'goodSegment' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY badNumbers
* 2. INTEGER lower
* 3. INTEGER upper
*/
public static int goodSegment(List<Integer> badNumbers, int lower, int upper) {
int max = 0;
Collections.sort(badNumbers);
int start = badNumbers.get(0);
int end = badNumbers.get(badNumbers.size() - 1);
/**
* status 1:lower<=start<=upper<=end
* start-------------end
* lower-------upper
*/
if (lower <= start && upper <= end) {
badNumbers.add(lower);
badNumbers.add(upper);
Collections.sort(badNumbers);
for (int i = 0; i < badNumbers.size() - 1; i++) {
if (badNumbers.get(i) == upper) {
break;
}
int count = badNumbers.get(i + 1) - badNumbers.get(i);
if (count > 1) {
max = Math.max(max, count);
}
}
}
/**
* status 2:start<=lower<=upper<=end
* start----------------end
* lower--upper
*/
if (start < lower && upper < end) {
badNumbers.add(lower);
badNumbers.add(upper);
Collections.sort(badNumbers);
for (int i = 0; i < badNumbers.size() - 1; i++) {
if (badNumbers.get(i) >= lower) {
if (badNumbers.get(i) == upper) {
break;
}
int count = badNumbers.get(i + 1) - badNumbers.get(i) - 1;
if (count > 1) {
max = Math.max(max, count);
}
}
}
}
/**
* status 3:start<=lower<=end<=upper
* start----------------end
* lower-------------upper
*/
if (start < lower && end < upper) {
badNumbers.add(lower);
badNumbers.add(upper);
Collections.sort(badNumbers);
for (int i = 0; i < badNumbers.size() - 1; i++) {
if (badNumbers.get(i) >= lower) {
int count = badNumbers.get(i + 1) - badNumbers.get(i);
if (count > 1) {
max = Math.max(max, count);
}
}
}
}
/**
* status 4:lower<=start<=end<=upper
* start----------------end
* lower-------------------------upper
*/
if (lower < start && end < upper) {
badNumbers.add(lower);
badNumbers.add(upper);
Collections.sort(badNumbers);
for (int i = 0; i < badNumbers.size() - 1; i++) {
int count = badNumbers.get(i + 1) - badNumbers.get(i);
if (count > 1) {
max = Math.max(max, count);
}
}
}
return max;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toList;
/**
* @author hunter
* @since 2023-04-04 12:43:44
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int badNumbersCount = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> badNumbers = IntStream.range(0, badNumbersCount).mapToObj(i -> {
try {
return bufferedReader.readLine().replaceAll("\\s+$", "");
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.map(String::trim)
.map(Integer::parseInt)
.collect(toList());
int lower = Integer.parseInt(bufferedReader.readLine().trim());
int upper = Integer.parseInt(bufferedReader.readLine().trim());
int result = Result.goodSegment(badNumbers, lower, upper);
System.out.println(result);
// bufferedWriter.write(String.valueOf(result));
// bufferedWriter.newLine();
bufferedReader.close();
// bufferedWriter.close();
}
}
标签:upper,lower,Good,int,max,import,badNumbers,Segment
From: https://www.cnblogs.com/createman/p/17308275.html