We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
1) DFS bruteforce (TLE) - go through all comibinations. Sort as you add.
O( nsq log n)
2) DFS memoized (TLE) - approach 1) but keep tracked of last stone values and do not proceed if they have already been processed. O ( n log n)
3) Math - with initial stone value Math.min(a,b) * (n-1), thereafter you're adding Math.abs(a-b) to the previous value n times. O (n)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Manasa and Stones
You are viewing a single comment's thread. Return to all comments →
Approaches:
1) DFS bruteforce (TLE) - go through all comibinations. Sort as you add.
O( nsq log n) 2) DFS memoized (TLE) - approach 1) but keep tracked of last stone values and do not proceed if they have already been processed. O ( n log n)
3) Math - with initial stone value Math.min(a,b) * (n-1), thereafter you're adding Math.abs(a-b) to the previous value n times. O (n)