Skip to main content
fixed typo
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
a =>                        // outer function taking a[]
n =>                        // inner function taking n
(o = [])                    // o[] = array of [ value, group, index ] tuples
.map(a =>                   // once o[] is finalized ...
  a[2],                     //   ... keep only the original indices
  a.map((v, i) => [ v, i ]) //   we first turn a[] into an array of
                            //   [ value, index ] pairs
  .sort(g = ([a], [b]) =>   //   and sort them ...
    b - a                   //     ... from highest to lowest value
  )                         //
  .map(([v, i]) =>          //   for each pair [ value, index ]:
    ( [p, q] = o.sort(g)    //     sort o[] by decreasing 'birth time'
      .filter(a =>          //     and keep only the tuples ...
        a[1].some(j =>      //       ... whose groups contain a value
          (j -= i) * j < 2  //       adjacent to the current value
        )                   //
      ),                    //     save at most 2 tuples that in [ p, q ]
      q = q || [, []],      //     force a default dummy value for q
      p                     //     test p
    ) ?                     //     if p is defined:
      q[                    //
        p[1].push(          //       append to the group of p:
          ...q[1],          //         the values from the group of q
          i                 //         followed by i
        ),                  //
        q[0] -= v,          //       subtract the 'dead time' from the
        1                   //       'birth time' of q
      ] = []                //       and clear the group of q
    :                       //     else:
      o.push([v, [i], i])   //       create a new tuple [ value, group, index ]
  )                         //   end of inner map()
)                           // end of outer map()
.slice(0, n)                // return only the n first elements
a =>                        // outer function taking a[]
n =>                        // inner function taking n
(o = [])                    // o[] = array of [ value, group, index ] tuples
.map(a =>                   // once o[] is finalized ...
  a[2],                     //   ... keep only the original indices
  a.map((v, i) => [ v, i ]) //   we first turn a[] into an array of
                            //   [ value, index ] pairs
  .sort(g = ([a], [b]) =>   //   and sort them ...
    b - a                   //     ... from highest to lowest value
  )                         //
  .map(([v, i]) =>          //   for each pair [ value, index ]:
    ( [p, q] = o.sort(g)    //     sort o[] by decreasing 'birth time'
      .filter(a =>          //     and keep only the tuples ...
        a[1].some(j =>      //       ... whose groups contain a value
          (j -= i) * j < 2  //       adjacent to the current value
        )                   //
      ),                    //     save at most 2 tuples that in [ p, q ]
      q = q || [, []],      //     force a default dummy value for q
      p                     //     test p
    ) ?                     //     if p is defined:
      q[                    //
        p[1].push(          //       append to the group of p:
          ...q[1],          //         the values from the group of q
          i                 //         followed by i
        ),                  //
        q[0] -= v,          //       subtract the 'dead time' from the
        1                   //       'birth time' of q
      ] = []                //       and clear the group of q
    :                       //     else:
      o.push([v, [i], i])   //       create a new tuple [ value, group, index ]
  )                         //   end of inner map()
)                           // end of outer map()
.slice(0, n)                // return only the n first elements
a =>                        // outer function taking a[]
n =>                        // inner function taking n
(o = [])                    // o[] = array of [ value, group, index ] tuples
.map(a =>                   // once o[] is finalized ...
  a[2],                     //   ... keep only the original indices
  a.map((v, i) => [ v, i ]) //   we first turn a[] into an array of
                            //   [ value, index ] pairs
  .sort(g = ([a], [b]) =>   //   and sort them ...
    b - a                   //     ... from highest to lowest value
  )                         //
  .map(([v, i]) =>          //   for each pair [ value, index ]:
    ( [p, q] = o.sort(g)    //     sort o[] by decreasing 'birth time'
      .filter(a =>          //     and keep only the tuples ...
        a[1].some(j =>      //       ... whose groups contain a value
          (j -= i) * j < 2  //       adjacent to the current value
        )                   //
      ),                    //     save at most 2 tuples in [ p, q ]
      q = q || [, []],      //     force a default dummy value for q
      p                     //     test p
    ) ?                     //     if p is defined:
      q[                    //
        p[1].push(          //       append to the group of p:
          ...q[1],          //         the values from the group of q
          i                 //         followed by i
        ),                  //
        q[0] -= v,          //       subtract the 'dead time' from the
        1                   //       'birth time' of q
      ] = []                //       and clear the group of q
    :                       //     else:
      o.push([v, [i], i])   //       create a new tuple
  )                         //   end of inner map()
)                           // end of outer map()
.slice(0, n)                // return only the n first elements
added a commented version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

Commented

