Noob Recursive Backtracker vs Dynamic Programming Tabulator

Sdílet
Vložit
  • čas přidán 6. 05. 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

Komentáře • 380

  • @dominikschweigl574
    @dominikschweigl574 Před 2 měsíci +1717

    God tier wouldn't store the whole array which makes it O(n) space complexity. Instead only store previous two for O(1) space.

    • @weissgal1977
      @weissgal1977 Před 2 měsíci +35

      Exactly my first though

    • @rikthecuber
      @rikthecuber Před 2 měsíci +175

      Technically, god-tier would use matrix exponentiation to get it in O(log n) time and O(1) space.

    • @GregHogg
      @GregHogg  Před 2 měsíci +143

      Absolutely correct:)

    • @TheJanstyler
      @TheJanstyler Před 2 měsíci +20

      Is that really god tier? I'm a first year cs student and thought of that. Kinda feels like the most intuitive way to do it imo.

    • @dominikschweigl574
      @dominikschweigl574 Před 2 měsíci +2

      @@rikthecuber can you explain? Never heard of it

  • @kevinchandra280
    @kevinchandra280 Před 2 měsíci +573

    Mathematician: "Well, I'll just use the Binet's Formula."

    • @mattiamarchese6316
      @mattiamarchese6316 Před měsícem +44

      It's a huge meme among the high school students attending LUISS lessons about competitive programming, each year the "know it all" ends up saying "I can get fib(N) in almost O(1)"

    • @z69b
      @z69b Před měsícem +5

      that still needs loops for the pow

    • @user-yr7ck5ed7w
      @user-yr7ck5ed7w Před měsícem

      I thought about it too 😮

    • @zoetorres1894
      @zoetorres1894 Před měsícem

      ​@@mattiamarchese6316it's closer to log n tho due to it being the fastest you can get the exponent

    • @vilmospalik1480
      @vilmospalik1480 Před měsícem

      Exactly what I was thinking

  • @user-ip1rx2vs6j
    @user-ip1rx2vs6j Před 2 měsíci +361

    Only 2 variables are needed. Overkill for using array😅

    • @GregHogg
      @GregHogg  Před 2 měsíci +26

      Yep, you're right!

    • @nonconsensualopinion
      @nonconsensualopinion Před 2 měsíci

      Even then, if you're going to use an array, at least preallocate it.

    • @joshnjoshgaming
      @joshnjoshgaming Před 29 dny +2

      Technically 3 for a swap, no?

    • @Kitsuinox
      @Kitsuinox Před 29 dny +1

      ​@@joshnjoshgaming you can also use the xor operator to swap two numbers without having to introduce another variable. will use more cycles, though

    • @user-ip1rx2vs6j
      @user-ip1rx2vs6j Před 29 dny

      @@joshnjoshgaming There's a "slightly" more elegant way for using 2 variables only. It's a bit useless memory-wise tbh, but good for thinking.

  • @xevento8682
    @xevento8682 Před 2 měsíci +335

    Can't get over the fact that the "God-Tier" Programmer is concerned about time & space complexity and speed and then chooses to use Python of all languages lol

    • @GregHogg
      @GregHogg  Před 2 měsíci +56

      God tier python programmer I suppose 😂

    • @_Epidemic_
      @_Epidemic_ Před 2 měsíci +11

      At least it isn’t Java or JavaScript

    • @xevento8682
      @xevento8682 Před 2 měsíci +13

      @@_Epidemic_ still faster than python most of the time but yeah, I'd have expected like c++

    • @808brotherstv4
      @808brotherstv4 Před 2 měsíci +2

      Just for fibonacci ?? Why not use a memoized recusive function and reduse speed from exponential to linear🤔🤔🤔

    • @xevento8682
      @xevento8682 Před 2 měsíci +1

      @@808brotherstv4 think he does that in another video actually. though it wouldn't surprise me if that was also in python.

  • @lucasct1426
    @lucasct1426 Před 2 měsíci +21

    For the Fibonacci sequence, if you just need a specific number of it, you can use matrix exponentiation and calculated really large indices very quickly

    • @GregHogg
      @GregHogg  Před 2 měsíci +1

      Yes absolutely right

    • @MengdaYang
      @MengdaYang Před 2 měsíci

      is there a word in english for the god above god?

    • @MrMeztar
      @MrMeztar Před 14 dny

      @@MengdaYang Yes, Computer Science Major :P

  • @flamendless
    @flamendless Před měsícem +34

    "god-tier" then proceeds to store a big list. And in python. Lol

  • @addone9871
    @addone9871 Před 2 měsíci +32

    @GregHogg You can also represent linear sequence with a matrix and use fast exponantiation to get O(logn) time complexity with O(1) space complexity

    • @kjl3080
      @kjl3080 Před měsícem +1

      Binet Eigenvalues my beloved

    • @robosapiens-yd1hb
      @robosapiens-yd1hb Před měsícem

      @@kjl3080 idk what that is. I solved it by manually deriving an f(2n) and f(2n-1) function in terms of f(n) and f(n-1) then just cached the immediate previous states. This resulted in O(log(n)) time and O(1) space.
      code:
      "
      def fib(self, n: int) -> int:
      f0 , f1, r0, r1, i = 1, 1, 0, 1, 1
      while i

  • @kintr8597
    @kintr8597 Před 2 měsíci +104

    The most optimal is literally just using the formula: (((1+sqrt(5))/2)^n-((1-sqrt(5))/2)^n)/sqrt(5)

    • @markusklyver6277
      @markusklyver6277 Před 2 měsíci +18

      "I use Binet's formula, btw"

    • @gabrielushijima1525
      @gabrielushijima1525 Před 2 měsíci +7

      Loses precision for big n

    • @aertyty3900
      @aertyty3900 Před 2 měsíci +8

      ​@@gabrielushijima1525do calculations in Q(√5)

    • @padraighill4558
      @padraighill4558 Před měsícem +4

      @@aertyty3900 Just Q[x] / (x^2 + x - 1)

    • @aertyty3900
      @aertyty3900 Před měsícem +2

      @@padraighill4558 x^2-x-1 *nerd*
      tho u dont even need generating functions or characteristics polynomials, because Q(sqrt(5)) is kinda intuitive due to similarity with complex numbers. Or just use matrices, it works just the same. But its better to use polynomials division for longer linear recurrences, cuz O(k^2logn) is better than O(k^3logn). I just like the idea of using Q(sqrt(5)), it can mathematically prove some very fun properties

  • @raymaleh8851
    @raymaleh8851 Před 2 měsíci +8

    While I agree that storing the entire array is not needed for a single call to this function, if we make the array static (reusable between function calls), we effectively get a cache. At any point, if we have solved the problem up to size M and n M, we just append to our cache until we solve for n, making n the new largest solved problem. Assuming that the inputs follow some reasonable probability distribution without too heavy of a tail and are uncorrelated, then the runtime should converge to an average of O(1).

  • @adityasoni8924
    @adityasoni8924 Před 2 měsíci +67

    It's funny that my uni professor solved the fib question with recursion and I solved it with iteration on the test and I got a 0 for it.
    I talked with him, googled in the front of him to prove that it can be solved with a loop to get my marks on test.

    • @karstr770
      @karstr770 Před 2 měsíci +50

      Any recursive problem can be solved with a loop, this is programming 101 and does not need proving. Some problems are just more suited for recursive solutions.
      Maybe the requirement was to use a recursive approach?

    • @user-ip9dg4dt5e
      @user-ip9dg4dt5e Před 2 měsíci +8

      Considering it's uni, the test probably meant to test certain subject and not so much on the result

    • @weissgal1977
      @weissgal1977 Před 2 měsíci +9

      Ask your professor if his implementation can do fib(100),
      Spoiler: it can't, unless it doing some internal cache

    • @GregHogg
      @GregHogg  Před 2 měsíci +1

      Wow that's brutal

    • @Scarybug
      @Scarybug Před 2 měsíci +14

      First year programming is learning how to use recursion, second year programming is learning never to use recursion.

  • @chancefleeting6479
    @chancefleeting6479 Před měsícem +5

    This is solvable in closed form…

  • @Dd-do-and-dont
    @Dd-do-and-dont Před měsícem +12

    Dynamic programming 101.
    But, you could also use some kind of queue etc to store only last two elements :).
    I am to lazy for variables :)

  • @GregHogg
    @GregHogg  Před 2 měsíci +12

    I hope you enjoyed the video, thanks for watching! The sentiment behind this one is that many people I tutor (email greg.hogg1@outlook.com if you're interested) get stuck in a "let's just solve it and not worry about speed" way of thinking. This is okay to start, but I think you should always learn the best approach (and even some of the worse but different approaches) to solving each problem. In this one I show that Leetcode will actually accept your O(2^n) recursive backtracking solution, even though an O(n) approach exists as well. Drop a like and subscribe if you found this helpful!

  • @jesroe5842
    @jesroe5842 Před 29 dny +5

    Oh i just did this. You're supposed to use the power of matrix (1,1;1,0)

  • @fastneuro9829
    @fastneuro9829 Před 2 měsíci +12

    You can use general formula for Fibonacci numbers with complex numbers to get even faster solution

    • @joelwillis2043
      @joelwillis2043 Před 2 měsíci +2

      sure as long you can provide a valid proof in the interview I would accept it

    • @rikthecuber
      @rikthecuber Před 2 měsíci +1

      I know a general formula of fibonacci by solving its difference eqns but from what I understand, it is not computationally viable because the sqrt(5)'s will accumulate floating point errors.

    • @fastneuro9829
      @fastneuro9829 Před 2 měsíci

      @@rikthecuber you can just round numbers because you know they are int

    • @tomasbernardo5972
      @tomasbernardo5972 Před 2 měsíci +1

      ​@@fastneuro9829Maybe we could use Newton's Method to approximate the square root? And then round in the end?

    • @fastneuro9829
      @fastneuro9829 Před 2 měsíci

      @@tomasbernardo5972 probably yes

  • @ayoubelktibi500
    @ayoubelktibi500 Před měsícem +24

    u can literally solve that in O(log(n)) using maths

  • @mrCheeeseGuy
    @mrCheeeseGuy Před měsícem +9

    I would just write absolutely garbage and slow code but write it in a language like c++ or rust so it's fast. This may mean i am a noob programmer but i digress

    • @GregHogg
      @GregHogg  Před měsícem +2

      Lmao yeah that works

    • @jonatansexdoer96
      @jonatansexdoer96 Před měsícem

      Slow code is by definition still slow in "fast" languages

  • @JonBristow
    @JonBristow Před měsícem +6

    Why can't you solve this in O(1)? It's possible, you "just" find the closest integer (round half up) where F(n) = phi^n / sqrt(5) where phi is the golden ratio [1+sqrt(5)]/2.
    You can grab the 1 billionth Fibonacci number if your language's number representation can handle arbitrarily large numbers.

    • @GregHogg
      @GregHogg  Před měsícem

      You're right!

    • @yperm7627
      @yperm7627 Před měsícem

      Exponential is not constant time

    • @JonBristow
      @JonBristow Před měsícem

      Nvm I can’t read

    • @JonBristow
      @JonBristow Před měsícem

      C’s ** algo is most certainly log(exponent)

  • @Indyy67
    @Indyy67 Před měsícem

    I find your videos are a great way to learn and look at other ways to program fascinating. I have been a back office developer for over 3 decades building business systems. This was the way I was taught the fit the industries in the Midwest and East Coast. I have never had to use DSA in my systems in the way you are showing. A lot of the tools and operating systems I worked with handled most of this for you. Now that bI’m on the West Coast, I have to get past the culture shock and learn DSA. The problem I have is, once I picked up a lot of this knowledge, I never use it in my daily work. That’s when I forget it all.

    • @GregHogg
      @GregHogg  Před měsícem

      Yeah that's pretty common. People generally learn this stuff well when they need it for interviews, and start to forget about it on the job. That's natural and okay. And thank you for the kind words :)

  • @deemon710
    @deemon710 Před 26 dny

    watched this a few times and it still breaks my head

  •  Před měsícem +8

    you are absolute god expect you are appending all the useless values to list, instead of using using 2 variables

  • @not_vinkami
    @not_vinkami Před 2 měsíci +4

    When I see problems like these I tend to just solve it mathematically and throw in the general solution to get a O(1) space and about O(1) time code

    • @Ibra_
      @Ibra_ Před 2 měsíci +1

      But there is no mathematical equation that can just get you the fibonacci number without knowing the 2 before

    • @not_vinkami
      @not_vinkami Před 2 měsíci +3

      @@Ibra_ Binet's formula:

    • @stranger0152
      @stranger0152 Před 2 měsíci

      @@Ibra_ There is. You learn it in Linear Algebra courses in colleges. Actually any linear recursive defined function like fibonacci has general solution.

    • @Ibra_
      @Ibra_ Před 2 měsíci +3

      I feel stupid for not knowing that. Thank you guys

  • @sambitbasu6261
    @sambitbasu6261 Před měsícem

    The thing is, we are taught the second method first as kids and then later the recursive method, as a cooler way to solve the problem.

  • @ThePokeMaster03
    @ThePokeMaster03 Před měsícem +1

    You can also use dynamic programming with the recursive solution for the same time complexity

  • @uraid
    @uraid Před 2 měsíci

    You can also implement the fibonacci function for O(1)

  • @Bunnokazooie
    @Bunnokazooie Před měsícem +4

    There's no backtracking in the recursive solution

    • @GregHogg
      @GregHogg  Před měsícem

      Yeah that's kinda true. Moreso just naiive recursive solution

  • @FBHI
    @FBHI Před měsícem

    The way that I learned is to use "memoization" technique + recursive, and I thought that this was the best way to do this lol!!!

  • @g3mint446
    @g3mint446 Před měsícem +15

    Nice, meme. But for anyone serious about this. Part 1 was the god-tier programmer. Part 2 was the noob one.. It's called premature optimization, which is pointing and wrangling with stuff that just works fine. Programmers go from bad, to worse, to good. The good ones know to appreciate working code and some level of readability. Anything else usually just burns money.

    • @g3mint446
      @g3mint446 Před měsícem +2

      Having that said! Being able to go from recursive to iterative is very useful. It doesn't take too much to buffer overflow. Just don't default to it. Default to simple and readable.
      It's an optimization. Always start with simple and readable. Always, always.. it works, it works... If you for some reason cannot understand that.. We must be on different planets.

    • @anto_fire8534
      @anto_fire8534 Před měsícem

      This is not premature optimization.

    • @g3mint446
      @g3mint446 Před měsícem

      @@anto_fire8534 I don't agree, good code is relative to needs and goals. Maybe you have an argument in there?

    • @anto_fire8534
      @anto_fire8534 Před měsícem

      ​@@g3mint446 This is not premature optimization, the quote is always misinterpreted when it's actually intended to talk about micro-optimizations. Here, the algorithm is changed to a more efficient one. Also, since we're talking about readability, I'd say the second algorithm is much more readable considering it's much closer to what an human would actually do to manually calculate the Fibonacci sequence instead of the recursive solution. Finally, saying the first solution "just works fine" is wrong in my opinion considering it runs in non-polynomial times when there exists many simple polynomial time solutions and the non-polynomial time solution is unable to be used in a real world context considering it gets so slow it can't calculate numbers past like 60-70 in a reasonable amount of time.

    • @g3mint446
      @g3mint446 Před měsícem

      @@anto_fire8534 I have to agree with you there. But only if your team think it's the most readable and simple way to write code. If you don't have a clear need to rewrite readable code, why would you? I can do a million things to improve something, it doesn't mean I should. You get the most value from code that is readable, maintainable and working (not some smart ass genious, waste of time code).
      Some times you don't even need to read it again, it should just work. It is all about time management, and there are cases where scrapping the thing and making it just work is preferable to anything else. Anyone who call themselves an expert but cannot realize this, is not an expert (those people are lying to themselves).
      I have worked with horrible code and great code. I very much value simple code, but some times the code doesn't matter enough to be a priority, the project is, not the code.
      The project and the solution spec is the priority. That is what the Manager and the Customer has agreed on, and that is what they need you to help them with. Not a single thing beyond. If you have suggestions to do more, it should be clearly reported and discussed. That is my obligation as a developer. That is my job. Itching my brain and having arrogant opinions is not.
      I get work done fast and as ordered, that is my job. That is how I keep customers and managers happy. Not by being a genious. Your job is not your company, you don't decide how to run it.

  • @nk361
    @nk361 Před měsícem +1

    I am sitting here glad the "God tier" solution was easy

  • @Vzduch2
    @Vzduch2 Před 11 dny

    You can get the 2n-th and 2n+1-th fibonacci number if you have the n-th and n+1-th. If you consider multiplication to be constant, this is actually an O(log(n)) solution.

  • @serzhredalert1
    @serzhredalert1 Před měsícem +2

    using matrices you can achieve asymptotics O(log n)

  • @sorcdk2880
    @sorcdk2880 Před 6 dny

    Here is the more or less standard way to solve it:
    def fib(end):
    last = 0
    current = 1
    for i in range(end):
    yield current
    current, last = current + last, current
    This gives you a generator that builds it while going along without storing it as necessary. Alternatively you can build in the option for infiite generation, but you often do not want that.

  • @LaNiBlackLight
    @LaNiBlackLight Před 13 dny

    Are people actually jumping straight to recursions without thinking about other ways of solving issues?

  • @rutvikrana512
    @rutvikrana512 Před měsícem +1

    Absolute god who doesn’t care about O(N) space and time taken for appending to list not to mention recreating if hit upper limit. 😂

  • @jajceslav5665
    @jajceslav5665 Před měsícem

    Could go for a Fibonacci matrix exponentiation, though you would get overflow, but you'll want to calculate large Fibonacci numbers modulo some number

  • @TheJmax04
    @TheJmax04 Před měsícem +3

    (The recursive algorithm is not a backtracking algorithm)

    • @GregHogg
      @GregHogg  Před měsícem

      Yeah I think I misspoke

  • @TimOder
    @TimOder Před dnem

    Legends use matrix exponentiating

  • @sandipdaw5023
    @sandipdaw5023 Před 2 dny

    How is that constant time? When looping through n 🙄

  • @VivBrodock
    @VivBrodock Před měsícem

    "this is slow"
    proceeds to use arrays in python
    lol

  • @Willgtl
    @Willgtl Před měsícem

    I was fully expecting "Pro Tier" where you just call a library function.

  • @byronvega8298
    @byronvega8298 Před 2 měsíci +8

    And people who know math would do it in constant time and space complexity.

  • @PaingHtetKyaw-f1d
    @PaingHtetKyaw-f1d Před 8 dny

    May I know what sote is it?

  • @tomasbernardo5972
    @tomasbernardo5972 Před 2 měsíci +2

    Or you could just use something really cool called Binet's formula

  • @srijitdas5748
    @srijitdas5748 Před měsícem +6

    Meanwhile me who can do it O(1) time

    • @gvenkatesh8935
      @gvenkatesh8935 Před měsícem

      How?

    • @dahcus
      @dahcus Před měsícem

      ​@@gvenkatesh8935 There's a closed form solution. Search for Binet's forumula

    • @mtheos
      @mtheos Před měsícem

      @@gvenkatesh8935JonBristow posted it below, there's a formula for calculating the nth fib number which is constant time.

    • @Mammothina
      @Mammothina Před měsícem +1

      @@mtheos What is fun you can derive a comparable formula for any recursively defined sequence

  • @_skf_2262
    @_skf_2262 Před měsícem +1

    @GregHogg Actually if you write tail recursive functions you will get the same time and space complexity :) Stack recursive functions are bad of course

  • @rishabhshenoy3258
    @rishabhshenoy3258 Před 8 dny +1

    Those who don't know backtracking are real noobs😂😂

  • @shreehari2589
    @shreehari2589 Před 2 měsíci +2

    Two variables can do the job

  • @user-ts3nz1fs6w
    @user-ts3nz1fs6w Před 7 dny

    I don't know how does it happens on your language, but in my language adding new element into dynamic array - it is very expensive operation.

  • @firasnizam
    @firasnizam Před měsícem

    can you give me the links of these questions? that i try to solve them myself

  • @yoelczalas
    @yoelczalas Před 2 dny

    and the memory?

  • @mayureshsaitwal5608
    @mayureshsaitwal5608 Před měsícem

    what if i want to learn most of god tier methods , how do i search and learn !?

  • @parthrathod2607
    @parthrathod2607 Před měsícem +2

    Wait why I feel like I'm learning wrong way I did exactly what is best and then I was taught to do recursion 😮 that's not right it's like the simpler the better

    • @juanmacass
      @juanmacass Před měsícem

      no, recursion is good. That program is badly done

  • @nw4009
    @nw4009 Před 6 dny

    I have no idea what any of this means but I’m looking forward to failing to ever truly understand what any of this means.

  • @nano-sult9442
    @nano-sult9442 Před 25 dny

    God tier would use binary matrix exponentiation to get the answer in O(log n)

  • @astrahcat1212
    @astrahcat1212 Před 19 dny

    And then theres Titan God tier programmer: use an api thats optimized for you.

  • @Doodle1283
    @Doodle1283 Před měsícem +1

    This is decent for most real world applications but an O(n) algorithm is too lame to be called god tier imo. Use matrix exponentiation and we have O(logn).

    • @GregHogg
      @GregHogg  Před měsícem

      Haha yep you've got a point

  • @carloslopez-jg5ol
    @carloslopez-jg5ol Před měsícem

    where is he solving this question on?

  • @Zetta_Hakai
    @Zetta_Hakai Před měsícem

    I like your funny words, magic man.

  • @RegularNobody
    @RegularNobody Před měsícem

    >noob programmer
    >Recursion
    Yeah, that's a good joke

  • @yashpachkhede5931
    @yashpachkhede5931 Před 28 dny

    a=0
    b=1
    For(int i=2;i

  • @SkyVergil83
    @SkyVergil83 Před měsícem

    whats the extension in VS Code that measures the performance?

  • @MB-tb4vj
    @MB-tb4vj Před 2 měsíci +12

    Legend programmer with a Degree from Havard :
    Math.round(1/(5**0.5) * (1+(5**0.5))/2)**(n)
    With just a logn approach 🗿🗿

    • @paulkanja
      @paulkanja Před 2 měsíci +1

      exactly...

    • @warguy6474
      @warguy6474 Před 2 měsíci +1

      ok but you lose accuracy and speed for really large inputs

    • @paulkanja
      @paulkanja Před 2 měsíci

      @@warguy6474 true, there is a better formula that you can use

    • @MB-tb4vj
      @MB-tb4vj Před 2 měsíci

      @@warguy6474 might be less accuracy due to the 64 bit double/integer precision limit in most of the languages but it will be much faster than the O(n) approach in any given input size.

    • @MB-tb4vj
      @MB-tb4vj Před 2 měsíci

      @@paulkanja is it?? could you tell me??

  • @kolere23
    @kolere23 Před měsícem +6

    Memoization anyone?

  • @firebolt3626
    @firebolt3626 Před měsícem

    Use this instead of the array
    a, b = 0, 1
    For _ in range(n):
    Print(a)
    a, b = b, a + b

  • @pranjalpandeyp
    @pranjalpandeyp Před 16 dny

    Tried DP yet ?

  • @karstr770
    @karstr770 Před 2 měsíci +1

    Honestly, both solutions are subpar. Or maybe it was intended as satire, I don't even know anymore.

    • @GregHogg
      @GregHogg  Před 2 měsíci

      Somewhat satire, moreso entertaining helpful. Yes there's even more optimal solutions

  • @Jjer.z
    @Jjer.z Před měsícem +3

    Dynamic Programming 😍

  • @DerHouy
    @DerHouy Před 5 dny

    Then I'm a non-existent programmer because I don't know either solution haha

  • @rishujeetrai5780
    @rishujeetrai5780 Před 2 minutami

    Binet's formul does it in O(1) time and space

  • @Bandana-Waddle-Dee
    @Bandana-Waddle-Dee Před 2 měsíci +2

    If you really cared about speed, you wouldn't be using Python

  • @howareyou4400
    @howareyou4400 Před měsícem

    Or, we could overkill and do this in O(log n) time for the fancy points.

  • @sambitkanjilal8099
    @sambitkanjilal8099 Před měsícem

    True God tier: mathematician using o(1) binets formula

  • @user-zn2vh1tc3y
    @user-zn2vh1tc3y Před 25 dny

    God tier whould use formula to calculate final value

  • @ajuc005
    @ajuc005 Před měsícem

    you just need 2 numbers why store all?

  • @iron_spark
    @iron_spark Před měsícem

    both forgot to name the class and method names correctly - the code wouldn't get discovered or used

  • @professornumbskull5555

    The God tier would use Matrix based calculation for logarithmic space and time for the most optimal solution... My disappointment is immeasurable and my day is ruined.

  • @matthiasflukiger4866
    @matthiasflukiger4866 Před 25 dny

    God tier would use math and linear algebra to prove there is a general formula to calculate the nth number in the fibonnacci suite (it's called Binet's formula) and then the algorithm is in time complexity of 0(1)

    • @GregHogg
      @GregHogg  Před 24 dny

      😂 super mega nerdy coder tier

  • @calebkirschbaum8158
    @calebkirschbaum8158 Před 24 dny

    You can just calculate it using a formula for constant time. Sometimes doing it the better algorithmically way fails if you don't know the math.

  • @BomberKing
    @BomberKing Před 2 měsíci

    You can do even faster using matrices

  • @Enter54623
    @Enter54623 Před měsícem +1

    He said recursive and I shuddered

  • @ShadowBoy609
    @ShadowBoy609 Před 2 měsíci +1

    Im an idiot in programming and I still wouldn’t do recursive backtracking lol

  • @u_s.e.r
    @u_s.e.r Před 2 měsíci

    use the general form of the sequence would be even faster

  • @jeeboi347
    @jeeboi347 Před měsícem

    I'm a beginner. What website it this? Just curious, thx

  • @DoubleFractals
    @DoubleFractals Před 2 měsíci

    This is how I program. If it works, it works.

  • @modularcuriosity
    @modularcuriosity Před měsícem

    How about using a generating function and then it's o(1)?

    • @fow7139
      @fow7139 Před měsícem +1

      That's not how it works

  • @nif4345
    @nif4345 Před 9 dny

    Use the (φ^n - (1-φ)^n)/√5 technique

  • @juanmacass
    @juanmacass Před měsícem +1

    You are a junior programmer... the problem is not the recursion. The problem is the way you wrote that recursive program, that calculate many times the sames numbers. You can make the program recursive without doing that. You wrote an O(2^n) solution when you can make an O(n) one.

    • @GregHogg
      @GregHogg  Před měsícem

      Yes we cache it and make it o(n)

  • @abdennacerlachiheb319
    @abdennacerlachiheb319 Před měsícem

    You could've kept the recursive approach and added caching, and you will get the same results as the tabular dynamic programming.

    • @GregHogg
      @GregHogg  Před měsícem

      Not quite the same results as for loops are better than recursion. But you're right, same time complexity

  • @hashedone
    @hashedone Před 16 dny

    It's not a god tier programmer, he just wasted sh*tload of memory instead of solving it in constant space.

  • @RRFlash
    @RRFlash Před měsícem

    matrix exponentiation is best solution for general Fibonacci problem

  • @davidgillies620
    @davidgillies620 Před měsícem

    Fibonacci of -n is (-1)^(n + 1) fib(n)

  • @reapedspartan8172
    @reapedspartan8172 Před měsícem

    If you're just starting out, what language would you guys advise learning first?

    • @GregHogg
      @GregHogg  Před měsícem

      JavaScript or Python. Personally if i were to go back I would choose js, it's just more practical

    • @reapedspartan8172
      @reapedspartan8172 Před měsícem

      @GregHogg practical in what sense? Is it a more in-demand language from employers?

  • @anshumankhanna8296
    @anshumankhanna8296 Před měsícem

    You can reduce the space complexity to O(1) bro, why didn't you

  • @Yolosopher27
    @Yolosopher27 Před 29 dny

    broo wtf?!!! you look like my bro James more than my bro James looks like himself

  • @GPCTM
    @GPCTM Před měsícem

    Step aside, peasants.
    n = int(input("how many Fibonacci numbers do you want?"))
    x,y = 1,1
    for i in range(1, n+1):
    print(x)
    x,y = y, x+y

  • @user-jz7vf5iq7h
    @user-jz7vf5iq7h Před měsícem

    god tier matematician.
    there's no fibonacci NUMBER.
    closest you could get to "fibonacci number" would be "the numberS on the fibonacci SEQUENCE".

    • @GregHogg
      @GregHogg  Před měsícem

      Not sure why there's a distinction lol

    • @user-jz7vf5iq7h
      @user-jz7vf5iq7h Před měsícem

      @@GregHogg easy.
      a number is A number. (for example: 3 or 28 or 137)
      a secuence is COUNTLESS numbers that are in a certain, relevant, order. (for example: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, ... or 1, 3, 5, 7, 9, 11, 13, 15, 17 or 2, 3, 5, 7, 11, ...)

  • @HamzaAhmed-wb7iw
    @HamzaAhmed-wb7iw Před měsícem

    Bruh I literally wrote the same program for fibonacci number while learning C++ because I hadn't learnt recursion.

  • @magiccuttlefish
    @magiccuttlefish Před 12 dny

    The god program is actually the noob at this point. Who thinks of recitation as a first option

  • @farhadzada3162
    @farhadzada3162 Před 22 dny

    You can do it in logarithmic time ))

  • @rubyverma484
    @rubyverma484 Před měsícem +1

    Im god tier then