C
C#ā€¢13mo ago
morry329#

āœ… What is the function of the Dictionary here?

public class Solution { Dictionary<int,int>cached=new Dictionary<int,int>(); public int ClimbStairs(int n) { if(cached.ContainsKey(n)) { return cached[n]; } int output = 1; if(n<=3){ output = n; } else { output = ClimbStairs(n-1) + ClimbStairs(n-2);
} cached[n]=output; return output; } } So I wanted to understand the above solution for this LeetCode puzzle https://leetcode.com/problems/climbing-stairs/description/ I am still wondering what the Dictionary is doing here. My guess is that it prevents the code from exceeding the time limit (as my code went to exceed the limit without the Dictionary). But I appreciate any other plausible explanation for the Dictionary's presence here.
LeetCode
Climbing Stairs - LeetCode
Can you solve this real interview question? Climbing Stairs - You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Ā  Example 1: Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps...
5 Replies
ero
eroā€¢13mo ago
it's a cache. say n is 6 initially. it would then call ClimbStairs with both 5 and 4 (in that order). the call with 5 does another call with 4 and 3. the result of the call with 4 is stored in the cache. that means the other call with 4 no longer needs to do any calculation, because we've already done it once. we just access the previously-calculated result
HimmDawg
HimmDawgā€¢13mo ago
That's not part of the task here, but I'd probably go as far as adding the number of ways for 1, 2 and 3 steps manually to the dictionary. Then we don't need the if-else
ero
eroā€¢13mo ago
whatever leetcode uses to measure speed and performance is a compelte joke anyway
ero
eroā€¢13mo ago
this is the same solution run twice in a row
morry329#
morry329#ā€¢13mo ago
alright, thank you for clarifying people šŸ™‚
Want results from more Discord servers?
Add your server
More Posts
ā” JWT Refresh tokensI've got a RefreshToken endpoint on my UserController class. I'm currently able to regenerate these ā” System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=6.0.0.0Hey all I am designing a win forms application and when trying to test a build I am greeted with theā” streaming an IAsyncEnumerable<string> from complex typehey. i have a minimal api that looks like this: ```cs public static async IAsyncEnumerable<string> Aā” Assign result from await to a variableIs there a difference between those two calls? `var awaitable = await GetObject(); otherObject.IntPā” āœ… Prints System.Func`1[System.Int32] instead of actual int.I'm trying to return a private field member variable and instead of giving me the actual integer valā” Exception trying to run code from a .NET Framework 4.5.2 on .NET 6 projectHi, I have a new project using .NET 6, but I need to call a method in a project using .NET Framewoāœ… Hosting web app with SQL Server for free/minimal cost?Hello there, I was wondering if anyone knew any cloud/hosting providers that allow hosting an ASP.Nā” WinUI3: Restoring Window Size on relaunchIn UWP and WinUI2, it would automatically relaunch to the last width/height of the application, but ā” āœ… custom mapper and resolversHello, I've been trying to use automapper for a while, but now I've decided to just replace it with ā” Best way to convert rgb to ConsoleColor?Hey, so I have a Console App, where I want to transition between 2 colors( and output the color as b