Ad
  • Custom User Avatar

    You're right. Thanks!

  • Default User Avatar

    Shouldn't that be a x instead of a y in the 2nd line?

  • Default User Avatar

    similar to what floydchen said, the ENTIRE PAIR 3 and 7 comes before the second value of 5,5 so it's counted as earlier

  • Default User Avatar

    You should look at the second index that completes the pair. Whichever pair has "earlier" closing index should be the answer.

    In this case, the second index of (3, 7) is 4, which comes before 5, the second index of (5, 5).

  • Custom User Avatar

    Nice solution! As I was trying to wrap my head around it though, it seems to hit the stack size limit on node 0.12.1 for the last test case, the 5x5 map.

    var map = [[true, true, true, false, true],
        [false, false, true, false, true],
        [true, true, true, true, true],
        [true, false, true, false, false],
        [false, true, true, true, true]];
    
    console.log(solve(map, {x:0,y:0}, {x:4,y:4}));
    

    Output:

    (null):0
    (null)
    
    RangeError: Maximum call stack size exceeded
    
  • Custom User Avatar

    I like that you added the decoded string to the key itself. I kept the decoded string in a separate string so I could access it within the map/forEach. This is a much better approach. Nicely done!

  • Custom User Avatar

    Makes sense. Thanks @surtich!

  • Custom User Avatar

    Because you can redo all undo actions:

    unRe.set('x', 10);
    unRe.set('x', 100);
    unRe.set('x', 150);
    unRe.set('x', 50);
    unRe.undo(); //undo the last action. Now x = 150
    unRe.undo(); //undo the action before the last action. Now x = 100
    unRe.redo(); //redo the last undo action. Now x = 150
    unRe.redo(); //redo the undo action before the last undo action. Now x = 100
    
  • Custom User Avatar

    I don't understand how the pair [3, 7] is earlier in this example:

    sum_pairs([10, 5, 2, 3, 7, 5],         10)
    #              ^-----------^   5 + 5 = 10, indices: 1, 5
    #                    ^--^      3 + 7 = 10, indices: 3, 4 *
    #  * entire pair is earlier, and therefore is the correct answer
    == [3, 7]
    

    If the 5 occurs at index 1, why isn't it "earlier" than the 3 at index 3? Aren't we reading from the left?

  • Custom User Avatar

    The description states that "redo is only possible after an undo". I interpret that to mean that Redo will only work when the immediate last action was an undo. For example, if I do set(), undo(), and then redo(), my last action is a redo(), and so I should not be able to redo(), since my last action is not an undo().

    If that's correct, then why do I see in the provided test cases the following:

      unRe.redo();
      unRe.redo();
      unRe.redo();
      unRe.redo();
    

    Shouldn't this throw an exception?

  • Custom User Avatar

    Yeah, plus creating a copy of the array costs memory and cycles.

  • Custom User Avatar

    TIL you can have multiple declarations separated by comma in a for loop expression (e.g. i++, j--;). Neat!