How much time should I spend on a difficult DSA problem before reading its editorial?

  1. Reading and understanding the problem — Lets take an example problem.This problem looks very complicated by its statement. But, if you calmly try to understand it, you will get that it just wants you to look at all the query subarrays for different [l,r]. Those subarrays which have a positive sum should be taken in answer and their sum is to be added to total answer.
  2. Write a brute force solution to it. This would be a very basic solution to the problem. In this case, you would just loop on all queries, take sum of the subarray for that query. If it is positive, add it to answer.
  3. Optimise it using DSA. Here comes your ability to look for patterns which indicate using certain Data Structures and Algorithms. Like in this problem, because we are looking at subarray sums and recalculating it in each query, we can precompute it using prefix sum and answer each query in O(1).
  • If part 1 is weak, try to practice reading multiple questions and dry running your understanding on sample cases.
  • If part 2 is weak, do more easy problems on platforms to be able to at least think brute force for each and every problem.
  • If part 3 is weak, you need to work more algo by algo to make sure that you know and map different patterns to different algorithms.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Programming Pathshala

Programming Pathshala

We are working to democratise access to Tech Education. We help students learn coding and be confident about themselves.