Giter Club home page Giter Club logo

algos's Introduction

Algo Strategies and Patterns

Definitions

Space Complexity

Can depend on:

  • data structures that grow in some proportion to input
  • a call stack that grows in some proportion to input

Strategies

Iterate once while keeping track of global max/mins

Rather than comparing each element to every other element, keep track of relevant max/mins as you go

  • Benefits:
    • Saves time compared to nested loops
  • Used in:
    • Best Time to Buy and Sell Stock

Two pointer

Can be for loop with two variables:

for (let i=0, j=arr.length-1; i <= j; i++, j--){
    // ...
}

Or while loop with left/right:

let left = 0
let right = 0

while (left < right) {
    // ...
    left ++
    right --
}
  • Benefits:
    • Saves space compared to creating a new array to track values
  • Used in:
    • Valid Palindromes

Edit input in place

Can use regex, .replace() (or equivalent)

  • Benefits:
    • Saves time and space compared to creating a new entity
  • Used in:
    • Valid Palindromes

Recursion

Works well for fractal things (e.g. trees) โ€” that is, things whose zoomed-in pieces look similar to the zoomed-out whole

  • Used in:
    • Invert Binary Tree

Depth-First Search

Uses recursion to reach the lowest node of one branch of a tree, then progresses to subsequent branches

  • Used in:
    • Invert Binary Tree

Breadth-First Search

Uses a queue data structure (First in, first out): Diagram of queue data structure: a row of 5 purple triangles arranged back to front (left to right). A new rectangle enters the queue (enqueues) from the left (back). Another rectangle leaves the queue from the right (front).

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.