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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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.