Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

deleted 1072 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (ES6), 83 bytes

a=>b=>a.reduce((r,_,i)=>b.every(x=>j=a.indexOf(x,j)+1,j=i)/r[j-i]?a.slice(i,j):r,a)

Try it online!

Combine my original solution with Arnauld 's indexOf trick . I finally got this 83 bytes version.

This answer is also \$O(M^2)\$ time complexity and \$O(N)\$ extra spaces.


Original post:

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 83 bytes

a=>b=>a.reduce((r,_,i)=>b.every(x=>j=a.indexOf(x,j)+1,j=i)/r[j-i]?a.slice(i,j):r,a)

Try it online!

Combine my original solution with Arnauld 's indexOf trick . I finally got this 83 bytes version.

This answer is also \$O(M^2)\$ time complexity and \$O(N)\$ extra spaces.


Original post:

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

added 964 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (ES6), 83 bytes

a=>b=>a.reduce((r,_,i)=>b.every(x=>j=a.indexOf(x,j)+1,j=i)/r[j-i]?a.slice(i,j):r,a)

Try it online!

Combine my original solution with Arnauld 's indexOf trick . I finally got this 83 bytes version.

This answer is also \$O(M^2)\$ time complexity and \$O(N)\$ extra spaces.


Original post:

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 83 bytes

a=>b=>a.reduce((r,_,i)=>b.every(x=>j=a.indexOf(x,j)+1,j=i)/r[j-i]?a.slice(i,j):r,a)

Try it online!

Combine my original solution with Arnauld 's indexOf trick . I finally got this 83 bytes version.

This answer is also \$O(M^2)\$ time complexity and \$O(N)\$ extra spaces.


Original post:

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

deleted 5 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M\cdot N\right) \$\$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M\cdot N\right) \$ time and \$ O\left(N\right) \$ extra memory.

JavaScript (ES6), 93 bytes

a=>b=>a.reduce(r=>1/r[p=b.map((m,j)=>t=m-a[i]?p[j]:j?p[j-1]:i),++i-t]?a.slice(t,i):r,a,i=p=0)

Try it online!

For inputs \$ a\left[1\dots N\right] \$ and \$ b\left[1\dots M\right] \$

Let matrix \$p_{N\times M}\$ be

$$ p_{i,j}=\begin{cases} i & b_j=a_i \land j=1 \\ p_{i-1,j-1} & b_j=a_i \land j>1 \land i>1 \\ p_{i-1,j} & b_j\ne a_i \land i > 1 \\ -\infty & \text{otherwise} \\ \end{cases} $$

the minimal length of output \$L\$ is

$$ L = \min_{i \in 1\dots N} i-p_{i,M}+1 $$

And the output is

$$ a\left[p_{i,M}\dots i\right] $$

where \$i\$ is the value for minimal \$L\$.

This solution works in \$ O\left(M^2\right) \$ time and \$ O\left(N\right) \$ extra memory.

added 2 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
added 100 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
edited body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
added 1 character in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
added 1 character in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
deleted 18 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133
Loading

AltStyle によって変換されたページ (->オリジナル) /