a =>                        // outer function taking a[]
n =>                        // inner function taking n
(o = [])                    // o[] = array of [ value, group, index ] tuples
.map(a =>                   // once o[] is finalized ...
  a[2],                     //   ... keep only the original indices
  a.map((v, i) => [ v, i ]) //   we first turn a[] into an array of
                            //   [ value, index ] pairs
  .sort(g = ([a], [b]) =>   //   and sort them ...
    b - a                   //     ... from highest to lowest value
  )                         //
  .map(([v, i]) =>          //   for each pair [ value, index ]:
    ( [p, q] = o.sort(g)    //     sort o[] by decreasing 'birth time'
      .filter(a =>          //     and keep only the tuples ...
        a[1].some(j =>      //       ... whose groups contain a value
          (j -= i) * j < 2  //       adjacent to the current value
        )                   //
      ),                    //     save at most 2 tuples that in [ p, q ]
      q = q || [, []],      //     force a default dummy value for q
      p                     //     test p
    ) ?                     //     if p is defined:
      q[                    //
        p[1].push(          //       append to the group of p:
          ...q[1],          //         the values from the group of q
          i                 //         followed by i
        ),                  //
        q[0] -= v,          //       subtract the 'dead time' from the
        1                   //       'birth time' of q
      ] = []                //       and clear the group of q
    :                       //     else:
      o.push([v, [i], i])   //       create a new tuple [ value, group, index ]
  )                         //   end of inner map()
)                           // end of outer map()
.slice(0, n)                // return only the n first elements

Commented

a =>                        // outer function taking a[]
n =>                        // inner function taking n
(o = [])                    // o[] = array of [ value, group, index ] tuples
.map(a =>                   // once o[] is finalized ...
  a[2],                     //   ... keep only the original indices
  a.map((v, i) => [ v, i ]) //   we first turn a[] into an array of
                            //   [ value, index ] pairs
  .sort(g = ([a], [b]) =>   //   and sort them ...
    b - a                   //     ... from highest to lowest value
  )                         //
  .map(([v, i]) =>          //   for each pair [ value, index ]:
    ( [p, q] = o.sort(g)    //     sort o[] by decreasing 'birth time'
      .filter(a =>          //     and keep only the tuples ...
        a[1].some(j =>      //       ... whose groups contain a value
          (j -= i) * j < 2  //       adjacent to the current value
        )                   //
      ),                    //     save at most 2 tuples that in [ p, q ]
      q = q || [, []],      //     force a default dummy value for q
      p                     //     test p
    ) ?                     //     if p is defined:
      q[                    //
        p[1].push(          //       append to the group of p:
          ...q[1],          //         the values from the group of q
          i                 //         followed by i
        ),                  //
        q[0] -= v,          //       subtract the 'dead time' from the
        1                   //       'birth time' of q
      ] = []                //       and clear the group of q
    :                       //     else:
      o.push([v, [i], i])   //       create a new tuple [ value, group, index ]
  )                         //   end of inner map()
)                           // end of outer map()
.slice(0, n)                // return only the n first elements
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

JavaScript (ES10*), 215 bytes

* Only because we needES10 guarantees a stable sort, which is guaranteedbut the OP has since ES10clarified that this is not a strict requirement.

(or 214 bytes in ES12, but ATO doesn't seem to be working right now)

Expects (array)(n).

a=>n=>(o=[]).map(a=>a[2],a.map((v,i)=>[v,i]).sort(g=([a],[b])=>b-a).map(([v,i])=>([p,q]=o.sort(g).filter(a=>a[1].some(j=>(j-=i)*j<2)),q=q||[,[]],p)?q[p[1].push(...q[1],i),q[0]-=v,1]=[]:o.push([v,[i],i]))).slice(0,n)

Try it online!

JavaScript (ES10*), 215 bytes

* Only because we need a stable sort, which is guaranteed since ES10

(or 214 bytes in ES12, but ATO doesn't seem to be working right now)

Expects (array)(n).

a=>n=>(o=[]).map(a=>a[2],a.map((v,i)=>[v,i]).sort(g=([a],[b])=>b-a).map(([v,i])=>([p,q]=o.sort(g).filter(a=>a[1].some(j=>(j-=i)*j<2)),q=q||[,[]],p)?q[p[1].push(...q[1],i),q[0]-=v,1]=[]:o.push([v,[i],i]))).slice(0,n)

Try it online!

JavaScript (ES10*), 215 bytes

* ES10 guarantees a stable sort, but the OP has since clarified that this is not a strict requirement.

(or 214 bytes in ES12, but ATO doesn't seem to be working right now)

Expects (array)(n).

a=>n=>(o=[]).map(a=>a[2],a.map((v,i)=>[v,i]).sort(g=([a],[b])=>b-a).map(([v,i])=>([p,q]=o.sort(g).filter(a=>a[1].some(j=>(j-=i)*j<2)),q=q||[,[]],p)?q[p[1].push(...q[1],i),q[0]-=v,1]=[]:o.push([v,[i],i]))).slice(0,n)

Try it online!

minor clarification
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added a link to an ES12 version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading