Replaces the previous graph-based approach with a more efficient
dynamic programming solution to find the minimum fragments.
#### Is your solution the most efficient way to solve a problem? Why? Yes, since I utilized dynamic programming approach which is considered a highly efficient and standard way to solve this problem. Actually, I saw the 3 years ago solution which uses BFS approach. For me, BFS requires potentially expensive preprocessing to find all fragment occurrences and build a graph. So that is the difference between the solution I make, since it will avoid this upfront cost by directly calculating the min fragments for each prefix length as it iterates through the word.
#### Have you used any specific algorithm? Yes, I've used dynamic programming. Specifically, it applies a bottom-up approach where the min fragments are needed to build progressively prefixes of the word (dp[i]) are calculated based on the results for smaller prefixes (dp[i - len(fragment)]).
#### What is time and space complexity of your solution? O(N * M * L)
合并状态:未合并 关闭于 2025-04-13 1 条评论