@aescling @vaporeon_ with let
the algo ran the test cases ~700ms and with var
it shaved ~100ms
@aescling @vaporeon_ ....it runs faster with this change
@soft PREACH
@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
@aescling @vaporeon_ I just ran it and what is here is correct
@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]
andi + 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
[2,3,1,1,4]
2
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;
};
@coriander @wohali I was gonna write this comment too, lol
@onfy whoa new pfp
@vaporeon_ you store all versions of your software on laserdisc
secret do not open
@coriander break stuff
@coriander that phrase immediately filled with me with ick so im glad you think they are bad
@coriander they didn't sound good from the wikipedia description but i didn't say anything in case you liked them
@coriander my parents obsessively policed every kind of art I consumed until I was in my last year or two of high school so I have gaps in my knowledge media from the early 2000s that wasn't family entertainment
@coriander what genre are they
@kit yes im glad someone else understands
@aschmitz @aescling @vaporeon_ I once used a JavaScript array as a queue, using the shift
method to pluck elements from the start of the array. Of course, this means iterating over an entire "queue" should be slow, since every call to shift
causes every element in the array to be reassigned and reduces the size of the array by 1. I benchmarked my algorithm and it was slower than I hoped, so I immediately worked on the queue as I thought it was a throttle for the algorithm.
I made an object that effectively had a JavaScript array as a private variable and created a method for "removing" the first element from the array. In reality, there was pointer being tracked by the object that simply shifted to the right by 1, simulating the behavior of a queue with an ordinary array. Obviously iterating over this structure should be O(n).
To my surprise, when I benchmarked the algorithm again, it was slower. Evidently, with my first approach, the JIT engine realized I was using an array as a queue and secretly optimized it for me. Without realizing, I tried to outsmart the JIT engine, and lost.
kept afloat by big ideas.
videogame enjoyer. mathematics hobbyist and recovering physicist. software engineer. professional wonk. certified weird movie liker. top-ranked c++ hater. prophet of The Truth. space dandy and kill la kill propagandist. the walking embodiment of "not diagnosed, but somethings wrong". i like animals that wear cowboy hats.
I am not picky about names. Most people here call me catwin, clodboy, clodsire, or Caleb.
pfp is by @The_T
header is by @vaporeon_
"i regret ever allowing him here" ~aescling
"i know your taste in movies well enough to discount your opinion" ~globin
he/him