Decoding the Fast & Slow Two Pointer Algorithm to Find Cycles in Javascript Part 3 of 3

  Рет қаралды 1,374

Coderbyte

Coderbyte

Күн бұрын

Пікірлер: 2
@vigneshswamy3058
@vigneshswamy3058 2 жыл бұрын
Very good content learned
@amorfati4559
@amorfati4559 2 жыл бұрын
Here's the naive approach: function hasCycle(arr){ const nodes = {}; let pointer = 0; while(true){ if(arr[pointer + arr[pointer]] < arr.length){ pointer = pointer + arr[pointer]; } else{ pointer = pointer + arr[pointer] - arr.length; } if(arr[pointer] < 0){ let x = 0; for(let i=pointer-1; i>=pointer+arr[pointer]; i--){ x+=arr[i]; } if(x == arr[pointer]*-1){return false;} } if(!nodes[pointer]){ nodes[pointer] = true; } else{ return true; } } } console.log(hasCycle([1, 2, -1, 2, 2])); //true console.log(hasCycle([2, 1, -1, -2])); //false console.log(hasCycle([2, 2, -1, 2])); //true
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
How to Traverse Binary Trees with Queues in Javascript
28:11
Coderbyte
Рет қаралды 1,2 М.
Sliding Window Technique - Algorithmic Mental Models
36:45
Ryan Schachte
Рет қаралды 371 М.
The Mathematician So Strange the FBI Thought He Was a Spy
13:11
Simon Sinek's Advice Will Leave You SPEECHLESS 2.0 (MUST WATCH)
20:43
Alpha Leaders
Рет қаралды 2,4 МЛН
5 Secrets to Stop Stuttering & Speak More Clearly!
12:44
Vinh Giang
Рет қаралды 71 М.
Meta Interview Question - Two Pointers
25:19
Coderbyte
Рет қаралды 454
Step by Step Guide on Rotating LinkedLists in Javascript
25:47
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 849 М.
A* (A Star) Search Algorithm - Computerphile
14:04
Computerphile
Рет қаралды 1,2 МЛН