Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
Prashant Basnet, a software engineer at Unisala.com, focuses on software development and enjoys building platforms to share knowledge. Interested in system design, data structures, and is currently learning NLP
π Problem Restatement (in simpler terms)
You are given:
π§ What is a path?
A path is a sequence of nodes:
Now suppose the colors string is:
colors = "abaca" β so:
And your edges are:
Letβs pick a path:
On this path, the colors are:
So the color frequency for this path is:
β What this does imply:
β Example:
Letβs say:
Instead of tracking that whole path, you just do:
π§ Why not 1D array ?
β οΈ The core issue:
π§ͺ Example to show why 1D fails:
Letβs say:
Suppose two paths go to node 2:
But:
β Why 2D works:
π§ Analogy:
Think of it like tracking the highest GPA in every subject per student:
Final Space Complexity:
Because 26 is constant β drops out in Big-O notation.
Final Time Compelxity: