Guide contents

Neetcode 250 (Java)

The NeetCode 250 problem set, grouped by the same categories as the NeetCode roadmap, each solved in Java with a brute force and an optimal solution.

250 lessons in 18 sections

Start with 217. Contains Duplicate →

Arrays & Hashing

Frequency counting, grouping, and lookups with hash maps and sets.

  1. 1.217. Contains Duplicate
  2. 2.242. Valid Anagram
  3. 3.1. Two Sum
  4. 4.49. Group Anagrams
  5. 5.347. Top K Frequent Elements
  6. 6.271. Encode and Decode Strings
  7. 7.238. Product of Array Except Self
  8. 8.36. Valid Sudoku
  9. 9.122. Best Time to Buy and Sell Stock II
  10. 10.128. Longest Consecutive Sequence
  11. 11.1929. Concatenation of Array
  12. 12.14. Longest Common Prefix
  13. 13.27. Remove Element
  14. 14.169. Majority Element
  15. 15.304. Range Sum Query 2D - Immutable
  16. 16.75. Sort Colors
  17. 17.560. Subarray Sum Equals K
  18. 18.705. Design HashSet
  19. 19.706. Design HashMap
  20. 20.912. Sort an Array
  21. 21.229. Majority Element II
  22. 22.41. First Missing Positive

Two Pointers

Scan from both ends or with two indices instead of nested loops.

  1. 23.125. Valid Palindrome
  2. 24.167. Two Sum II - Input Array Is Sorted
  3. 25.15. 3Sum
  4. 26.11. Container With Most Water
  5. 27.42. Trapping Rain Water
  6. 28.1768. Merge Strings Alternately
  7. 29.344. Reverse String
  8. 30.88. Merge Sorted Array
  9. 31.26. Remove Duplicates from Sorted Array
  10. 32.680. Valid Palindrome II
  11. 33.18. 4Sum
  12. 34.189. Rotate Array
  13. 35.881. Boats to Save People

Sliding Window

Maintain a moving range over an array or string instead of recomputing it.

  1. 36.121. Best Time to Buy and Sell Stock
  2. 37.3. Longest Substring Without Repeating Characters
  3. 38.424. Longest Repeating Character Replacement
  4. 39.567. Permutation in String
  5. 40.76. Minimum Window Substring
  6. 41.239. Sliding Window Maximum
  7. 42.219. Contains Duplicate II
  8. 43.209. Minimum Size Subarray Sum
  9. 44.658. Find K Closest Elements

Stack

Last-in, first-out problems: matching pairs, monotonic stacks, and expression evaluation.

  1. 45.20. Valid Parentheses
  2. 46.155. Min Stack
  3. 47.150. Evaluate Reverse Polish Notation
  4. 48.22. Generate Parentheses
  5. 49.739. Daily Temperatures
  6. 50.853. Car Fleet
  7. 51.84. Largest Rectangle in Histogram
  8. 52.682. Baseball Game
  9. 53.225. Implement Stack using Queues
  10. 54.232. Implement Queue using Stacks
  11. 55.735. Asteroid Collision
  12. 56.901. Online Stock Span
  13. 57.71. Simplify Path
  14. 58.394. Decode String
  15. 59.895. Maximum Frequency Stack

Binary Search

Halve the search space on sorted data or on a monotonic answer.

  1. 60.704. Binary Search
  2. 61.74. Search a 2D Matrix
  3. 62.875. Koko Eating Bananas
  4. 63.153. Find Minimum in Rotated Sorted Array
  5. 64.33. Search in Rotated Sorted Array
  6. 65.981. Time Based Key-Value Store
  7. 66.4. Median of Two Sorted Arrays
  8. 67.35. Search Insert Position
  9. 68.374. Guess Number Higher or Lower
  10. 69.69. Sqrt(x)
  11. 70.1011. Capacity To Ship Packages Within D Days
  12. 71.81. Search in Rotated Sorted Array II
  13. 72.410. Split Array Largest Sum
  14. 73.1095. Find in Mountain Array

Linked List

Pointer manipulation, fast and slow pointers, and designs built on linked lists.

  1. 74.206. Reverse Linked List
  2. 75.21. Merge Two Sorted Linked Lists
  3. 76.141. Linked List Cycle Detection
  4. 77.143. Reorder List
  5. 78.19. Remove Node From End of Linked List
  6. 79.138. Copy List With Random Pointer
  7. 80.2. Add Two Numbers
  8. 81.287. Find the Duplicate Number
  9. 82.146. LRU Cache
  10. 83.23. Merge K Sorted Linked Lists
  11. 84.25. Reverse Nodes In K-Group
  12. 85.92. Reverse Linked List II
  13. 86.622. Design Circular Queue
  14. 87.460. LFU Cache

Trees

Binary tree recursion, traversals, and binary search trees.

  1. 88.226. Invert Binary Tree
  2. 89.104. Maximum Depth of Binary Tree
  3. 90.543. Diameter of Binary Tree
  4. 91.110. Balanced Binary Tree
  5. 92.100. Same Binary Tree
  6. 93.572. Subtree of Another Binary Tree
  7. 94.235. Lowest Common Ancestor in Binary Search Tree
  8. 95.102. Binary Tree Level Order Traversal
  9. 96.199. Binary Tree Right Side View
  10. 97.1448. Count Good Nodes in Binary Tree
  11. 98.98. Validate Binary Search Tree
  12. 99.230. Kth Smallest Integer in BST
  13. 100.105. Construct Binary Tree From Preorder and Inorder Traversal
  14. 101.124. Binary Tree Maximum Path Sum
  15. 102.297. Serialize and Deserialize Binary Tree
  16. 103.94. Binary Tree Inorder Traversal
  17. 104.144. Binary Tree Preorder Traversal
  18. 105.145. Binary Tree Postorder Traversal
  19. 106.701. Insert into a Binary Search Tree
  20. 107.450. Delete Node in a BST
  21. 108.427. Construct Quad Tree
  22. 109.337. House Robber III
  23. 110.1325. Delete Leaves With a Given Value

Tries

Prefix trees for fast prefix lookup, wildcard search, and searching many words at once.

  1. 111.208. Implement Trie (Prefix Tree)
  2. 112.211. Design Add and Search Words Data Structure
  3. 113.212. Word Search II
  4. 114.2707. Extra Characters in a String

Heap / Priority Queue

Repeatedly pull out the smallest or largest item, or keep only the top k.

  1. 115.703. Kth Largest Element in a Stream
  2. 116.1046. Last Stone Weight
  3. 117.973. K Closest Points to Origin
  4. 118.215. Kth Largest Element in an Array
  5. 119.621. Task Scheduler
  6. 120.355. Design Twitter
  7. 121.295. Find Median From Data Stream
  8. 122.767. Reorganize String
  9. 123.1834. Single-Threaded CPU
  10. 124.1405. Longest Happy String
  11. 125.1094. Car Pooling
  12. 126.502. IPO

Backtracking

Explore choices depth first, undo them, and prune dead ends.

  1. 127.78. Subsets
  2. 128.39. Combination Sum
  3. 129.40. Combination Sum II
  4. 130.46. Permutations
  5. 131.90. Subsets II
  6. 132.79. Word Search
  7. 133.131. Palindrome Partitioning
  8. 134.17. Letter Combinations of a Phone Number
  9. 135.51. N-Queens
  10. 136.1863. Sum of All Subset XOR Totals
  11. 137.77. Combinations
  12. 138.47. Permutations II
  13. 139.698. Partition to K Equal Sum Subsets
  14. 140.473. Matchsticks to Square
  15. 141.52. N-Queens II
  16. 142.140. Word Break II

Graphs

Grid and graph traversal with DFS, BFS, topological sort, and union-find.

  1. 143.200. Number of Islands
  2. 144.133. Clone Graph
  3. 145.695. Max Area of Island
  4. 146.417. Pacific Atlantic Water Flow
  5. 147.130. Surrounded Regions
  6. 148.994. Rotting Oranges
  7. 149.286. Walls and Gates
  8. 150.207. Course Schedule
  9. 151.210. Course Schedule II
  10. 152.684. Redundant Connection
  11. 153.323. Number of Connected Components in an Undirected Graph
  12. 154.261. Graph Valid Tree
  13. 155.127. Word Ladder
  14. 156.463. Island Perimeter
  15. 157.953. Verifying an Alien Dictionary
  16. 158.997. Find the Town Judge
  17. 159.752. Open the Lock
  18. 160.1462. Course Schedule IV
  19. 161.721. Accounts Merge
  20. 162.399. Evaluate Division
  21. 163.310. Minimum Height Trees

Advanced Graphs

