Giter VIP home page Giter VIP logo

dsa's People

Contributors

sanjarcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dsa's Issues

The trick to single pass array problems, and more

Copying from activity_logger

All single pass 1D array problems rely on the "knowing" what has happened in the past - i.e possibility of creating a fast lookup structure for the till-seen part of the array.

This fast lookup structure may be a number (e.g. in case of Kadane's algorithm) or a hashmap or a heap, anything.

Approximately, this should takeo(n) space and/or o(n) lookup time (small O notation).

If this is not possible - i.e. one cannot create a structure at all or it exceeds the said bound, two (or more) loops are inevitable.

*more than one loop is inevitable

This thinking could be extended to more dimensional structures like n-dimensional arrays or trees or graphs or a combination.

Why this might work - noob level information theory

Make utils/generators for DSA

Th Vimeagen (Prims algo) https://youtu.be/om4nY7nQyBQ?si=F70CoVp8eREBMAjx (see first 1 min)

  • One reason I always feel lazy thinking/starting out with trying out DSA is the setups one needs to do. Maybe there's a lib already. Example - adjacency lists, iterators, stack funcs, fun data examples.
  • Also make sure there's exactly one representation of each algo/ds/idea. Variations (including important ones are 'collapsed' by default). One source of truth for each algo/ds.
  • Make a debug, animation layer (optional). May be irrelevant or even harmful for some algo/ds.

Deeply study the 2 conditions for DP

  1. Optimal substructure, and examine problems that have/don't have it.
  2. Overlapping subproblems, and examine problems that have/don't have it.
  3. Relation between DP and Divide and Conquer
  4. Effect of nature of the problem and these algorithm design techniques

Calculate optimality of based on optimal substructure and overlapping subproblems

Consider this problem to understand Trapping Water - LeetCode

Understanding the problem

Suppose we start from the left and continue, we would stop at a height >= where we started, 
and then calculate the water = left * (k - j - 1) - sum(blocks in the way).

Now, apply the same logic to the largest and 2nd largest heights. Basically, we cut the maximum height to be 
the 2nd maximum height for each case (of selection of region). 
We will basically after finding water between them, 
the part on the left and right are completely independent. 
So we do have optimal substructure. 
But we don't have overlapping subproblems. So this can be done by divide and conquer.

trap(i, j) = trap(i, a) + water(a...b) + trap(b, j) where a, b are first and second maximas (in any order). 
Trap will return a number.

Also, we can reuse the walls of a region for outer regions.
*/

/*
Approach 1 - simple recursion is optimal? because we have no overlapping subproblems, so nothing to store. 
And recursion is a must because input selection is variable for each case.

As we are doing everything optimally, this is optimal? Is it?

By the way, this gave TLE error on LeetCode.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.