type Extract<
T extends any[],
U extends number,
I extends any[] = [],
K extends any = null
> = I['length'] extends T['length'] | U
? I
: K extends null
? Extract<T, U, [...I, T[I['length']]]>
: Extract<T, U, [...I, K], K>;
type Fill<
T extends any[],
U,
Start extends number = 0,
End extends number = T['length']
> = Extract<T, T['length'], Extract<T, End, Extract<T, Start>, U>>;
思路:
本问题可以分为三个阶段处理:
Extract
方法功能是产生目标长度数组,可以指定填充的值,以及添加初始数组。
This problem can be handled in three stages:
The Extract method's function is to produce an array of the target length, allowing for the specification of a value to fill with and the addition of the initial array.
type T1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
type T2 = Extract<T1, 3>; // [1, 2, 3]
type T3 = Extract<T1, 5, [0, 0]>; // [0, 0, 3, 4, 5]
type T4 = Extract<T1, 5, [0, 0], true>; // [0, 0, true, true, true]
Solution by quanquan2100 #34619
// [...L, 1][Start] extends undefined是看下一个元素到替换位置未
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
L extends any[] = []
> = T extends [infer R, ...infer rest] ?
[...L, 1][Start] extends undefined ?
Fill<rest, N, Start, End, [...L, R]> :
[...L, 1][End] extends undefined ?
Fill<rest, N, Start, End, [...L, N]> :
Fill<rest, N, Start, End, [...L, R]> : L
Solution by ouzexi #34074
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Ret extends unknown[] = [],
Prev extends boolean = false,
// use this to mark whether we shall Fill
// we seperate T to three segments [ ...Not fill, ...Fill, ...Not fill ]
// initially we do not fill,
// when we see Start, we set DoFill to true
// and preserve this state, until we see Ret
DoFill extends boolean =
Prev extends true
? Ret['length'] extends End
? false
: Prev
: Ret['length'] extends Start
? Ret['length'] extends End ? false : true // in case End == Start
: Prev
> = Ret['length'] extends T['length'] ? Ret
: Fill<T, N, Start, End, [...Ret, DoFill extends true ? N : T[Ret['length']]], DoFill>
Solution by bkmashiro #33199
// your answers
type Length<T extends any[]> = T['length'];
type Push<T extends any[], P> = [...T, P];
type NTuple<N extends number, T extends any[] = []> = Length<T> extends N ? T : NTuple<N, Push<T, any>>;
type Add<N1 extends number, N2 extends number> = Length<[...NTuple<N1>, ...NTuple<N2>]>;
// N1이 큰 경우에만 true
type IsBiggerOrEqual<N1 extends number, N2 extends number> = NTuple<N1> extends [...NTuple<N2>, ...any[]] ? true : false;
type IsBigger<N1 extends number, N2 extends number> = IsBiggerOrEqual<N1, N2> extends true ? N1 extends N2 ? false : true : false;
// 두 값 사이에 속한 경우에만 true (TargetNumber가 Minimum 이상, Maximum 미만인 경우)
type Between<TargetNumber extends number, Minimum extends number, Maximum extends number> =
IsBiggerOrEqual<TargetNumber, Minimum> extends true
? IsBigger<Maximum, TargetNumber> extends true
? true
: false
: false
type ToNumber<T> = T extends number ? T : never;
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
CurrentIndex extends number = 0
> = T extends [infer First, ...infer Rest]
? Between<CurrentIndex, Start, End> extends true
? [N, ...Fill<Rest, N, Start, End, ToNumber<Add<CurrentIndex, 1>>>]
: [First, ...Fill<Rest, N, Start, End, ToNumber<Add<CurrentIndex, 1>>>]
: []
Solution by kakasoo #32734
type Fill<T extends any[], U extends number> = {
[K in keyof T]: U;
};
Solution by rkamely #32104
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Result extends unknown[] = [],
> = T extends [infer L, ...infer Rest]
? Result['length'] extends Start
? Start extends End
? [...Result, ...T]
: Fill<Rest, N, [...Result, N]['length'] & number, End, [...Result, N]>
: Fill<Rest, N, Start, End, [...Result, L]>
: Result
Solution by Lu9709 #32026
// your answers
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
C extends any[] = [],
OPEN extends boolean = false,
> = Start extends End ? T :
T extends [infer S, ...infer O] ? (
C['length'] extends Start ? [N, ...Fill<O, N, Start, End, [1, ...C], true>] :
C['length'] extends End ? [S, ...Fill<O, N, Start, End, [1, ...C], false>] :
[OPEN extends true ? N : S, ...Fill<O, N, Start, End, [1, ...C], OPEN>]
) : [];
Solution by tarotlwei #31735
When current index is Start or above and lower than End, I'm increasing Start by 1.
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Acc extends unknown[] = []
> =
T extends [infer Head, ...infer Tail]
? Acc["length"] extends Start
? Acc["length"] extends End
? Fill<Tail, N, Start, End, [...Acc, Head]>
: Fill<Tail, N, [...Acc, 1]["length"], End, [...Acc, N]>
: Fill<Tail, N, Start, End, [...Acc, Head]>
: Acc
;
Solution by sdrjs #31701
// your answers
type NumberToArr<N extends `${number}` = '0', A extends 1[] = []> = `${A['length']}` extends N ? A : NumberToArr<N, [...A, 1]>;
type IsBetween<
N extends 1[] = [],
S extends 1[] = [],
E extends 1[] = [],
> = N extends [...S, ...args: infer R]
? E extends [...N, infer M, ...args: infer R]
? true
: false
: false
type Transform<
T extends unknown[],
N,
Start extends 1[] = [],
End extends 1[] = [],
> = {
[k in keyof T]: k extends `${number}` ? IsBetween<NumberToArr<k>, Start, End> extends true ? N : T[k] : T[k]
}
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
> = Transform<
T,
N,
NumberToArr<`${Start}`>,
NumberToArr<`${End}`>
>
Solution by TRIS-H #31638
type ArrayWithLength<T extends number, U extends any[] = []> =
U['length'] extends T ? U : ArrayWithLength<T, [true, ...U]>
type GreaterThan<T extends number, U extends number> =
ArrayWithLength<U> extends [...ArrayWithLength<T>, ...infer _] ?
false :
true
type GreaterOrEqual<T extends number, U extends number> =
T extends U ?
true :
GreaterThan<T, U>
type Fill<
T extends unknown[],
FillTo,
Start extends number = 0,
End extends number = T['length'],
U extends unknown[] = []
> =
U['length'] extends T['length'] ?
U :
T extends [infer F, ...infer Rest] ?
GreaterOrEqual<U['length'], Start> extends true ?
GreaterThan<End, U['length']> extends true ?
Fill<Rest, FillTo, Start, End, [...U, FillTo]> :
Fill<Rest, FillTo, Start, End, [...U, F]> :
Fill<Rest, FillTo, Start, End, [...U, F]> :
U
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Fill<[], 0>, []>>,
Expect<Equal<Fill<[], 0, 0, 3>, []>>,
Expect<Equal<Fill<[1, 2, 3], 0, 0, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0, 2, 2>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], 0>, [0, 0, 0]>>,
Expect<Equal<Fill<[1, 2, 3], true>, [true, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 1>, [true, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 1, 3>, [1, true, true]>>,
Expect<Equal<Fill<[1, 2, 3], true, 10, 0>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 10, 20>, [1, 2, 3]>>,
Expect<Equal<Fill<[1, 2, 3], true, 0, 10>, [true, true, true]>>,
]
Solution by gearonixx #30916
// your answers
type GreaterThan<T extends number, U extends number, Conter extends never[] = []> = Conter["length"] extends T ? false : Conter["length"] extends U ? true : GreaterThan<T, U, [never, ...Conter]>
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Counter extends unknown[] = [],
Flag = Start extends 0 ? true : false
> = GreaterThan<End, Start> extends true ? T extends [infer First, ...infer Rest] ? [Flag extends true ? N : First, ...Fill<Rest, N, Start, End, [never, ...Counter], [never, ...Counter]["length"] extends (Start | End) ? Exclude<boolean, Flag> : Flag>] : [] : T
Solution by Kakeru-Miyazaki #30891
type GreaterThanOrEqual<T extends number, U extends number> = MakeNumberToArray<T> extends [
...MakeNumberToArray<U>,
...infer _,
]
? true
: false;
type Fill<
T extends unknown[],
N extends unknown,
Start extends number = 0,
End extends number = T['length'],
R extends unknown[] = [],
> = T extends [infer F, ...infer Rest]
? GreaterThanOrEqual<R['length'], Start> extends true
? GreaterThanOrEqual<R['length'], End> extends true
? Fill<Rest, N, Start, End, [...R, F]>
: Fill<Rest, N, Start, End, [...R, N]>
: Fill<Rest, N, Start, End, [...R, F]>
: R;
Solution by leejaehyup #30847
// your answers
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
A extends any[] = [],
B extends boolean = false
> =
Start extends End? T:
A['length'] extends Start?
T extends [infer F, ...infer R]? Fill<R, N, Start, End, [...A, N], true>
:A
:A['length'] extends End? [...A, ...T]
: T extends [infer F, ...infer R]?
B extends true? Fill<R, N, Start, End, [...A, N], true>: Fill<R, N, Start, End, [...A, F], false>
:A
引入两个A用于记录记录数据,并用其长度记录当前索引值,B用于判断当前索引值是否在替换范围 当index = start 变为 true
Solution by yangyyou #30461
With an extra array argument A
for accumulating the result (starting with []
), A
itself serves as count (A[‘length’]
). When this count reaches End
, we can shortcut to […A, …T]
.
Otherwise, we infer the first element F
and rest R
of T
and append F
to A
in a recursive call for R
- until the count A[‘length’]
reaches Start
. Then we append N
instead.
There is no need to add an extra flag argument telling that we passed Start
, when we pass Start + 1
(as […A, N][‘length’]
) as the new Start
argument in the recursive call.
When there is nothing more to infer (when T
is []
), the result is in A
.
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
A extends unknown[] = []
> =
A['length'] extends End
? [...A, ...T]
: T extends [infer F, ...infer R]
? A['length'] extends Start
? Fill<R, N, [...A, N]['length'], End, [...A, N]>
: Fill<R, N, Start, End, [...A, F]>
: A
Solution by sbr61 #30212
// 你的答案
type NTSA<T extends unknown[], S extends number, U extends unknown[] = []> = S extends U['length'] ? U :
T extends [infer F, ...infer Rest] ? NTSA<Rest, S, [...U, F]> : U
type NTEA<T extends unknown[], E extends number, A = T, U extends unknown[] = []> = E extends U['length'] ?
T extends [...U, ...infer R] ? R : [] : A extends [infer F, ...infer Rest] ? NTEA<T, E, Rest, [...U, F]> : []
type FillT<T extends unknown[], N> = T extends [any, ...infer R] ? [N, ...FillT<R, N>] : []
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
SA extends unknown[] = NTSA<T, Start>,
EA extends unknown[] = NTEA<T, End>
> = T extends [...SA, ...infer Rest, ...EA] ? [...SA, ...FillT<Rest, N>, ...EA] : T
Solution by YE840926098 #30124
1、加一个变量R作为最终的结果返回 2、当start === end的时候,如果T['length'] === R['length']的话就直接返回R,否则把剩余的T加入到R中再返回R
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
R extends unknown[] = []
> = Start extends End ? R['length'] extends T['length'] ? R : [... R, ... T] :
T extends [infer F, ... infer Rest] ?
R['length'] extends Start ? Fill<Rest, N, [...R, N]['length'], End, [...R, N]> : Fill<Rest, N, Start, End, [...R, F]>
: R
Solution by sv-98-maxin #30021
// your answers
// 增加了一个C,记录替换前的元素,用于判断在哪里开始替换
// 增加了一个U,记录替换后的数组结果,用于结果的返回
// 1、start 和 end相等,没有要替换的,直接返回T
// 2、C['length']和Start相等判断,不等则往C和U里面加入T的第一个元素F,递归计算,当T中没有元素的时候,说明还没有到start的位置,数组就遍历空了,直接返回C即可
// 相等后则进行下一步计算
// 3、判断U['length']和 End是否相等,不等则说明可以替换当前元素,对T进行推断,如果T还可以划分为[infer F, ...infer R],就用N替换F,继续递归计算,如果不行,则说明T遍历完了,还没有到end,可以直接返回U了
// 相等则说明全替换完了,返回U和剩余的T,[...U, ...T]
// 好像处理不了负数,感觉还是有点问题
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
C extends unknown[] = [],
U extends unknown[] = []
> = Equal<Start, End> extends true
? T
: Equal<C['length'], Start> extends true
? Equal<U['length'], End> extends true
? [...U, ...T]
: T extends [infer F, ...infer R]
? Fill<R, N, Start, End, C, [...U, N]>
: U
: T extends [infer F, ...infer R]
? Fill<R, N, Start, End, [...C, F], [...U, F]>
: C
Solution by bebusy007 #30018
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Result extends unknown[] = [],
> = Start extends End
? [...Result, ...T]
: T extends [infer R, ...infer Rest]
? Fill<
Rest,
N,
Start extends Result['length'] ? [...Result, 0]['length'] : Start,
End,
[...Result, Start extends Result['length'] ? N : R]
>
: Result
Solution by zhangyu1818 #29995
题目意思很容易理解,就是将数组下标Idx
在 [Start, End)
范围里的元素用N
替换。
第一次在遇到这个题的时候,由于把这个条件判断想的太复杂,导致没有做出来。
过了一周后再做,重新捋思路,就用最简单的判断,即 Idx >= Start && Idx < End
判断,符合这个条件就替换,不符合就不替换,通过了。
因此,需要写两个工具类:
GET
: 大于等于 工具类LT
: 小于 工具类// A 大于等于 B
type GET<A extends number, B extends number, cnt extends 1[] = []> =
A extends B
? true
: cnt['length'] extends A
? false
: cnt['length'] extends B
? true
: GET<A, B, [...cnt, 1]>
// A 小于 B
type LT<A extends number, B extends number, cnt extends 1[] = []> =
A extends B
? false
: cnt['length'] extends A
? true
: cnt['length'] extends B
? false
: LT<A, B, [...cnt, 1]>
利用上面这两个工具类,参考解法如下:
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Idx extends 1[] = []
> =
T extends [infer F, ...infer R ]
? GET<Idx['length'], Start> extends true // Idx >= Start
? LT<Idx['length'], End> extends true // Idx < End
? [N, ...Fill<R, N, Start, End, [...Idx, 1]>]
: [F, ...Fill<R, N, Start, End, [...Idx, 1]>]
: [F, ...Fill<R, N, Start, End, [...Idx, 1]>]
: []
Solution by stevenaces #29762
type Fill<T extends any[], E, R extends any[] = []> = R['length'] extends T['length'] ? R :
Fill<T, E, [...R, E]>;
type fill = Fill<[1, 2, 3], 0>;
Solution by sundial-dreams #29545
type GetArr<
N extends number,
Arr extends number[] = []
> = N extends Arr["length"] ? Arr : GetArr<N, [...Arr, 1]>;
type GreaterThanOrEq<
T extends number,
U extends number,
UArr extends number[] = GetArr<U>,
ArrChecker extends number[] = []
> = T extends U
? true
: UArr["length"] extends T
? true
: ArrChecker["length"] extends T
? false
: GreaterThanOrEq<T, U, [...UArr, 1], [...ArrChecker, 1]>;
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T["length"],
R extends unknown[] = [],
Acc extends unknown[] = T
> = R["length"] extends T["length"]
? R
: Acc extends [infer Head, ...infer Tail]
? GreaterThanOrEq<R["length"], Start> extends true
? GreaterThanOrEq<R["length"], End> extends false
? Fill<T, N, Start, End, [...R, N], Tail>
: Fill<T, N, Start, End, [...R, Head], Tail>
: Fill<T, N, Start, End, [...R, Head], Tail>
: R;
Solution by idebbarh #29062
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
_Result extends any[] = [],
_IsFilling extends boolean = false
> =
T extends [infer F, ...infer R] ?
(_IsFilling extends true ?
//开始fill
(_Result[`length`] extends End ?
[..._Result, ...T]/*return*/ : //已结束
Fill<R, N, Start, End, [..._Result, N], true> //filling
) :
//未开始
(_Result[`length`] extends Start ?
Fill<T, N, Start, End, _Result, true> : //开始fill
Fill<R, N, Start, End, [..._Result, F]> //未开始,维持原样
)
) :
_Result/*return*/;
Solution by E-uler #28929
type BuildArr<T, Arr extends unknown[] = []> = Arr["length"] extends T
? Arr
: BuildArr<T, [...Arr, unknown]>;
type GreaterThan<
T,
U,
TArr extends unknown[] = BuildArr<T>,
UArr extends unknown[] = BuildArr<U>
> = TArr extends [...UArr, unknown, ...infer Rest] ? true : false;
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T["length"],
Cur extends unknown[] = [],
Res extends unknown[] = []
> = GreaterThan<End, Start> extends true
? T extends [infer F, ...infer Rest]
? GreaterThan<Start, Cur["length"]> extends true
? Fill<Rest, N, Start, End, [...Cur, unknown], [...Res, F]>
: GreaterThan<End, Cur["length"]> extends true
? Fill<Rest, N, Start, End, [...Cur, unknown], [...Res, N]>
: [...Res, ...T]
: Res
: T;
Solution by DoubleWoodLin #28766
// your answers
// ============= Your Code Here =============
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Run extends any[] = [], // is start fill
FillCount extends any[] = [], // fill count
> = Run['length'] extends Start
? FillCount['length'] extends End
? T
// start fill
: T extends [infer F, ...infer TR]
? [N, ...Fill<TR, N, Start, End, Run, [...FillCount, N]>]
: T
// no fill
: T extends [infer F, ...infer TR]
? [F, ...Fill<TR, N, Start, End, [...Run, F], [...FillCount, F]>]
: [];
Solution by ixiaolong2023 #27850
// 数字转数组
type NumberToArray<T extends number, S extends unknown[] = []> = S['length'] extends T ? S : NumberToArray<T, [...S, 1]>
// 数字减1
type Minus<T extends number> = NumberToArray<T> extends [...infer Pre, 1] ? Pre['length'] : T
// 大于等于
type GreaterThanAndEq<T extends number, S extends number> = T extends 0 ? S extends 0 ? true : false : GreaterThanAndEq<Minus<T>, Minus<S>>
// 大于
type Greater<T extends number, S extends number> = T extends 0 ? false : S extends 0 ? true : Greater<Minus<T>, Minus<S>>
// 是否是never
type IsNever<T> = [T] extends [never] ? true : false
type Fill<T extends readonly unknown[], Type, Start extends number = never, End extends number = never> = {
[key in keyof T]: IsNever<Start> extends true
? Type : key extends `${infer Number extends number}`
? GreaterThanAndEq<Number, Start> extends true
? Greater<End, Number> extends true
? Type
: T[key]
: T[key]
: T[key]
}
Solution by jiechliu #27716
type If<C, T, F> = C extends true ? T : F;
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T["length"],
CountArr extends 0[] = [],
ReachedStart extends boolean = Equal<CountArr["length"], Start>,
ReachedEnd extends boolean = Equal<CountArr["length"], End>,
Acc extends unknown[] = []
> = T extends [infer First, ...infer Rest]
? Fill<
Rest,
N,
Start,
End,
[...CountArr, 0],
If<ReachedStart, true, Equal<[...CountArr, 0]["length"], Start>>,
If<ReachedEnd, true, Equal<[...CountArr, 0]["length"], End>>,
[...Acc, If<ReachedStart, If<ReachedEnd, First, N>, First>]
>
: Acc;
(Note - we could have re-used Acc
instead of CountArr
since they have the same length, but that'd feel hacky since it'd depend on Acc
coincidentally being filled one item at a time)
Solution by alythobani #27608
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T["length"],
T2 extends unknown[] = [],
S extends unknown[] = [],
F extends unknown[] = []
> = S["length"] extends Start | T["length"]
? T extends [...S, ...infer R1]
? [...S,...F]["length"] extends End | T["length"]
? T extends [...S, ...T2, ...infer R2]
? [...S, ...F, ...R2]
: [...S, ...F]
: Fill<T, N, Start, End, [...T2, R1[F["length"]]], S, [...F, N]>
: S
: Fill<T, N, Start, End, T2, [...S, T[S["length"]]], F>;
Solution by smileboyi #27102
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
Past extends unknown[] = [],
Begun extends boolean = Past["length"] extends Start ? true : false
> = T extends [infer First, ...infer Rest] ?
Past["length"] extends End ?
[...Past, ...T]
:
Begun extends true ? Fill<Rest, N, Start, End, [...Past, N], true> : Fill<Rest, N, Start, End, [...Past, First]>
:
Past;
Solution by shhhplus #26840
// Returns number literal equal to A['length'] + 1, used to propagate information if constructed array has len > Start
type ArrLenPlusOne<A extends Array<unknown>> = [...A, 1]['length'] extends number ? [...A, 1]['length'] : never
type Fill<
T extends Array<unknown>,
N,
Start extends number = 0,
End extends number = T['length'],
R extends Array<unknown> = []
> = T extends [infer First, ...infer Rest]
? R['length'] extends Start
? R['length'] extends End
? [...R, ...T]
: Fill<Rest, N, ArrLenPlusOne<R>, End, [...R, N]>
: Fill<Rest, N, Start, End, [...R, First]>
: R
Solution by retkiewi #25915
// your answers
type SliceEnd<T extends any[], Start, Result extends any[] = []> =
Result['length'] extends Start
? Result['length'] extends 0
? T
: Result
: T extends [infer F, ...infer L]
? SliceEnd<L, Start, [...Result, F]>
: []
type SliceStart<T extends any[], Start, Result extends any[] = []> =
Result['length'] extends Start
? Result['length'] extends 0
? T
: Exclude<T, Result>
: T extends [infer F, ...infer L]
? SliceStart<L, Start, [...Result, F]>
: []
type Slice<T extends any[], Start, End> = SliceStart<SliceEnd<T, End>, Start>
type FillArray<T extends any[], N, Result extends any[] = []> =
Result['length'] extends T['length']
? Result
: T extends [infer F, ...infer L]
? [N, ...FillArray<L, N>]
: []
// Fill T with N from Start to End
type Fill<
T extends unknown[],
N,
Start extends number = 0,
End extends number = T['length'],
> =
FillArray<
Slice<T, Start,End>,
N
>
Solution by kiki-zjq #25300