Skip to content

Instantly share code, notes, and snippets.

@any9527
any9527 / leetcode_42.md
Last active September 14, 2022 03:22
Leetcode 42: Trapping rain water

Description

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Link to the original problem

Example 1

rainwatertrap

@any9527
any9527 / leetcode_85.md
Last active September 12, 2022 04:44
Leetcode 85: Maximal Rectangle

Description

Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

Link to the original problem

Example 1

maximal

@any9527
any9527 / leetcode_636.md
Last active January 14, 2025 22:10
Leetcode 636: Exclusive Time of Functions

Description

On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1.

Function calls are stored in a call stack: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is the current function being executed. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.

You are given a list logs, where logs[i] represents the ith log message formatted as a string "{function_id}:{"start" | "end"}:{timestamp}". For example, "0:start:3" means a function call with function ID 0 started at the beginning of timestamp 3, and "1:end:2" means a function call with function ID 1 ended at the end of timestamp 2. Note that a function can be called **multiple times, possibly recur

@any9527
any9527 / leetcode_20.md
Last active August 18, 2022 03:31
Leetcode 20: Valid Parentheses

Description

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Link to the original problem

@any9527
any9527 / leetcode_399.md
Last active August 11, 2022 03:41
Leetcode 399: Evaluate division

Description

You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

You are also given some queries, where queries[j] = [Cj, Dj] represents the jth query where you must find the answer for Cj / Dj = ?.

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Note: The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.

@any9527
any9527 / leetcode_726.md
Last active August 12, 2022 03:00
Leetcode 726: Number of Atoms

Description

Given a string formula representing a chemical formula, return the count of each atom.

The atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.

One or more digits representing that element's count may follow if the count is greater than 1. If the count is 1, no digits will follow.

  • For example, "H2O" and "H2O2" are possible, but "H1O2" is impossible.

Two formulas are concatenated together to produce another formula.

@any9527
any9527 / leetcode_460.md
Last active June 3, 2022 01:43
LFU Cache

Description

Design and implement a data structure for a Least Frequently Used (LFU) cache.

Implement the LFUCache class:

  • LFUCache(int capacity) Initializes the object with the capacity of the data structure.
  • int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
  • void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.
@any9527
any9527 / leetcode_146.md
Last active June 2, 2022 02:44
LRU Cache

Description

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Implement the LRUCache class:

  • LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
  • int get(int key) Return the value of the key if the key exists, otherwise return -1.
  • void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity.
@any9527
any9527 / leetcode_518.md
Last active May 19, 2022 03:45
Coin Change II

Description

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

@any9527
any9527 / shortest_path_in_matrix_summary.md
Last active May 8, 2022 04:23
Shortest Path in Matrix - Summary

Context

Very frequently we'll encounter problems like "Find the path in a matrix with minimum distance/cost", so the question is how can we solve it easily and also efficiently? This note is not intended to be a definitive guide by any means, but it only attempts to give a general idea/direction, so that when we meet such a problem, we know where to start figuring it out.

Note

  1. We use "matrix" and "grid" interchangeably in this note.
  2. This note assumes you have experience with basic grid traverse patterns like BFS and DFS.

Idea