首页 > 其他分享 >Good Segment

Good Segment

时间:2023-04-11 23:33:37浏览次数:37  
标签:upper lower Good int max import badNumbers Segment

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

相关文章

  • 关于在执行 SAP ERP MM 模块 Post Goods Issue 时修改 Material Cost 的讨论
    我的知识星球里有朋友向我提问:MaterialPGI(601movement)willcalculatethematerialcostfrommaterialmasterdata.Myquestionis:isthereanywaystochangethematerialcostwhenPGI?(Exceptenhancement)在SAPERPMM模块中,MaterialPostGoodsIssue(PGI......
  • 上线一天,4k star | Facebook:Segment Anything
    前言 本文介绍了FacebookAIResearch的SegmentAnything(SA)项目:用于图像分割的新任务、模型和数据集。在数据收集循环中使用该模型,它构建了迄今为止最大的分割数据集,在1100万张许可和尊重隐私的图像上有超过10亿个掩码。该模型被设计和训练为可提示的,因此它可以将零样本......
  • 一个类似Tabs的控件SegmentControl
    packagecom.ql.view;importjava.util.HashMap;importjava.util.Map;importandroid.content.Context;importandroid.graphics.Color;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importandroid.widget.Linea......
  • segment anything
    Whatisthestructureofthemodel?AViT-HimageencoderthatrunsonceperimageandoutputsanimageembeddingApromptencoderthatembedsinputpromptssuchasclicksorboxesAlightweighttransformerbasedmaskdecoderthatpredictsobjectmasks......
  • Keil ERROR C249: 'DATA': SEGMENT TOO LARGE
    1、网上都说存储器模式改为大型即可,但是我尝试后发现有点问题。 2、经过很多操作,最后解决了(1)unsignedcharcodeNumM[480],添加code关键字,但是这不是问题关键;(2)本来声明了两个数组,一个是unsignedcharcodeNumM[256],一个是unsignedcharHanzi[14][16],最初采用把Hanzi数组也......
  • goodFeaturesToTrack
    一、goodFeaturesToTrack1、过程:1)函数查找图像中或指定图像区域中最突出的角点(1)函数使用cornerMinEigenVal或cornerHarris计算每个源图像像素的角点质量度量。(2)函数执行非最大值抑制(保留3x3邻域中的局部最大值)。(3)最小特征值小于qualityLevel*maxx,yqualityMeasureMap(x,y)......
  • 【segmentation fault】std::string析构崩溃
    今天写了一个小工具,运行时发生segmentationfault,现象如下第一步:review崩溃附近代码,产生疑惑,崩溃的地方居然是变量定义的地方std::stringaccessToken;崩溃在这个地方,我直接懵了,只是变量定义为啥会报错,没有任何思路,打算单步调试。第二步:单步调试代码,发现并且是定义的时候崩......
  • segment_tree
    这是一棵一棵一棵线段树众所周知,线段树是一个优质的维护区间信息(需要满足结合律)的数据结构我们可以\(O(nlogn)\)建树\(O(logn)\)查询任意区间的信息关于结合......
  • Segment Tree Beats! 初步和其他
    不会渐进表示,全无脑用\(\Theta\)了/kk目录区间最值问题不含区间加减的情况划分数域历史最值问题双半群模型问题描述使用标记维护的问题区间最值问题不含区间加减的......
  • [CVPR2020] RandLA-Net: Efficient Semantic Segmentation of Large-Scale Point Clou
    大佬的TensorFlow代码:here另一个大佬的Pytorch代码:等我看完代码再贴链接,之前那个不太行keywords高分辨率点云——约\(10^5\)点云语义分割多层次特征在正式开始......