Pattern
Graph
A compact view of archive entries grouped by recurring technique.
211 entries
211 entries tagged as Graph.
02.04.2026
medium3418. Maximum Amount of Money Robot Can Earn
Max sum path, 2 skips allowed
14.03.2026
medium1415. The k-th Lexicographical String of All Happy Strings of Length n
K-th generated abc non-repeating string
12.03.2026
hard3600. Maximize Spanning Tree Stability with Upgrades
Max of min values in span tree of graph, 2 s at most k
27.02.2026
hard3666. Minimum Operations to Equalize Binary String
Min steps to convert 01 stirng to ones flipping K
09.02.2026
medium1382. Balance a Binary Search Tree
Balance binary search tree
08.02.2026
easy110. Balanced Binary Tree
Is tree balanced?
28.01.2026
hard3651. Minimum Cost Path with Teleportations
Min cost to walk 2D grid with free teleports
27.01.2026
medium3650. Minimum Cost Path with Edge Reversals
Min path in weighted graph with reverse 2w nodes
09.01.2026
medium865. Smallest Subtree with all the Deepest Nodes
Lowest common ancestor of the deepest nodes
06.01.2026
medium1161. Maximum Level Sum of a Binary Tree
Min level max sum in tree
03.12.2026
hard1411. Number of Ways to Paint N × 3 Grid
Ways to color 3xN grid
29.12.2025
medium756. Pyramid Transition Matrix
Can build a pyramid from transitions?
19.12.2025
hard2092. Find All People With Secret
Time based secret spread in a graph
28.11.2025
hard2872. Maximum Number of K-Divisible Components
Max cuts where every subtree sum %k
06.10.2025
hard778. Swim in Rising Water
Min time to swim to end, flooding every second
05.10.2025
medium417. Pacific Atlantic Water Flow
Cells travel to TL&BR in decrease order
03.10.2025
hard407. Trapping Rain Water II
Fill water in 3D
10.09.2025
medium1733. Minimum Number of People to Teach
Min friends to teach common language to communicate in graph
31.08.2025
hard37. Sudoku Solver
Solve sudoku
10.08.2025
medium869. Reordered Power of 2
Rearrange digits to make power of two
17.07.2025
medium3202. Find the Maximum Length of Valid Subsequence II
Longest same-pair-k-parity subsequence
27.06.2025
hard2014. Longest Subsequence Repeated k Times
Longest k-repeating subsequence
08.06.2025
medium386. Lexicographical Numbers
Generate lexicographical numbers 1..n
05.06.2025
medium1061. Lexicographically Smallest Equivalent String
Map by smallest in group from associate s1 with s2
03.06.2025
hard1298. Maximum Candies You Can Get from Boxes
Open boxes graph with keys simulation
31.05.2025
medium909. Snakes and Ladders
Shortest path bottom-top in zig-zag matrix with jumps
30.05.2025
medium2359. Find Closest Node to Given Two Nodes
Closest common node
29.05.2025
hard3373. Maximize the Number of Target Nodes After Connecting Trees II
Max even-edged siblings after merging trees
28.05.2025
medium3372. Maximize the Number of Target Nodes After Connecting Trees I
Max k-reachable nodes of merged trees
26.05.2025
hard1857. Largest Color Value in a Directed Graph
Max color freq path
23.05.2025
hard3068. Find the Maximum Sum of Node Values
Max sum by k-xoring edges
18.05.2025
hard1931. Painting a Grid With Three Different Colors
Ways to 3-color m n grid, no adjucents
08.05.2025
medium3342. Find Minimum Time to Reach Last Room II
Fastest travel in graph, alternating dt=1,2
07.05.2025
medium3341. Find Minimum Time to Reach Last Room I
Fastest path in graph
01.04.2025
medium2140. Solving Questions With Brainpower
Max sum of (points, skip next) pairs
28.03.2025
hard2503. Maximum Number of Points From Grid Queries
Running queries of increasing BFS
23.03.2025
medium1976. Number of Ways to Arrive at Destination
Count optimal paths from 0 to n - 1 ford
22.03.2025
medium2685. Count the Number of Complete Components
Count fully connected components
21.03.2025
medium2115. Find All Possible Recipes from Given Supplies
Valid recipes from supplies
20.03.2025
hard3108. Minimum Cost Walk in Weighted Graph
Min AND of path weights manipulation
24.02.2025
medium2467. Most Profitable Path in a Tree
Max Alice path down, Bob up in tree
21.02.2025
medium1261. Find Elements in a Contaminated Binary Tree
Is number in a tree 2 v+(1L, 2R)?
20.02.2025
medium1980. Find Unique Binary String
Binary number not in a set
19.02.2025
medium1415. The k-th Lexicographical String of All Happy Strings of Length n
kth string of abc permutations
31.01.2025
hard827. Making A Large Island
Max area after filling one empty 2D grid cell find
30.01.2025
hard2493. Divide Nodes Into the Maximum Number of Groups
Max count of bipartitions in a graph
29.01.2025
medium684. Redundant Connection
First edge making a cycle in graph find
28.01.2025
medium2658. Maximum Number of Fish in a Grid
Largest region in 2D grid
27.01.2025
medium1462. Course Schedule IV
All innodes for each query in graph warshall
26.01.2025
hard2127. Maximum Employees to Be Invited to a Meeting
Max connected siblings in graph
24.01.2025
medium802. Find Eventual Safe States
Nodes without cycles
22.01.2025
medium1765. Map of Highest Peak
Make growing landscape
19.01.2025
hard407. Trapping Rain Water II
Trap the water in 2D height matrix
18.01.2025
hard1368. Minimum Cost to Make at Least One Valid Path in a Grid
Min undirected jumps to reach the end in grid
28.12.2024
hard689. Maximum Sum of 3 Non-Overlapping Subarrays
max non-intersecting intervals programming window
26.12.2024
medium494. Target Sum
Count signs permutations to sum equal target programming
25.12.2024
medium515. Find Largest Value in Each Tree Row
Tree layers maxes
24.12.2024
hard3203. Find Minimum Diameter After Merging Two Trees
Diameter of 2 connected trees
23.12.2024
medium2471. Minimum Number of Operations to Sort a Binary Tree by Level
Min swaps to sort tree layers
21.12.2024
hard2872. Maximum Number of K-Divisible Components
Max connected components divisible by k in graph
20.12.2024
medium2415. Reverse Odd Levels of Binary Tree
Odd-levels reversal in a perfect tree
30.11.2024
hard2097. Valid Arrangement of Pairs
Hierholzer algorithm
29.11.2024
hard2577. Minimum Time to Visit a Cell In a Grid
Min time start-end 4-d travel in 2D matrix with waiting
28.11.2024
hard2290. Minimum Obstacle Removal to Reach Corner
Min removals to travel first-last in 2D grid
27.11.2024
medium3243. Shortest Distance After Road Addition Queries I
Query shortest paths after adding new edges
26.11.2024
medium2924. Find Champion II
Root of the graph
25.11.2024
hard773. Sliding Puzzle
Steps to solve slide puzzle
26.10.2024
hard2458. Height of Binary Tree After Subtree Removal Queries
n new heights by cutting nodes in a Tree
24.10.2024
medium951. Flip Equivalent Binary Trees
Are trees flip-equal
23.10.2024
medium2641. Cousins in Binary Tree II
Replace Tree's values with cousines sum
22.10.2024
medium2583. Kth Largest Sum in a Binary Tree
kth largest level-sum in a tree
16.10.2024
medium1405. Longest Happy String
Longest string of a, b, c not repeating 3 times
21.09.2024
medium386. Lexicographical Numbers
Lexicographical ordered numbers 1..n
31.08.2024
medium1514. Path with Maximum Probability
Max path in graph
30.08.2024
hard2699. Modify Graph Edge Weights
Assign vacant -1 weight in graph to make shorted path equal target
29.08.2024
medium947. Most Stones Removed with Same Row or Column
Count islands of intersecting x and y
28.08.2024
medium1905. Count Sub Islands
Count islands intersecting in both 2D grids
27.08.2024
medium1514. Path with Maximum Probability
Max path in a weighted graph
31.07.2024
medium1105. Filling Bookcase Shelves
Min total height to split [[w,h]] array by shelfWidth programming
28.07.2024
hard2045. Second Minimum Time to Reach Destination
Second min time to travel from 1 to n in time-edged graph stopping every change seconds
26.07.2024
medium1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
Node with minimum neighbors by distanceThreshold
21.07.2024
hard2392. Build a Matrix With Conditions
Build a matrix from a graph conditions
30.06.2024
medium1579. Remove Max Number of Edges to Keep Graph Fully Traversable
Remove extra nodes in a connected graph by type 1, 2 and 3=1+2
29.06.2024
medium2192. All Ancestors of a Node in a Directed Acyclic Graph
List of ancestors in a DAG
28.06.2024
medium2285. Maximum Total Importance of Roads
Sort graph by siblings and compute sum(i s)
27.06.2024
easy1791. Find Center of Star Graph
Center of a start graph
25.06.2024
medium1038. Binary Search Tree to Greater Sum Tree
Aggregate Binary Search Tree from the right
25.05.2024
hard140. Word Break II
All string splits with dictionary programming
24.05.2024
hard1255. Maximum Score Words Formed by Letters
Max score of words subset from letters
23.05.2024
medium2597. The Number of Beautiful Subsets
Count subsets without k difference in them
22.05.2024
medium131. Palindrome Partitioning
All palindrome partitions programming
21.05.2024
medium78. Subsets
All subsets
20.05.2024
easy1863. Sum of All Subset XOR Totals
Sum of subsets xors
18.05.2024
medium979. Distribute Coins in Binary Tree
Min moves to spread the coins across the tree
17.05.2024
easy1325. Delete Leaves With a Given Value
Recursively remove target leafs from the tree
16.05.2024
easy2331. Evaluate Boolean Binary Tree
Evaluate tree where 0/1 is false/true and 2/3 is or/and
15.05.2024
medium2812. Find the Safest Path in a Grid
Safest path in a grid with thieves
14.05.2024
medium1219. Path with Maximum Gold
Max one-way path in matrix
28.04.2024
hard834. Sum of Distances in Tree
Sums of paths to each leafs in a tree
23.04.2024
medium310. Minimum Height Trees
Center of an acyclic graph
22.04.2024
medium752. Open the Lock
Steps to rotate 4-wheel 0000 -> target
21.04.2024
easy1971. Find if Path Exists in Graph
Are source and destination connected in graph?
20.04.2024
medium1992. Find All Groups of Farmland
Count 1-rectangles in 0-1 2D matrix
29.02.2024
medium1609. Even Odd Tree
Binary tree levels are odd increasing and even decreasing.
23.02.2024
medium787. Cheapest Flights Within K Stops
Cheapest travel src -> dst with at most k stops in a directed weighted graph.
22.02.2024
easy997. Find the Town Judge
Find who trusts nobody and everybody trusts him in [trust, trusted] array.
09.02.2024
medium368. Largest Divisible Subset
Longest subset of divisible by s[i] % s[j] == 0 || s[j] % s[i] == 0.
08.02.2024
medium279. Perfect Squares
Min square numbers sum up to n.
18.01.2024
easy70. Climbing Stairs
Ways to climb n stairs by 1 or 2 steps.
10.01.2024
medium2385. Amount of Time for Binary Tree to Be Infected
Max distance from node in a Binary Tree.
25.12.2023
medium91. Decode Ways
Ways to decode back 'A' -> '1', 'B' -> '2' … 'Z' -> '26'
12.11.2023
hard815. Bus Routes
Minimum buses to travel by given routes
11.11.2023
hard2642. Design Graph With Shortest Path Calculator
Implement graph with shortest path searching
10.11.2023
medium1743. Restore the Array From Adjacent Pairs
Restore an array from adjacent pairs
28.10.2023
hard1220. Count Vowels Permutation
Count of n lengths paths according to graph rules a->e, e->(a, i), etc
26.10.2023
medium823. Binary Trees With Factors
Number of trees from arr where each k node has i and j leafs arr[k]=arr[j] arr[i]
21.10.2023
hard1425. Constrained Subsequence Sum
Max sum of subsequence i - j <= k
20.10.2023
medium341. Flatten Nested List Iterator
Implement graph iterator
18.10.2023
hard2050. Parallel Courses III
Shortest time to visit all nodes in relations=[from, to] graph
13.10.2023
easy746. Min Cost Climbing Stairs
Classic DP: climbing stairs
26.09.2023
medium316. Remove Duplicate Letters
Lexicographical smallest subsequence without duplicates
23.09.2023
medium1048. Longest String Chain
Longest chain of words with single character added
15.09.2023
medium1584. Min Cost to Connect All Points
Min manhatten distance connected graph
14.09.2023
hard332. Reconstruct Itinerary
Smallest lexical order path using all the tickets
13.09.2023
hard135. Candy
Minimum candies count to satisfy condition: ratings[i] < ratings[i-1] must give more candies to i-1
5.09.2023
medium138. Copy List with Random Pointer
Copy of a graph
26.08.2023
medium646. Maximum Length of Pair Chain
Max count non-overlaping intervals
25.08.2023
medium97. Interleaving String
Can a string be a merge of two other strings
23.08.2023
medium767. Reorganize String
Create non repeated subsequent chars string from string
20.08.2023
hard1203. Sort Items by Groups Respecting Dependencies
Sort items by groups and in groups given dependencies.
17.08.2023
medium542. 01 Matrix
Distances to 0 in an 0-1 matrix
04.08.2023
medium139. Word Break
If a word is a wordDict concatenation
03.08.2023
medium17. Letter Combinations of a Phone Number
Possible words from phone keyboard
02.08.2023
medium46. Permutations
List of all numbers permutations
29.07.2023
medium808. Soup Servings
Probability of soup A drained first or both A and B with 0.5 multiplier.
22.07.2023
medium688. Knight Probability in Chessboard
Probability of making k steps on a chessboard without stepping outside
21.07.2023
medium673. Number of Longest Increasing Subsequence
use an array cache, as Map gives TLE
13.07.2023
medium207. Course Schedule
If none edges in a cycle
12.07.2023
medium863. All Nodes Distance K in Binary Tree
List of k distanced from target nodes in a Binary Tree
11.07.2023
easy111. Minimum Depth of Binary Tree
Count nodes in the shortest path from root to leaf
2.07.2023
hard1601. Maximum Number of Achievable Transfer Requests
Max edges to make all counts in == out edges in graph
1.07.2023
medium2305. Fair Distribution of Cookies
Min of the max distributing n cookies to k children
29.06.2023
hard864. Shortest Path to Get All Keys
Min steps to collect all lowercase keys in matrix. # and uppercase locks are blockers.
28.06.2023
medium1514. Path with Maximum Probability
Max probability path from start to end in a probability edges graph
27.06.2023
medium373. Find K Pairs with Smallest Sums
List of increasing sum pairs a[i], b[j] from two sorted lists a, b
04.06.2023
medium547. Number of Provinces
Count connected groups in graph.
03.06.2023
medium1376. Time Needed to Inform All Employees
Total time from headID to all nodes in graph.
02.06.2023
medium2101. Detonate the Maximum Bombs
Count detonated bombs by chain within each radius.
01.06.2023
medium1091. Shortest Path in Binary Matrix
0 path length in a binary square matrix.
28.05.2023
hard1547. Minimum Cost to Cut a Stick
Min cost of cuts c1,..,ci,..,cn of [0..n] where cut cost the length = to-from.
21.05.2023
medium934. Shortest Bridge
Find the shortest path from one island of 1's to another.
20.05.2023
medium399. Evaluate Division
Given values for a/b and b/c find answers for a/c.
19.05.2023
medium785. Is Graph Bipartite?
Find if graph is bipartite
18.05.2023
medium1557. Minimum Number of Vertices to Reach All Nodes
Find all starting nodes in graph.
14.05.2023
hard1799. Maximize Score After N Operations
Max indexed-gcd-pair sum from 2n array; [3,4,6,8] -> 11 (1gcd(3,6) + 2gcd(4,8))
13.05.2023
medium2466. Count Ways To Build Good Strings
Let's write a DFS solution, adding zero or one and count the good strings.
11.05.2023
medium1035. Uncrossed Lines
use an array for the faster cache instead of HashMap
30.04.2023
hard1579. Remove Max Number of Edges to Keep Graph Fully Traversable
Use separate Union-Find objects for Alice and for Bob
29.04.2023
hard1697. Checking Existence of Edge Length Limited Paths
for better time complexity, compress the Union-Find path uf[x] = n
22.04.2023
hard1312. Minimum Insertion Steps to Make a String Palindrome
Just DFS and cache.
21.04.2023
hard879. Profitable Schemes
Do DFS and cache result in an array.
20.04.2023
medium662. Maximum Width of Binary Tree
We can do BFS and track node positions.
19.04.2023
medium1372. Longest ZigZag Path in a Binary Tree
Compute the max as you go
16.04.2023
hard1639. Number of Ways to Form a Target String Given a Dictionary
precompute the freq array - count of each character at each position
15.04.2023
hard2218. Maximum Value of K Coins From Piles
We can cache the result by the keys of every pile to taken
14.04.2023
medium516. Longest Palindromic Subsequence
For cleaner code
09.04.2023
hard1857. Largest Color Value in a Directed Graph
use visited set to detect cycles
08.04.2023
medium133. Clone Graph
we can avoid using visited set by checking if a new node already has filled its neighbors.
07.04.2023
medium1020. Number of Enclaves
We can use visited set, or modify the grid or use Union-Find.
06.04.2023
medium1254. Number of Closed Islands
DFS will solve the problem.
31.03.2023
hard1444. Number of Ways of Cutting a Pizza
carefully precompute prefix sum. You move by row, increasing sumX, then you move by column and reuse the result of the previous row.
30.03.2023
hard87. Scramble String
A simple approach is to concatenate strings, but in Kotlin it gives TLE, so we need bottom up approach, or just operate with indices.
28.03.2023
medium983. Minimum Cost For Tickets
Let's write DFS with memoization algorithm as it is simple to understand.
27.03.2023
medium64. Minimum Path Sum
Use DFS + memo, careful with the ending condition.
26.03.2023
hard2360. Longest Cycle in a Graph
Use separate visited sets for the current path and for the global visited nodes.
25.03.2023
medium2316. Count Unreachable Pairs of Nodes in an Undirected Graph
use path compression for better root time complexity
24.03.2023
medium1466. Reorder Routes to Make All Paths Lead to the City Zero
We can use data structure or just use sign to encode the direction.
23.03.2023
medium1319. Number of Operations to Make Network Connected
for the better time complexity of the findRoot use path compression: uf[x] = n
22.03.2023
medium2492. Minimum Score of a Path Between Two Cities
Connect all roads and update minimums in the Union-Find data structure. Use simple arrays for both connections and minimums.
15.03.2023
medium958. Check Completeness of a Binary Tree
Right depth must not be larger than left.
14.03.2023
medium129. Sum Root to Leaf Numbers
The most trivial way is to keep sum variable outside the dfs function.
13.03.2023
easy101. Symmetric Tree
Recursive: just write helper function.
27.02.2023
medium427. Construct Quad Tree
use inclusive ranges to simplify the code
26.02.2023
hard72. Edit Distance
Do DFS and use array for memoizing the result.
19.02.2023
medium103. Binary Tree Zigzag Level Order Traversal
for zigzag, we can skip a boolean variable and track result count.
16.02.2023
easy104. Maximum Depth of Binary Tree
Let's write a one-liner.
12.02.2023
medium2477. Minimum Fuel Cost to Report to the Capital
Use DFS and data class for the result.
11.02.2023
medium1129. Shortest Path with Alternating Colors
Start with two simultaneous points, one for red and one for blue. Keep track of the color.
10.02.2023
medium1162. As Far from Land as Possible
Add all land cells into BFS, then just run it.
31.01.2023
medium1626. Best Team With No Conflicts
We can use DFS to search all the possible teams and memorize the result in dp cache.
26.01.2023
medium787. Cheapest Flights Within K Stops
by the problem definition, path length is k+1, not just k
22.01.2023
medium131. Palindrome Partitioning
fun partition(s: String): List> {
21.01.2023
medium93. Restore IP Addresses
fun restoreIpAddresses(s: String): List
20.01.2023
medium491. Non-decreasing Subsequences
fun findSubsequences(nums: IntArray): List> {
13.01.2023
hard2246. Longest Path With Different Adjacent Characters
fun longestPath(parent: IntArray, s: String): Int {
12.01.2023
medium1519. Number of Nodes in the Sub-Tree With the Same Label
fun countSubTrees(n: Int, edges: Array
11.01.2023
medium1443. Minimum Time to Collect All Apples in a Tree
fun minTime(n: Int, edges: Array
30.12.2022
medium797. All Paths From Source to Target
fun allPathsSourceTarget(graph: Array> {
24.12.2022
medium790. Domino and Tromino Tiling
fun numTilings(n: Int): Int {
23.12.2022
medium309. Best Time to Buy and Sell Stock with Cooldown
data class K(val a:Int, val b: Boolean, val c:Boolean)
22.12.2022
hard834. Sum of Distances in Tree
fun sumOfDistancesInTree(n: Int, edges: Array
19.12.2022
easy1971. Find if Path Exists in Graph
fun validPath(n: Int, edges: Array
15.12.2022
medium1143. Longest Common Subsequence
fun longestCommonSubsequence(text1: String, text2: String): Int {
14.12.2022
medium198. House Robber
fun rob(nums: IntArray): Int {
11.12.2022
hard124. Binary Tree Maximum Path Sum
fun maxPathSum(root: TreeNode?): Int {
10.12.2022
medium1339. Maximum Product of Splitted Binary Tree
fun maxProduct(root: TreeNode?): Int {
9.12.2022
medium1026. Maximum Difference Between Node and Ancestor
fun maxAncestorDiff(root: TreeNode?): Int {
8.12.2022
easy872. Leaf-Similar Trees
fun leafSimilar(root1: TreeNode?, root2: TreeNode?): Boolean {
24.11.2022
medium79. Word Search
fun exist(board: Array
6.11.2022
hardWord Search Ii
Use trie + dfs
2.11.2022
mediumMinimum Genetic Mutation
make graph