当前位置:网站首页>2021-05-01: given an ordered array arr, it represents the points located on the X axis. Given a positive number k

2021-05-01: given an ordered array arr, it represents the points located on the X axis. Given a positive number k

2022-06-24 15:55:00 Fuda scaffold constructor's daily question

2021-05-01: Given an ordered array arr, The representative is located in X The point on the axis . Give a positive number K, Represents the length of the rope . Return to the maximum number of points in the rope ? Even if the edge of the rope covers the point, it is covered .

Fuda answer 2021-05-01:

The sliding window . The window will only become larger or unchanged , It doesn't get smaller . Subtract the left pointer position from the last right pointer position , Is the length to be returned .

The code to use golang To write . The code is as follows :

package main

import "fmt"

func main() {
    arr := []int{1, 4, 5, 6, 9, 10, 12, 17}
    ret := maxPoint(arr, 4)
    fmt.Println(ret)
}
func maxPoint(arr []int, num int) int {
    arrLen := len(arr)
    L := 0
    R := 0
    for R < arrLen {
        if arr[R]-arr[L] > num {
            L++
        }
        R++
    }
    return R - L
}

The results are as follows :

picture

Zuo Shen java Code

原网站

版权声明
本文为[Fuda scaffold constructor's daily question]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210504233532324V.html