I believe that no strategy of that type can possibly work. Here's an example where a number 10 in changes whether it is better to pop or shift first:
0.631752274168395 0.502325774658843 0.0553136679912676 0.696650395168113 0.904058789590099 0.83366887317402 0.676563261504992 0.620510190454731 0.835401306610937 0.331025798953092 100 0.460845097973213 0.0150072228842113 0.0863915966693014 0.252652201491809 0.0344964741543734 0.277799160386071 0.358041640281542 0.984148718850204 0.846530682637557
(With the 100 there, you should shift, remove it and you should pop.)

Here is the program that I wrote to find that strategy:

#! /usr/bin/perl -w use strict; my $n = shift || 4; my $tries = shift || 4**$n; for (1..$tries) { my @num = map rand, 1..$n, 1..$n; $num[$n] = 0; my $iter = get_move_iter(@num); my ($move) = $iter->(); $num[$n] = 100; $iter = get_move_iter(@num); my ($move2) = $iter->(); if ($move eq $move2) { print "Try $_ was same\n"; } else { print "Got $move vs $move2:\n@num\n"; last; } } # Scores is a 2-dim array, $scores[$i][$j] is the best score # that you can get given a string of $j numbers starting at # $numbers[$i]. sub get_scores { my @numbers = @_; my @scores; for (0..$#numbers) { $scores[$_] = [0, $numbers[$_]]; } for my $j (2..@numbers) { for my $i (0..(@numbers - $j)) { my $a = $numbers[$i] - $scores[$i+1][$j-1]; my $b = $numbers[$i+$j-1] - $scores[$i][$j-1]; $scores[$i][$j] = ($a > $b) ? $a : $b; } } return @scores; } sub get_move_iter { my @n = @_; my @scores = get_scores(@n); my $i = 0; my $j = $#n; return sub { if ($j < 0) { return; } elsif ($n[$i] - $scores[$i+1][$j] > $n[$i+$j] - $scores[$i][$j]) { $j--; return "shift", $n[$i++]; } else { return "pop", $n[$i + $j--]; } }; }

In reply to Re^2: Puzzle: Given an array of integers, find the best sequence of pop / shift... by tilly
in thread Puzzle: Given an array of integers, find the best sequence of pop / shift... by TedPride

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.