# prdlk > Documentation for my LeetCode solutions ## Docs - [Introduction](https://prdcs.wiki/): Structured dependency spine based roadmap for solving DSA Problems on leetcode. - [Keywords](https://prdcs.wiki/keywords): Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. "K-th largest" hints at a heap. "Shortest path" in an unweighted graph hints at BFS. ## Linear Structures ### Hash Table - [1. Two Sum](https://prdcs.wiki/hash-table/two-sum): You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target - [49. Group Anagrams](https://prdcs.wiki/hash-table/group-anagrams): Given an array of strings strs, group the anagrams together. You can return the answer in any order - [128. Longest Consecutive Sequence](https://prdcs.wiki/hash-table/longest-consecutive-sequence): Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence - [205. Isomorphic Strings](https://prdcs.wiki/hash-table/isomorphic-strings): Given two strings s and t, determine if they are isomorphic - [217. Contains Duplicate](https://prdcs.wiki/hash-table/contains-duplicate): 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 - [242. Valid Anagram](https://prdcs.wiki/hash-table/valid-anagram): Given two strings s and t, return true if t is an anagram of s, and false otherwise - [290. Word Pattern](https://prdcs.wiki/hash-table/word-pattern): Given a pattern and a string s, find if s follows the same pattern - [347. Top K Frequent Elements](https://prdcs.wiki/hash-table/top-k-frequent-elements): Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order - [387. First Unique Character in a String](https://prdcs.wiki/hash-table/first-unique-character-in-a-string): Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1 - [451. Sort Characters By Frequency](https://prdcs.wiki/hash-table/sort-characters-by-frequency): Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string - [1832. Check if the Sentence Is Pangram](https://prdcs.wiki/hash-table/check-if-the-sentence-is-pangram): A pangram is a sentence where every letter of the English alphabet appears at least once ### Prefix Sum - [238. Product of Array Except Self](https://prdcs.wiki/prefix-sum/product-of-array-except-self): 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] - [303. Range Sum Query - Immutable](https://prdcs.wiki/prefix-sum/range-sum-query-immutable): Given an integer array nums, handle multiple queries of the following type - [525. Contiguous Array](https://prdcs.wiki/prefix-sum/contiguous-array): Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1 - [560. Subarray Sum Equals K](https://prdcs.wiki/prefix-sum/subarray-sum-equals-k): Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k - [974. Subarray Sums Divisible by K](https://prdcs.wiki/prefix-sum/subarray-sums-divisible-by-k): Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k ### Two Pointers - [11. Container With Most Water](https://prdcs.wiki/two-pointers/container-with-most-water): You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i]) - [15. 3Sum](https://prdcs.wiki/two-pointers/3sum): Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0 - [42. Trapping Rain Water](https://prdcs.wiki/two-pointers/trapping-rain-water): 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 - [80. Remove Duplicates from Sorted Array II](https://prdcs.wiki/two-pointers/remove-duplicates-from-sorted-array-ii): Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same - [125. Valid Palindrome](https://prdcs.wiki/two-pointers/valid-palindrome): A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers - [167. Two Sum II - Input Array Is Sorted](https://prdcs.wiki/two-pointers/two-sum-ii-input-array-is-sorted): Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length - [189. Rotate Array](https://prdcs.wiki/two-pointers/rotate-array): Given an integer array nums, rotate the array to the right by k steps, where k is non-negative - [344. Reverse String](https://prdcs.wiki/two-pointers/reverse-string): Write a function that reverses a string. The input string is given as an array of characters s - [392. Is Subsequence](https://prdcs.wiki/two-pointers/is-subsequence): Given two strings s and t, return true if s is a subsequence of t, or false otherwise - [977. Squares of a Sorted Array](https://prdcs.wiki/two-pointers/squares-of-a-sorted-array): Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order ### Sliding Window - [3. Longest Substring Without Repeating Characters](https://prdcs.wiki/sliding-window/longest-substring-without-repeating-characters): Given a string s, find the length of the longest substring without duplicate characters - [76. Minimum Window Substring](https://prdcs.wiki/sliding-window/minimum-window-substring): Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string "" - [121. Best Time to Buy and Sell Stock](https://prdcs.wiki/sliding-window/best-time-to-buy-and-sell-stock): You are given an array prices where prices[i] is the price of a given stock on the i^th day - [209. Minimum Size Subarray Sum](https://prdcs.wiki/sliding-window/minimum-size-subarray-sum): Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead - [424. Longest Repeating Character Replacement](https://prdcs.wiki/sliding-window/longest-repeating-character-replacement): You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times - [567. Permutation in String](https://prdcs.wiki/sliding-window/permutation-in-string): Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise - [643. Maximum Average Subarray I](https://prdcs.wiki/sliding-window/maximum-average-subarray-i): You are given an integer array nums consisting of n elements, and an integer k - [1004. Max Consecutive Ones III](https://prdcs.wiki/sliding-window/max-consecutive-ones-iii): Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's ### Binary Search - [4. Median of Two Sorted Arrays](https://prdcs.wiki/binary-search/median-of-two-sorted-arrays): Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays - [33. Search in Rotated Sorted Array](https://prdcs.wiki/binary-search/search-in-rotated-sorted-array): There is an integer array nums sorted in ascending order (with distinct values) - [34. Find First and Last Position of Element in Sorted Array](https://prdcs.wiki/binary-search/find-first-and-last-position-of-element-in-sorted-array): Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value - [35. Search Insert Position](https://prdcs.wiki/binary-search/search-insert-position): Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order - [74. Search a 2D Matrix](https://prdcs.wiki/binary-search/search-a-2d-matrix): You are given an m x n integer matrix matrix with the following two properties: - [153. Find Minimum in Rotated Sorted Array](https://prdcs.wiki/binary-search/find-minimum-in-rotated-sorted-array): Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: - [162. Find Peak Element](https://prdcs.wiki/binary-search/find-peak-element): A peak element is an element that is strictly greater than its neighbors - [704. Binary Search](https://prdcs.wiki/binary-search/binary-search): Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1 - [875. Koko Eating Bananas](https://prdcs.wiki/binary-search/koko-eating-bananas): Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours ### Stack - [20. Valid Parentheses](https://prdcs.wiki/stack/valid-parentheses): Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid - [71. Simplify Path](https://prdcs.wiki/stack/simplify-path): You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path - [150. Evaluate Reverse Polish Notation](https://prdcs.wiki/stack/evaluate-reverse-polish-notation): You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation - [155. Min Stack](https://prdcs.wiki/stack/min-stack): Design a stack that supports push, pop, top, and retrieving the minimum element in constant time - [224. Basic Calculator](https://prdcs.wiki/stack/basic-calculator): Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation ### Monotonic Stack - [84. Largest Rectangle in Histogram](https://prdcs.wiki/monotonic-stack/largest-rectangle-in-histogram): Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram - [496. Next Greater Element I](https://prdcs.wiki/monotonic-stack/next-greater-element-i): The next greater element of some element x in an array is the first greater element that is to the right of x in the same array - [739. Daily Temperatures](https://prdcs.wiki/monotonic-stack/daily-temperatures): Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead - [853. Car Fleet](https://prdcs.wiki/monotonic-stack/car-fleet): There are n cars at given miles away from the starting mile 0, traveling to reach the mile target - [901. Online Stock Span](https://prdcs.wiki/monotonic-stack/online-stock-span): Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day ## Nodal & Grid ### Linked List - [19. Remove Nth Node From End of List](https://prdcs.wiki/linked-list/remove-nth-node-from-end-of-list): Given the head of a linked list, remove the n^th node from the end of the list and return its head - [21. Merge Two Sorted Lists](https://prdcs.wiki/linked-list/merge-two-sorted-lists): You are given the heads of two sorted linked lists list1 and list2 - [82. Remove Duplicates from Sorted List II](https://prdcs.wiki/linked-list/remove-duplicates-from-sorted-list-ii): You are given the head of a sorted linked list - [92. Reverse Linked List II](https://prdcs.wiki/linked-list/reverse-linked-list-ii): Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list - [141. Linked List Cycle](https://prdcs.wiki/linked-list/linked-list-cycle): Given head, the head of a linked list, determine if the linked list has a cycle in it - [143. Reorder List](https://prdcs.wiki/linked-list/reorder-list): You are given the head of a singly linked-list. The list can be represented as: - [206. Reverse Linked List](https://prdcs.wiki/linked-list/reverse-linked-list): Given the head of a singly linked list, reverse the list, and return the reversed list - [876. Middle of the Linked List](https://prdcs.wiki/linked-list/middle-of-the-linked-list): Given the head of a singly linked list, return the middle node of the linked list ### Hybrid Structures - [2. Add Two Numbers](https://prdcs.wiki/hybrid-structures/add-two-numbers): You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list - [146. LRU Cache](https://prdcs.wiki/hybrid-structures/lru-cache): Design a data structure that follows the constraints of a Least Recently Used (LRU) cache - [380. Insert Delete GetRandom O(1)](https://prdcs.wiki/hybrid-structures/insert-delete-getrandom-o1): Implement the RandomizedSet class: ### Matrix Index Math - [36. Valid Sudoku](https://prdcs.wiki/matrix-index-math/valid-sudoku): Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: - [48. Rotate Image](https://prdcs.wiki/matrix-index-math/rotate-image): You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise) - [54. Spiral Matrix](https://prdcs.wiki/matrix-index-math/spiral-matrix): Given an m x n matrix, return all elements of the matrix in spiral order - [73. Set Matrix Zeroes](https://prdcs.wiki/matrix-index-math/set-matrix-zeroes): Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's ## Hierarchical ### Tree DFS - [104. Maximum Depth of Binary Tree](https://prdcs.wiki/tree-dfs/maximum-depth-of-binary-tree): Given the root of a binary tree, return its maximum depth - [226. Invert Binary Tree](https://prdcs.wiki/tree-dfs/invert-binary-tree): Given the root of a binary tree, invert the tree, and return its root - [236. Lowest Common Ancestor of a Binary Tree](https://prdcs.wiki/tree-dfs/lowest-common-ancestor-of-a-binary-tree): Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree ### Tree BFS - [102. Binary Tree Level Order Traversal](https://prdcs.wiki/tree-bfs/binary-tree-level-order-traversal): Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level) - [199. Binary Tree Right Side View](https://prdcs.wiki/tree-bfs/binary-tree-right-side-view): Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom - [1448. Count Good Nodes in Binary Tree](https://prdcs.wiki/tree-bfs/count-good-nodes-in-binary-tree): Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X ### Binary Search Tree - [98. Validate Binary Search Tree](https://prdcs.wiki/binary-search-tree/validate-binary-search-tree): Given the root of a binary tree, determine if it is a valid binary search tree (BST) - [230. Kth Smallest Element in a BST](https://prdcs.wiki/binary-search-tree/kth-smallest-element-in-a-bst): Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree - [235. Lowest Common Ancestor of a Binary Search Tree](https://prdcs.wiki/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree): Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST ### Heap / Priority Queue - [23. Merge k Sorted Lists](https://prdcs.wiki/heap-priority-queue/merge-k-sorted-lists): You are given an array of k linked-lists lists, each linked-list is sorted in ascending order - [215. Kth Largest Element in an Array](https://prdcs.wiki/heap-priority-queue/kth-largest-element-in-an-array): Given an integer array nums and an integer k, return the k^th largest element in the array - [973. K Closest Points to Origin](https://prdcs.wiki/heap-priority-queue/k-closest-points-to-origin): Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0) ### Trie - [208. Implement Trie (Prefix Tree)](https://prdcs.wiki/trie/implement-trie-prefix-tree): A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker - [211. Design Add and Search Words Data Structure](https://prdcs.wiki/trie/design-add-and-search-words-data-structure): Design a data structure that supports adding new words and finding if a string matches any previously added string ## Relational ### Graph DFS - [133. Clone Graph](https://prdcs.wiki/graph-dfs/clone-graph): Given a reference of a node in a connected undirected graph - [200. Number of Islands](https://prdcs.wiki/graph-dfs/number-of-islands): Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands - [417. Pacific Atlantic Water Flow](https://prdcs.wiki/graph-dfs/pacific-atlantic-water-flow): There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges ### Graph BFS - [127. Word Ladder](https://prdcs.wiki/graph-bfs/word-ladder): A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that: - [994. Rotting Oranges](https://prdcs.wiki/graph-bfs/rotting-oranges): You are given an m x n grid where each cell can have one of three values: ### Topological Sort - [207. Course Schedule](https://prdcs.wiki/topological-sort/course-schedule): There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai - [210. Course Schedule II](https://prdcs.wiki/topological-sort/course-schedule-ii): There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai ### Union-Find - [323. Number of Connected Components in an Undirected Graph](https://prdcs.wiki/union-find/number-of-connected-components-in-an-undirected-graph): You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph - [684. Redundant Connection](https://prdcs.wiki/union-find/redundant-connection): In this problem, a tree is an undirected graph that is connected and has no cycles ## Decision Space ### Backtracking - [39. Combination Sum](https://prdcs.wiki/backtracking/combination-sum): Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order - [46. Permutations](https://prdcs.wiki/backtracking/permutations): Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order - [78. Subsets](https://prdcs.wiki/backtracking/subsets): Given an integer array nums of unique elements, return all possible subsets (the power set) ### 1-D Dynamic Programming - [70. Climbing Stairs](https://prdcs.wiki/1d-dynamic-programming/climbing-stairs): You are climbing a staircase. It takes n steps to reach the top - [198. House Robber](https://prdcs.wiki/1d-dynamic-programming/house-robber): You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night - [322. Coin Change](https://prdcs.wiki/1d-dynamic-programming/coin-change): You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money ### Multi-D / Grid DP - [62. Unique Paths](https://prdcs.wiki/multi-d-grid-dp/unique-paths): There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time - [64. Minimum Path Sum](https://prdcs.wiki/multi-d-grid-dp/minimum-path-sum): Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path - [1143. Longest Common Subsequence](https://prdcs.wiki/multi-d-grid-dp/longest-common-subsequence): Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0 ### Greedy - [53. Maximum Subarray](https://prdcs.wiki/greedy/maximum-subarray): Given an integer array nums, find the subarray with the largest sum, and return its sum - [55. Jump Game](https://prdcs.wiki/greedy/jump-game): You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position - [134. Gas Station](https://prdcs.wiki/greedy/gas-station): There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i] ### Intervals - [56. Merge Intervals](https://prdcs.wiki/intervals/merge-intervals): Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input - [57. Insert Interval](https://prdcs.wiki/intervals/insert-interval): You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval - [435. Non-overlapping Intervals](https://prdcs.wiki/intervals/non-overlapping-intervals): Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping ## Agent skills - [prdlk](https://prdcs.wiki/.well-known/agent-skills/prdlk/SKILL.md): Read the prdlk docs instead of answering from memory. Documentation for my LeetCode solutions Use when a task involves prdlk: how it works, how to set it up or configure it, or its API. ## Agent resources - [llms-full.txt](https://prdcs.wiki/llms-full.txt): The full Markdown of every page in one file. - [Page Markdown](https://prdcs.wiki/index.md): Append `.md` to any page URL to fetch that page as raw Markdown. - [JSON API](https://prdcs.wiki/api/docs/pages.json): Page index of the JSON docs API; each entry links the page's JSON and Markdown forms. Described by the OpenAPI document at https://prdcs.wiki/openapi.json. - [Agent skills](https://prdcs.wiki/.well-known/agent-skills/index.json): Agent Skills discovery index of the skills this site publishes. - [API catalog](https://prdcs.wiki/.well-known/api-catalog): RFC 9727 linkset of the APIs documented here. - [AI catalog](https://prdcs.wiki/.well-known/ai-catalog.json): ARD manifest of the agent-facing resources on this site (MCP server, skills, APIs). - [agent-readability.json](https://prdcs.wiki/agent-readability.json): Manifest of every agent-facing artifact on this site. - [Sitemap](https://prdcs.wiki/sitemap.xml): Every indexable page URL with its last-modified date.