tora-pan / 100DaysOfLeetCode

Time to step up my DSA game.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

100 days of leetcode

Why am i doing this?

Now that i am officially a fulltime software engineer, i thought it was about time that i start diving deeper into data structures and algorithms.

I did a quick search on google and multiple sources pointed toward something known as the blind 75 .

This list was curated by a facebook tech lead to help all of the "time-constrained engineers" out there looking for a job.

These questions teach you the core concepts and techniques for each category/type of problem. many of the leetcode type interview questions that you will most definitely run into when trying to break into "big tech" are either directly chosen from this list or a combination/mashup of a few of them.

Categories

  1. array
  2. binary
  3. dynamic programming
  4. graph
  5. interval
  6. linked list
  7. matrix
  8. string
  9. tree
  10. heap

Common Patterns


Array

two sum

prompt:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

my solution:

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function (nums, target) {
  //set up results object
  results = {};

  for (let i = 0; i < nums.length; i++) {
    // check to see if complement exists
    let complement = target - nums[i];
    if (results.hasOwnProperty(complement)) {
      return [results[complement], i];
    }
    results[nums[i]] = i;
  }

  return null;
};

notes:

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

link to leetcode

best time to buy and sell stock

prompt:

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

my solution:

// My initial naive solution timed out.
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  let buyPrice = 0;
  let sellPrice = 0;
  let profit = 0;

  for (let i = 0; i < prices.length; i++) {
    buyPrice = prices[i];
    for (let j = i + 1; j < prices.length; j++) {
      if (prices[j] > buyPrice) {
        sellPrice = prices[j];
        if (sellPrice - buyPrice > profit) {
          profit = sellPrice - buyPrice;
        }
      }
    }
  }
  return profit;
};

// After checking the discussion, I came to this solution.

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  // trying sliding window

  let buy = 0;
  let sell = 1;
  let profit = 0;

  for (let i = 0; i < prices.length - 1; i++) {
    if (prices[sell] - prices[buy] > profit) {
      profit = prices[sell] - prices[buy];
    }

    if (prices[buy] > prices[sell]) {
      buy = sell;
    }
    sell = sell + 1;
  }
  return profit;
};

notes:

The condition to use the sliding window technique is that the problem asks to find the maximum (or minimum) value for a function that calculates the answer repeatedly for a set of ranges from an array.

link to leetcode

contains duplicate

prompt:

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

my solution:

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function (nums) {
  let results = {};
  for (let i = 0; i < nums.length; i++) {
    if (results.hasOwnProperty(nums[i])) {
      return true;
    }
    results[nums[i]] = i;
  }
  return false;
};

notes:

Nothing really to note here... This was an easy one.

link to leetcode

product of array except self

prompt:

Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

my solution:

// insert solution here

notes:

link to leetcode

maximum subarray

prompt:

my solution:

// insert solution here

notes:

link to leetcode

maximum product subarray

prompt:

my solution:

// insert solution here

notes:

link to leetcode

find minimum in rotated sorted array

prompt:

my solution:

// insert solution here

notes:

link to leetcode

search in rotated sorted array

prompt:

my solution:

// insert solution here

notes:

link to leetcode

3sum

prompt:

my solution:

// insert solution here

notes:

link to leetcode

container with most water

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Binary

sum of two integers

prompt:

my solution:

// insert solution here

notes:

link to leetcode

number of 1 bits

prompt:

my solution:

// insert solution here

notes:

link to leetcode

counting bits

prompt:

my solution:

// insert solution here

notes:

link to leetcode

missing number

prompt:

my solution:

// insert solution here

notes:

link to leetcode

reverse bits

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Dynamic programming

climbing stairs

prompt:

my solution:

// insert solution here

notes:

link to leetcode

coin change

prompt:

my solution:

// insert solution here

notes:

link to leetcode

longest increasing subsequence

prompt:

my solution:

// insert solution here

notes:

link to leetcode

longest common subsequence

prompt:

my solution:

// insert solution here

notes:

link to leetcode

word break problem

prompt:

my solution:

// insert solution here

notes:

link to leetcode

combination sum

prompt:

my solution:

// insert solution here

notes:

link to leetcode

house robber

prompt:

my solution:

// insert solution here

notes:

link to leetcode

house robber ii

prompt:

my solution:

// insert solution here

notes:

link to leetcode

decode ways

prompt:

my solution:

// insert solution here

notes:

link to leetcode

unique paths

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Jump Game

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Graph

Clone Graph

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Course Schedule

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Pacific Atlantic Water Flow

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Number of Islands

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Longest Consecutive Sequence

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Alien Dictionary

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Graph Valid Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Number of Connected Components in an Undirected Graph

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Interval

Insert Interval

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Merge Intervals

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Non-overlapping Intervals

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Meeting Rooms

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Meeting Rooms II

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Linked list

Reverse a Linked List

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Detect Cycle in a Linked List

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Merge Two Sorted Lists

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Merge K Sorted Lists

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Remove Nth Node From End of List

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Reorder List

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Matrix

Set Matrix Zeros

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Spiral Matrix

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Rotate Image

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Word Search

prompt:

my solution:

// insert solution here

notes:

link to leetcode


String

Longest Substring Without Repeating Characters

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Longest Repeating Character Replacement

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Minimum Window Substring

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Valid Anagram

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Group Anagrams

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Valid Parentheses

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Valid Palindrome

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Longest Palindromic Substring

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Palindromic Substrings

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Encode and Decode Strings

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Tree

MaximumDepth of Binary Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Same Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Invert/Flip Binary Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Binary Tree Maximum Path Sum

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Binary Tree Level Order Traversal

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Serialize and Deserialize Binary Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Subtree of Another Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Construct Binary Tree from Preorder and Inorder Traversal

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Validate Binary Search Tree

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Kth Smallest Element in a BST

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Lowest Common Ancestor of BST

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Implement Trie (Prefix Tree)

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Add and Search Word

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Word Search II

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Heap

Merge K Sorted Lists

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Top K Frequent Elements

prompt:

my solution:

// insert solution here

notes:

link to leetcode

Find Median from Data Stream

prompt:

my solution:

// insert solution here

notes:

link to leetcode


Patterns

  • Sliding Window
  • Two Pointers or Iterators
  • Fast and Slow Pointers
  • Merge Intervals
  • Cyclic Sort
  • In-Place Reversal of - Linked List
  • Tree BFS
  • Tree DFS
  • Two Heaps
  • Subsets
  • Modified Binary Search
  • Top K Elements
  • K-Way Merge
  • Topological Sort

About

Time to step up my DSA game.