Two Sum
提出詳細
type NumToArr<N extends number, Acc extends readonly unknown[]=[]> = Acc["length"] extends N ? Acc : NumToArr<N,[...Acc,0]> type Sum<N extends number, M extends number> = [...NumToArr<N>, ...NumToArr<M>]["length"] type ArrSum<T extends readonly number[]> = T extends [infer T1 extends number, ...infer T2 extends number[]] ? Sum<T1,ArrSum<T2>> : 0 type PickTwo<T extends unknown[], Acc extends readonly unknown[]=[]> = Acc["length"] extends 2 ? Acc : T extends [infer T1, ...infer T2] ? PickTwo<T2,[...Acc, T1]> | PickTwo<T2,Acc> : never type TwoSumList<T extends number[], P extends readonly number[]=PickTwo<T>> = P extends P ? ArrSum<P> : never type TwoSum<T extends number[], U extends number, Lst extends number=TwoSumList<T>> = true extends (Lst extends U ? true : never) ? true : false
提出日時 | 2023-09-19 05:43:57 |
---|---|
問題 | Two Sum |
ユーザー | sankantsu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<TwoSum<[3, 3], 6>, true>>, Expect<Equal<TwoSum<[3, 2, 4], 6>, true>>, Expect<Equal<TwoSum<[2, 7, 11, 15], 15>, false>>, Expect<Equal<TwoSum<[2, 7, 11, 15], 9>, true>>, Expect<Equal<TwoSum<[1, 2, 3], 0>, false>>, Expect<Equal<TwoSum<[1, 2, 3], 1>, false>>, Expect<Equal<TwoSum<[1, 2, 3], 2>, false>>, Expect<Equal<TwoSum<[1, 2, 3], 3>, true>>, Expect<Equal<TwoSum<[1, 2, 3], 4>, true>>, Expect<Equal<TwoSum<[1, 2, 3], 5>, true>>, Expect<Equal<TwoSum<[1, 2, 3], 6>, false>>, ]