08987-medium-subsequence

Back

type Subsequence<T extends any[], R extends any[] = []> = T extends [infer First, ...infer Rest]
  ? Subsequence<Rest, [...R, First]> | Subsequence<Rest, R>
  : R

Solution by 2083335157 #35179

type Subsequence<T extends any[], Prefix extends any[] = []> = T extends [infer F, ...infer Rest]
  ? Subsequence<Rest, Prefix | [...Prefix, F]>
  : Prefix;

Solution by wendao-liu #35093

type Subsequence<T extends any[], K = T[number]> = T extends []
  ? []
  : K extends K
  ?
      | [K, ...Subsequence<T extends [K, ...infer R] ? R : []>]
      | Subsequence<T extends [K, ...infer R] ? R : []>
  : never

Solution by Keith-Web3 #35032

// ้‡‡็”จ | ่ฟžๆŽฅ๏ผŒ่ฟ™ๆ ทๆฏๆฌก้€’ๅฝ’้ƒฝไผš็”Ÿๆˆๅ…ถไธญไธ€้กน
type Subsequence<T extends any[]> = T extends [infer R, ...infer rest] ? [R] | [...Subsequence<rest>] | [R, ...Subsequence<rest>] : T

Solution by ouzexi #34106

type Subsequence<Arr extends any[], SubArr extends any[] = []> =
Arr extends [infer Fisrt, ... infer Rest extends any[]]
    ? SubArr | Subsequence<Rest, [...SubArr] > | Subsequence<Rest, [...SubArr, Fisrt] >
    : SubArr

Solution by sunupupup #33437

// your answers
type Subsequence<T extends any[], U extends any[] = []> = T extends [infer S, ...infer L]
? Subsequence<L,[...U,S]> | Subsequence<L, [...U]> : U;

Solution by pea-sys #33180

type Subsequence<T extends any[], R extends any[] = []> = T extends [infer A, ...infer B] ?
    Subsequence<B, [...R, A]> | Subsequence<B, [...R]> : R;

type subsequence = Subsequence<[1, 2, 3]>;

Solution by sundial-dreams #29561

type Subsequence<T extends any[]> = T extends [infer F, ...infer R]
  ?  [F, ...Subsequence<R>] | [...Subsequence<R>]
  : [];

Solution by DoubleWoodLin #28840

type Subsequence<Items extends unknown[]> = Items extends [
  infer Head,
  ...infer Tail
]
  ?
      | [Head]
      | (Subsequence<Tail> extends infer TailSubsequence extends unknown[]
          ? [Head, ...TailSubsequence] | TailSubsequence
          : never)
  : [];

Solution by yevhenpavliuk #28411

// your answers
type Subsequence<T extends any[]> = T extends [infer F, ...infer Rest] ? [...Subsequence<Rest>] |  [F, ...Subsequence<Rest>] : []

Solution by GreattitJY #27729

// your answers
type Subsequence<T extends any[]> = T extends [infer Left, ...infer Rest] ? [Left] | [Left, ...Subsequence<Rest>] | Subsequence<Rest> : T

Solution by studymachiney #24607

type Subsequence<T extends any[]> = T extends [infer F, ...infer R] ? [F] | [F, ...Subsequence<R>] | Subsequence<R> : [];

Solution by E-uler #24005

// your answers
type Subsequence<T extends any[] > =
  T extends [infer F, ...infer R]
    ? [F, ...Subsequence<R>] | Subsequence<R>
    : []

Solution by snakeUni #23304

// your answers
type Subsequence<T extends unknown[] > =
  T extends [infer A, ...infer Rest]
    ? [A, ...Subsequence<Rest>] | Subsequence<Rest>
    : []

Solution by jxhhdx #22753

type Subsequence<T extends unknown[] > =
  T extends [infer A, ...infer Rest]
    ? [A, ...Subsequence<Rest>] | Subsequence<Rest>
    : []

Solution by drylint #22116

// your answers

type Subsequence<T extends any[] > = T extends [infer first, ...infer rest] ? (
    [first, ...Subsequence<rest>] | Subsequence<rest> 
): []

type test1 = Subsequence<[2]> // [] | [2]
type test2 = [1, ...Subsequence<[2]>] // [1, 2] | [1]

