Show newer

@murz 5, because I ran a random number generator from 3-5 and it picked 5

@aschmitz vaporeon showed me a snippet of code from unix source that used the variable "gorp" and i thought it was funny so now i use it

@vaporeon_ it's even worse than you think! javascript has what is effectively a preprocessing phase where it looks for every single value initialized as a var and initializes them, and then the interpreter runs on the code like you would expect

this means you can use a variable before it is declared if you declare it with var! real javascript code out in the wild actually does this!!!!

@vaporeon_ @aescling no clue i just think the idea of storing project versions on laserdisc funny

@aescling @vaporeon_ with let the algo ran the test cases ~700ms and with var it shaved ~100ms

@vaporeon_ oooooh I could have done for (var min = Infinity, j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;

javascript variables declared with var and not block scoped, so min is available outside of the for loop teehee

@vaporeon_ here is another hack with a "beautiful" one liner:

Problem Statement
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

  • 0 <= j <= nums[i] and
  • i + j < n

Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Example

  • Input: nums = [2,3,1,1,4]
  • Output: 2
  • Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Code

const jump = (nums, i = 0, gorp = {}) => {
if (i === nums.length - 1) return 0;
if (i > nums.length - 1 || nums[i] === 0) return Infinity;
if (gorp[i] !== undefined) return gorp[i];

let min = Infinity;
for (let j = 1, res; j <= nums[i]; ++j) res = 1 + jump(nums, i + j, gorp), min = res < min ? res : min;

return gorp[i] = min;
};

@vaporeon_ you store all versions of your software on laserdisc

my greatest character flaw is that there is at least one limp bizkit song that i like

Show older
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.