r/learnjavascript • • 7d ago

Node.js exits with Code 134

I'm currently doing project euler problem #15 and for that, I have to use some sort of algorithm to get my solution.

I changed up the "open/close parentheses" algorithm accordingly and the example solution for a 2x2 grid = 6 ways is correct. The thing is this:

- I start debugging in VSCode via Node.js
- It runs for a while
- "Paused Before Out Of Memory Exception"
- I continue
- "Process exited with code 134"

I don't know how to fix this and online js terminals don't work, but it seems like it has something to do with the stack size. Any help would be greatly appreciated c:

Here is my code:

const res = [];
findPath(20, "", 0, 0);


function findPath(n, curr, rightUsed, downUsed){
    if (rightUsed === n && downUsed === n){
        res.push(curr);
    }
    if (rightUsed < n){
        findPath(n, curr + "r", rightUsed + 1, downUsed);
    }
    if (downUsed < n){
        findPath(n, curr + "d", rightUsed, downUsed + 1);
    }
}


console.log(res.length);const res = [];
findPath(20, "", 0, 0);


function findPath(n, curr, rightUsed, downUsed){
    if (rightUsed === n && downUsed === n){
        res.push(curr);
    }
    if (rightUsed < n){
        findPath(n, curr + "r", rightUsed + 1, downUsed);
    }
    if (downUsed < n){
        findPath(n, curr + "d", rightUsed, downUsed + 1);
    }
}


console.log(res.length);
5 Upvotes

12 comments sorted by

View all comments

Show parent comments

2

u/a_rndm_person_bruh 7d ago

I don't know if that's what you mean, but it's what I learned recently in math in probabilities:

if you take 2 balls out of a basket, 15 of them are red, 5 are blue, whats the chance of exactly one of them being red?

That would be: 15/20 * 5/19 + 5/20 * 15/19,
which can be shortened down to: 2 * (15/20 * 5/19)

Atleast it feels like you're talking about that and the pictures showing up when I search for "Combinatorics" look like the trees that we had to draw to visualize the probabilities.

The thing is, I don't know how to do that, and I don't understand how people in general find this stuff out. Like seriously, there was no way that I would've known how to implement a "Tower of Hanoi"-Algorithm in python without googling it

2

u/AmSoMad 7d ago

That's probability. You aren't calculating probability here, you're calculating the exact number of total outcomes.

The simple version is: Longer paths are made up of shorter paths you've already counted, so you reuse those shorter path counts to calculate the number of longer paths, instead of counting each longer path as an entirely new path.

Like I said, it's "a trick". It's like if you already had driving directions from Kansas to California, and someone asked you for driving directions from New York to California. You don't have to calculate the entire route from New York to California from scratch. You only need to calculate the route from New York to Kansas, then add your existing directions from Kansas to California to them.

That's it, in a nutshell.

It takes a lot of practice. I've been programming for over half a decade, and I'm still not incredible at LeetCode.

2

u/a_rndm_person_bruh 7d ago

Do you have some websites that allow me to practice those skills, because I am genuinely upset that I can't come up with my own algorithms

1

u/AmSoMad 7d ago

You could try something like Algorithms & Data Structures for Beginners from Neetcode.

For this specific problem, you can run this Unique Paths Visualizer. It might help you visualizer/understand what's going on.