Counting bounded slice codility

后端 未结 7 1325
一个人的身影
一个人的身影 2021-02-08 13:11

I have recently attended a programming test in codility, and the question is to find the Number of bounded slice in an array..

I am just giving you breif explanation of

7条回答
  •  半阙折子戏
    2021-02-08 13:50

    Here is my attempt at solving this problem:

    - you start with p and q form position 0, min =max =0;
    - loop until p = q = N-1
    - as long as max-min<=k advance q and increment number of bounded slides.
    - if max-min >k advance p
    - you need to keep track of 2x min/max values because when you advance p, you might remove one  or both of the min/max values
    - each time you advance p or q update min/max
    

    I can write the code if you want, but I think the idea is explicit enough...

    Hope it helps.

提交回复
热议问题