11 Replies
yeah you need to create a class that implements IEqualityComparer and do the logic to make it equal if the elements are the same and inject an instance of that class to the Dictionary's constructor
or perhaps there is a built in class that already does that
TheRanger
REPL Result: Success
Console Output
Compile: 781.850ms | Execution: 120.559ms | React with ❌ to remove this embed.
or you can make the class contain a generic argument
TheRanger
REPL Result: Success
Console Output
Compile: 754.357ms | Execution: 138.209ms | React with ❌ to remove this embed.
but the question is, why Dictionary<int[], int> ? if you will tell me what are you trying to achieve, i may provide a better solution
i was trying this problem https://leetcode.com/problems/unique-paths/
and I wanted to solve this with DP so I wanted to store the grid as key and the result as value
LeetCode
Unique Paths - LeetCode
Can you solve this real interview question? Unique Paths - There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, ...
but then, I kinda just converted to string and store it as <string, int>
like key = string.Format()
What is the effect of storing the grid as the key? There is only ever one grid and I wouldn't expect any cell to have state
your end goal is to determine the number of possible routes to get from a point
A
to a point B
so you could try instead tracking the number of possible routes to get from A
to each other point in the grid (the subproblem for a given cell is the number of routes from A
to that cell)
then your solution is the value assigned to the point B
in this case you would only need one 2d array, or even a map with points as keysAs the arrays appear to be a fixed size, I would just use ValueTuple
yeah that could work too. But using dictionary was just my first thought
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.