type CreateArray<T extends number, K extends any[] = []> = K['length'] extends T
? K
: CreateArray<T, [any, ...K]>
type CreateTriangular<N extends number> = [
...CreateArray<N>,
...(CreateArray<N> extends [any, ...infer R]
? CreateTriangular<R['length']>
: [])
]
type Triangular<N extends number> = CreateTriangular<N>['length']
Solution by Keith-Web3 #34848
type Triangular<N extends number, U extends 0[] = [], R extends 0[] = []> = U['length'] extends N ? R['length'] : Triangular<N, [...U, 0], [...R, ...U, 0]>
Solution by ouzexi #34153
type Triangular<
N extends number,
Current extends 0[] = [],
Prev extends 0[] = [],
> =
Current['length'] extends N
? Prev['length']
: Triangular<N, [...Current, 0], [...Current, ...Prev, 0]>
Solution by drylint #34135
type Triangular<
N extends number,
Temp extends any[] = [],
Ret extends any[] = []
> = N extends Temp["length"]
? Ret["length"]
: Triangular<N, [...Temp, any], [...Ret, ...Temp, any]>;
Solution by sunupupup #33485
type Triangular<N extends number, Count extends 1[] = [], Result extends 1[] = []>
= Count['length'] extends N ? Result['length']
: Triangular<N, [...Count, 1], [...Result, ...Count, 1]>
Solution by teamchong #33002
type GenerateArray<N extends number, Acc extends number[] = []> = Acc['length'] extends N
? Acc
: GenerateArray<N, [...Acc, Acc['length']]>
type Flatten<T extends unknown[], A extends unknown[] = []> = T extends [] ? A : (T extends [infer F, ...infer R] ? (F extends unknown[] ? Flatten<[...F, ...R], A> : Flatten<R, [...A, F]>): never);
type Triangular<N extends number, Acc extends unknown[] = []> = Acc['length'] extends N
? Flatten<[Acc, GenerateArray<Acc['length']>]>['length']
: Triangular<N, [...Acc, GenerateArray<Acc['length']>]>
Solution by keyurparalkar #32819
type Triangular<
N extends number,
Count extends any[] = [],
Total extends any[] = []
> = Equal<number, N> extends true
? never
: N extends Count["length"]
? Total["length"]
: Triangular<N, [...Count, ""], [...Total, ...Count, ""]>;
Solution by Vampirelee #32578
type Triangular<
N extends number,
T extends any[] = [],
U extends any[] = []
> = T["length"] extends N
? U["length"]
: Triangular<N, [...T, any], [...U, ...T, any]>;
Solution by moonpoet #31045
type Triangular<
N extends number,
U extends unknown[] = [],
R extends unknown[] = []
> = U['length'] extends N
? R['length']
: Triangular<N, [...U, unknown], [...R, ...U, unknown]>
Solution by milletlovemouse #30990
type Triangular<N extends number, U extends any[] = [], O extends any[] = []> = U['length'] extends N ? [...O, ...U]['length'] : Triangular<N, [...U, 1], [...O, ...U]>
Solution by dreamluo-plus #30699
type Triangular<
N extends number,
Count extends number[] = [],
Res extends number[] = []
> = Count["length"] extends N
? [...Res, ...Count]["length"]
: Triangular<N, [...Count, 1], [...Res, ...Count]>;
Solution by DoubleWoodLin #28918
type BuildArry<Length extends number,
Arr extends unknown[] = []> = Arr['length'] extends Length
? Arr
: BuildArry<Length, [...Arr, unknown]>
type sub<n1 extends number, n2 extends number> = BuildArry
type Triangular<N extends number, Arr extends unknown[] = []> = N extends 0
? Arr['length']
: Triangular<sub<N, 1>, [...Arr, ...BuildArry
Solution by kekekgwj #28858
type CartesianProduct<T, U> = T extends T ? U extends U ? [T, U] : never : never
Solution by kekekgwj #28855
计算的题目基本上都用数组来实现,利用数组的 length 求和,这题相当于需要把每个数转成对应长度的数组,例如 3,需要转成 1,2,3 这三个数字对应长度的数组
1: ['']
2: ['', ''],
3: ['', '', ''],
递归创建即可
type CountArr<N extends number, R extends string[] = []> = R['length'] extends N
? R
: CountArr<N, [...R, '']>
type Triangular<N extends number, R extends any[] = [], Count extends string[] = []> = Count['length'] extends N
? R['length']
: Triangular<N, [...CountArr<[...Count, ""]['length']>, ...R], [...Count, ""]>
Solution by linjunc #28553
type Triangular<N extends number, _Carriage extends 1[][] = [], _Passenger extends 1[] = [], _Next extends 1[] = _Carriage extends [...any, infer L extends 1[]] ? [...L, 1] : [1]> = _Carriage[`length`] extends N ?
_Passenger[`length`] :
Triangular<N, [..._Carriage, _Next], [..._Passenger, ..._Next]>;
Solution by E-uler #28099
The method is derived from Sobes76rus's solution
type Reverse<S extends string> = S extends `${infer F}${infer R}`
? `${Reverse<R>}${F}`
: S;
type NextDigit = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
type AddOne<S extends string> = S extends `${infer F extends number}${infer R}`
? F extends 9
? `0${AddOne<R>}`
: `${NextDigit[F]}${R}`
: `1`;
type AddN<
N extends number,
Sum extends string,
I extends 1[] = []
> = I['length'] extends N ? Sum : AddN<N, AddOne<Sum>, [...I, 1]>;
type Triangular<
N extends number,
I extends 1[] = [],
R extends string = '0'
> = I['length'] extends N
? Reverse<R> extends `${infer RN extends number}`
? RN
: never
: Triangular<N, [...I, 1], AddN<[...I, 1]['length'] & number, R>>;
Solution by JohnLi1999 #28019