Solution by Quanzhitong #20793

// your answers
type Subsequence<T extends any[]> = T extends [infer F, ...infer R] ? Subsequence<R> | [F, ...Subsequence<R>] : [] 

Solution by YqxLzx #20245

// your answers
type A1 = Subsequence<[1, 2]>; // [] | [1] | [2] | [1, 2]
type Subsequence<Arr> = Arr extends [infer f, ...infer r]
  ? Subsequence<r> | [f, ...Subsequence<r>]
  : [];

Solution by fengjinlong #20103

type Subsequence<T extends any[]> = 
  T extends [infer First, ...infer Rest]
    ? Subsequence<Rest> | [First, ...Subsequence<Rest>]
    : []

่ฟ™้ข˜ไธป่ฆๆ˜ฏๆŠŠๅญ—ไธฒ็š„็ฎ—ๆณ•ๆƒณๆ˜Ž็™ฝ๏ผŒ็ญ”ๆกˆๅฐฑ่‡ช็„ถๆตฎ็Žฐไบ†

Solution by zhaoyao91 #19869

type Subsequence<T extends any[]> = T extends [infer A, ...infer B] 
                                      ? Subsequence<B> | [A, ...Subsequence<B>] : [] 

Solution by kfess #19692

type Shift<T extends unknown[]> = T extends [unknown, ...infer A] ? A : T

type Subsequence<T extends unknown[]> = T['length'] extends 0
  ? T
  : [...([T[0]] | []), ...Subsequence<Shift<T>>]

Solution by theoolee #19524

type Subsequence<T extends any[]> = T extends [infer F, ...infer R] ? Subsequence<R> | [F, ...Subsequence<R>] : [];

Solution by CaoXueLiang #18702

type ArrayExclude<T, U> = T extends [infer F, ...infer R]
  ? F extends U
    ? R
    : [F, ...ArrayExclude<R, U>]
  : []

type Subsequence<T extends any[], U = T[number], I = U> =
  I extends infer N extends any
    ? [] | [N] | Subsequence<ArrayExclude<T, N>> | T
    : []
type Subsequence<T extends any[]> = T extends [infer F, ...infer R]
  ? [F, ...Subsequence<R>] | Subsequence<R>
  : []

Solution by milletlovemouse #18052

type Subsequence<T extends any[]> =
  T extends [infer F, ...infer R]
    ? Subsequence<R> | [F, ...Subsequence<R>]
    : []

Solution by YOUNGmaxer #17894

type ToUnion<T extends unknown[]> = T[number]
type ExcludeItem<T extends unknown[], S, Res extends unknown[] = []> =
  T extends [infer F, ...infer R]
    ? S extends F
      ? ExcludeItem<R, S, Res>
      : ExcludeItem<R, S, [...Res, F]>
    : Res

type Subsequence<T extends any[], U extends any = ToUnion<T>> =
  [U] extends [never]
    ? never
    : U extends U
      ? Subsequence<ExcludeItem<T, U>> extends never
        ? [] | T
        : [] | T | Subsequence<ExcludeItem<T, U>>
      : never

Solution by YOUNGmaxer #17893

type Subsequence<T extends any[]> = T extends [infer r, ...infer rest]
  ? Subsequence<rest> | [r, ...Subsequence<rest>]
  : [];

Solution by cc-hearts #17877

type Subsequence<T extends any[]> = T extends [infer P, ...infer R] ? [...Subsequence<R>] | [P, ...Subsequence<R>] : []

Solution by xjq7 #17217

Question & Answer

type Subsequence<T extends any[]> = T extends [infer Head, ...infer Tail]
  ? [...([Head] | []), ...Subsequence<Tail>]
  : []

Solution by doobee98 #16789

// your answers
type Subsequence<T extends any[], Result extends any[] = []> = T extends [infer R, ...infer U]
                                                                    ? Result | Subsequence<U, Result> | Subsequence<U, [...Result, R]>
                                                                    : Result;

Solution by jiaaoMario #16752

// your answers

type Subsequence<T extends any[], Result extends any[] = []> = T extends [infer F, ...infer R]
  ? Result | Subsequence<R, [...Result, F]> | Subsequence<R, Result>
  : Result

Solution by humandetail #16303