Shortest paths, minimum spanning trees, Eulerian paths, and topological ordering.

  1. 164.332. Reconstruct Itinerary
  2. 165.1584. Min Cost to Connect All Points
  3. 166.743. Network Delay Time
  4. 167.778. Swim in Rising Water
  5. 168.269. Alien Dictionary
  6. 169.787. Cheapest Flights Within K Stops
  7. 170.1631. Path With Minimum Effort
  8. 171.1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
  9. 172.2392. Build a Matrix With Conditions
  10. 173.2709. Greatest Common Divisor Traversal

1-D Dynamic Programming

Break a problem into overlapping subproblems indexed by one variable.

  1. 174.70. Climbing Stairs
  2. 175.746. Min Cost Climbing Stairs
  3. 176.198. House Robber
  4. 177.213. House Robber II
  5. 178.5. Longest Palindromic Substring
  6. 179.647. Palindromic Substrings
  7. 180.91. Decode Ways
  8. 181.322. Coin Change
  9. 182.152. Maximum Product Subarray
  10. 183.139. Word Break
  11. 184.300. Longest Increasing Subsequence
  12. 185.416. Partition Equal Subset Sum
  13. 186.1137. N-th Tribonacci Number
  14. 187.279. Perfect Squares
  15. 188.377. Combination Sum IV
  16. 189.343. Integer Break
  17. 190.1406. Stone Game III

2-D Dynamic Programming

Grid, string-pair, and interval DP with two changing indices.

  1. 191.62. Unique Paths
  2. 192.1143. Longest Common Subsequence
  3. 193.309. Best Time to Buy and Sell Stock with Cooldown
  4. 194.518. Coin Change II
  5. 195.494. Target Sum
  6. 196.97. Interleaving String
  7. 197.329. Longest Increasing Path in a Matrix
  8. 198.115. Distinct Subsequences
  9. 199.72. Edit Distance
  10. 200.312. Burst Balloons
  11. 201.10. Regular Expression Matching
  12. 202.64. Minimum Path Sum
  13. 203.63. Unique Paths II
  14. 204.877. Stone Game
  15. 205.1049. Last Stone Weight II
  16. 206.1140. Stone Game II

Greedy

Make the locally best choice at each step and prove it is safe.

  1. 207.53. Maximum Subarray
  2. 208.55. Jump Game
  3. 209.45. Jump Game II
  4. 210.134. Gas Station
  5. 211.846. Hand of Straights
  6. 212.1899. Merge Triplets to Form Target Triplet
  7. 213.763. Partition Labels
  8. 214.678. Valid Parenthesis String
  9. 215.860. Lemonade Change
  10. 216.918. Maximum Sum Circular Subarray
  11. 217.978. Longest Turbulent Subarray
  12. 218.1871. Jump Game VII
  13. 219.649. Dota2 Senate
  14. 220.135. Candy

Intervals

Sort by start or end and sweep to merge, insert, or count overlaps.

  1. 221.57. Insert Interval
  2. 222.56. Merge Intervals
  3. 223.435. Non-overlapping Intervals
  4. 224.252. Meeting Rooms
  5. 225.253. Meeting Rooms II
  6. 226.1851. Minimum Interval to Include Each Query
  7. 227.2402. Meeting Rooms III

Math & Geometry

Matrix transformations, number manipulation, and coordinate tricks.

  1. 228.48. Rotate Image
  2. 229.54. Spiral Matrix
  3. 230.73. Set Matrix Zeroes
  4. 231.202. Happy Number
  5. 232.66. Plus One
  6. 233.50. Pow(x, n)
  7. 234.43. Multiply Strings
  8. 235.2013. Detect Squares
  9. 236.13. Roman to Integer
  10. 237.168. Excel Sheet Column Title
  11. 238.1071. Greatest Common Divisor of Strings
  12. 239.2807. Insert Greatest Common Divisors in Linked List
  13. 240.867. Transpose Matrix

Bit Manipulation

XOR tricks, shifts, and masks on integers.

  1. 241.136. Single Number
  2. 242.191. Number of 1 Bits
  3. 243.338. Counting Bits
  4. 244.190. Reverse Bits
  5. 245.268. Missing Number
  6. 246.371. Sum of Two Integers
  7. 247.7. Reverse Integer
  8. 248.67. Add Binary
  9. 249.201. Bitwise AND of Numbers Range
  10. 250.3133. Minimum Array End