Giter VIP home page Giter VIP logo

leetcode's Introduction

LeetCode

Below is a summary of my solutions to the problems on LeetCode. The main idea of each problem can usually be found at the beginning of the corresponding source file. I will keep updating the list and feel free to share any of your thoughts!

# Title Solutions
546 Remove Boxes [ O(n^4)-time DP ]
545 Boundary of Binary Tree [ (Careful) Tree Traversal ]
544 Output Contest Matches [ Brute-force | O(n)-time Solution ]
543 Diameter of Binary Tree [ DP ]
542 01 Matrix [ BFS ]
541 Reverse String II [ Brute-force ]
539 Minimum Time Difference [ Sorting ]
538 Convert BST to Greater Tree [ Right-root-left-traversal ]
537 Complex Number Multiplication [ Brute-force ]
536 Construct Binary Tree from String [ LL(1)-parser ]
533 Lonely Pixel II [ Solution ]
532 K-diff Pairs in an Array [ O(n)-time Solution | Two-pointer ]
531 Lonely Pixel I [ Brute-force ]
530 Minimum Absolute Difference in BST [ DFS ]
529 Minesweeper [ DFS ]
527 Word Abbreviation [ Brute-force ]
526 Beautiful Arrangement [ Permutation ]
525 Contiguous Array [ Prefix Sum | Divide-and-conquer ]
524 Longest Word in Dictionary through Deleting [ Greedy ]
523 Continuous Subarray Sum [ Prefix Sum ]
520 Detect Capital [ Regex ]
517 Super Washing Machines [ O(n)-time Solution ]
515 Find Largest Value in Each Tree Row [ DFS ]
514 Freedom Trail [ DP | Faster DP ]
513 Find Bottom Left Tree Value [ DFS ]
509 Most Frequent Subtree Sum [ Tree Traversal ]
507 Perfect Number [ O(sqrt(n))-time Solution ]
506 Relative Ranks [ Sorting ]
505 The Maze II [ DFS + Pruning | A* Search | DFS + Multithreading ]
504 Base 7 [ 1-line Solution | Conventional Method ]
503 Next Greater Element II [ Monotone Stack ]
502 IPO [ Greedy ]
501 Find Mode in Binary Tree [ DFS + HashMap | O(1)-extra-space Solution ]
500 Keyboard Row [ 1-Line Solution ]
499 The Maze III [ A* | DFS ]
498 Diagonal Traversal [ Sorting | Direct Approach ]
496 Next Greater Element I [ Brute-force ]
495 Teemo Attacking [ Sweepline ]
494 Target Sum [ Knapsack ]
493 Reverse Pairs [ Merge-sort | Merge-sort Shorter Code | BIT ]
492 Construct the Rectangle [ Brute-force ]
491 Increasing Subsequences [ Brute-force ]
490 The Maze [ DFS ]
488 Zuma Game [ BFS ]
487 Max Consecutive Ones II [ O(1)-space Solution ]
486 Predict the Winner [ DP + Game Theory | Shorter Code ]
485 Max Consecutive Ones [ Brute-force ]
484 Find Permutation [ Topological-sort | Constructive Approach ]
483 Smallest Good Base [ Brute-force + Binary Search ]
482 License Key Formatting [ Short Solution ]
481 Magical String [ BFS ]
480 Sliding Window Median [ Order Statistic Tree via Fenwick Tree ]
477 Total Hamming Distance [ Bit-by-bit Counting ]
476 Number Complement [ One-line Solution ]
475 Heaters [ Direct Approach | Binary Search ]
474 One and Zeroes [ 0/1-Knapsack ]
473 Matchsticks to Square [ Backtracking ]
472 Concatenated Words [ DP + Trie ]
471 Encode String with Shortest Length [ DP ]
469 Convex Polygon [ Cross Product ]
468 Validate IP Address [ Solution ]
467 Unique Substring in Wraparound String [ String Breaking and Encoding ]
466 Count the Repetitions [ DP ]
465 Optimal Account Balancing [ Subset-Sum ]
464 Can I Win [ DP + Game Theory ]
463 Island Perimeter [ Brute-force ]
462 Minimum Moves to Equal Array Elements II [ Median Finding + Quick-Selection ]
461 Hamming Distance [ Brute-force ]
459 Repeated Substring Pattern [ O(n^1.5) Solution | 1-Liner | 1-Liner via Regex | Miller-Rabin | KMP ]
456 123 Pattern [ O(n log n) Solution via BST ]
455 Assign Cookies [ Greedy ]
454 4Sum II [ HashMap ]
453 Minimum Moves to Equal Array Elements [ Solution ]
452 Minimum Number of Arrows to Burst Balloons [ Greedy via Sweepline ]
451 Sort Characters by Frequency [ Counting-sort ]
450 Delete Node in a BST [ Iterative Solution | Shorter Solution ]
449 Serialize and Deserialize BST [ Pre-order Traversal | Optimized Version ]
448 Find All Numbers Disappeared in an Array [ Cycle-finding ]
447 Number of Boomerangs [ Counting via HashMap ]
446 Arithmetic Slices II - Subsequence [ Very Fast O(n^3) DP Solution | O(n^2) DP Solution with Pruning ]
444 Sequence Reconstruction [ Topological Sort ]
442 Find All Duplicates in an Array [ Mod Trick ]
441 Arranging Coins [ O(1) Brute-force ]
440 K-th Smallest in Lexicographical Order [ Counting + Greedy ]
439 Ternary Expression Parser [ Top-down Approach ]
438 Find All Anagrams in a String [ Sliding-window ]
437 Path Sum III [ One-pass DFS | One-pass DFS Version 2 | Two-pass DFS ]
436 Find Right Interval [ TreeMap ]
435 Non-overlapping Intervals [ Greedy | Activity Selection ]
435 Number of Segments in a String [ Regex ]
433 Minimum Genetic Mutation [ BFS ]
432 All O`one Data Structure [ HashMap + Doubly Circular Linked List ]
425 Word Squares [ Backtracking + Pruning with Prefix Structure | Hueristic Backtracking + Pruning with BitSet ]
424 Longest Repeating Character Replacement [ Binary Search | Sliding-window ]
423 Reconstruct Original Digits from English [ Greedy ]
422 Valid World Square [ Brute-force ]
421 Maximum XOR of Two Numbers in an Array [ Binary Trie ]
420 Strong Password Checker [ Stupid Solution ]
419 Battleships in a Board [ Single-pass + In-place + No modification ]
418 Sentence Screen Fitting [ Simulation ]
417 Pacific Atlantic Water Flow [ Graph Reachability | DP + Vertex Contraction ]
416 Partition Equal Subset Sum [ 0/1-Knapsack ]
415 Add Strings [ BigInteger Addition ]
414 Third Maximum Number [ One-pass Solution ]
413 Arithmetic Slices [ Solution | In-place Solution ]
412 Fizz Buzz [ Brute-force ]
411 Minimum Unique Word Abbreviation [ Brute-force + Pruning ]
410 Split Array Largest Sum [ Bisect + Greedy ]
409 Longest Palindrome [ Counting ]
408 Valid Word Abbreviation [ Brute-force ]
407 Trapping Rain Water II [ Priority Queue | Dijkstra | SPFA ]
406 Queue Reconstruction by Height [ Greedy + Sorting ]
405 Convert a Number to Hexadecimal [ Bit Manipulation ]
404 Sum of Left Leaves [ DFS ]
403 Frog Jump [ DP ]
402 Remove K Digits [ Greedy + Monotone Stack ]
401 Binary Watch [ Brute-force 1 | Brute-force 2]
400 Nth Digit [ Counting + Bisect ]
399 Evaluate Division [ Floyed | DFS ]
398 Random Pick Index [ Solution1 | Solution2 | Solution3 ]
397 Integer Replacement [ BFS ]
396 Rotate Fucntion [ Solution ]
395 Longest Substring with At Least K Repeating Characters [ O(256N) Solution | Solution with Pruning ]
394 Decode String [ Top-down Approach ]
393 UTF-8 Validation [ Solution ]
392 Is Subsequence [ Solution ]
391 Perfect Rectangle [ Sweepline + TreeMap | Sweepline + Segment Tree ]
390 Elimination Game [ O(log n) Math ]
389 Find the Difference [ Solution ]
388 Longest Absolute File Path [ Using a Stack | Recursively Solution ]
387 First Unique Character in a String [ Brute-Force ]
386 Lexicographical Numbers [ Using a stack | Recursive Solution ]
385 Mini Parser [ Char-by-char Approach | Using Scanner ]
384 Shuffle an Array [ Solution ]
383 Ransom Note [ Solution ]
381 Insert Delete GetRandom O(1) Duplicate Allowed [ Solution ]
379 Design Phone Directory [ Simple Solution ]
378 Kth Smallest Element in a Sorted Matrix [ Bisect + Young Tableau ]
377 Combination Sum IV [ DP ]
376 Wiggle Subsequence [ O(n) Climbing Mountain | O(n) In-place Maximal/Minimal Finding ]
375 Guess Number Higher or Lower II [ O(n^3) DP ]
374 Guess Number Higher or Lower [ Binary Search ]
373 Find K Pairs with Smallest Sums [ O(k log k) Solution | O(k log(min(k, m, n)) + min(k, m, n)) Solution ]
372 Super Pow [ Most-significant Bit -> Least-significant Bit | Least-significant Bit -> Most-significant Bit ]
371 Sum of Two Integers [ Bit Manipulation ]
370 Range Addition [ Fenwick Tree | Sweepline | O(n) Prefix Trick | Segment Tree ]
369 Plus One Linked List🔒
368 Largest Divisible Subset [ DP ]
367 Valid Perfect Square [ Binary Search ]
366 Find Leaves of Binary Tree [ Computing Tree Height ]
365 Water and Jug Problem [ Math ]
364 Nested List Weight Sum II🔒
363 Max Sum of Rectangle No Larger Than K [ BST ]
362 Design Hit Counter [ Sliding Window ]
361 Bomb Enemy [ DP ]
360 Sort Transformed Array [ O(n)-time Solution ]
359 Logger Rate Limiter [ Simple Solution | Sliding Window ]
358 Rearrange String k Distance Apart [ Greedy + Sliding Window ]
357 Count Numbers with Unique Digits [ Product Rule ]
356 Line Reflection [ In-place Solution ]
355 Design Twitter [ k-way Merging ]
354 Russian Doll Envelopes [ Longest Path | Longest Increasing Subsequence ]
353 Design Snake Game [ Deque + HashTable ]
352 Data Stream as Disjoint Intervals [ BST ]
351 Android Unlock Patterns🔒
350 Intersection of Two Arrays II [ Mergesort ]
349 Intersection of Two Arrays [ Java Stream ]
348 Design Tic-Tac-Toe [ O(1)-time Solution ]
347 Top K Frequent Elements [ Map+QuickSelection | Map+CountingSort ]
346 Moving Average from Data Stream [ Sliding Window ]
345 Reverse Vowels of a String [ Two-Pointer ]
344 Reverse String [ String ]
343 Integer Break [ Math/DP ]
342 Power of Four [ Bit Manipulate ]
341 Flatten Nested List Iterator [ Stack/DFS | Lazier ]
340 Longest Substring with At Most K Distinct Characters [ O(n) Sliding Window | O(n log n) Bisect | Slinding Window Shorter Code | Sliding Window for Streaming Data (1 pass) ]
339 Nested List Weight Sum [ DFS ]
338 Counting Bits [ Counting ]
337 House Robber III [ Tree DP ]
336 Palindrome Pairs [ A Naive Solution ]
335 Self Crossing [ Geometry ]
334 Increasing Triplet Subsequence [ LIS ]
333 Largest BST Subtree [ Tree-DP ]
332 Reconstruct Itinerary [ Euler Path ]
331 Verify Preorder Serialization of a Binary Tree [ Using a Stack | Property of Full Binary Tree ]
330 Patching Array [ Greedy Algorithm ]
329 Longest Increasing Path in a Matrix [ O(nm) DP ]
328 Odd Even Linked List [ O(n) in-place ]
327 Count of Range Sum [ O(n log n) solution via Fenwick Tree ]
326 Power of Three [ An interesting O(log log n) solution ]
325 Maximum Size Subarray Sum Equals k [ Prefix-sum + HashTable ]
324 Wiggle Sort II [ Median Finding, Dutch National Flag ]
323 Number of Connected Components in an Undirected Graph [ DFS ]
322 Coin Change [ Knapsack ]
321 Create Maximum Number [ Stack + Greedy ]
320 Generalized Abbreviation [ Bit-manipulation ]
319 Bulb Switcher [ O(1) Math ]
318 Maximum Product of Word Lengths [ O(n^2) brute-force ]
317 Shortest Distance from All Buildings🔒
316 Remove Duplicate Letters [ O(n) Greedy Solution | O(n) Solution via Stack ]
315 Count of Small Numbers After Self [ Discretization + Fenwick Tree | Mergesort ]
314 Binary Tree Vertical Order TGraversal [ BFS | BFS (slower, but shorter) ]
313 Super Ugly Number [ O(nk) Constructive Algorithm ]
312 Burst Balloons [ O(n^3) DP ]
311 Sparse Matrix Multiplication🔒
310 Minimum Height Trees [ Longest Path in Tree | Tree DP ]
309 Best Time to Buy and Sell Stock with Cooldown [ O(n^2) DP | O(n) DP ]
308 Range Sum Query 2D - Mutable [ 2D-Sqrt Partition | 2D Fenwick Tree ]
307 Range Sum Query - Mutable [ sqrt(n) Trick | Fenwick Tree | Segment Tree ]
306 Additive Number [ Fibonacci Sequence ]
305 Number of Islands II [ Union-Find Set ]
304 Range Sum Query 2D - Immutable [ Inclusion-Exclusion ]
303 Range Sum Query - Immutable [ Prefix Sum ]
302 Smallest Rectangle Enclosing Black Pixels [ DFS ]
301 Remove Invalid Parentheses [ Backtracking | DP ]
298 Binary Tree Longest Consecutive Sequence [ Tree-DP | DFS | Another DFS Solution]
297 Serialize and Deserialize Binary Tree [ DFS ]
294 Flip Game II [ Nim + Sprague-Grundy Theorem ]
293 Flip Game [ Brute-force ]
291 Word Pattern II [ Backtracking | Backtracking + Pruning ]
289 Game of Life [ In-place Solution | Infinite Board ]
288 Unique Word Abbreviation [ HashMap ]
286 Walls and Gates [ BFS]
285 Inorder Successor in BST [ Top-down Approach | Using Parent Pointers ]
283 Move Zeroes [ Partition | Cheating ]
281 Zigzag Iterator [ Solution ]
280 Wiggle Sort [ One-pass Solution ]
277 Find the Celebrity [ Universal Sink ]
276 Paint Fence [ Combinatorial ]
274 H-Index [ Counting Sort ]
272 Closest Binary Search Tree Value II [ Predesessor + Successor in BST ]
271 Encode and Decode Strings [ Escaping Character | Trie Serialization/Deserialization ]
270 Closest Binary Search Tree Value [ Solution ]
269 Alien Dictionary [ Topological Sort (BFS) | BFS with Smaller Constant | Topological Sort (DFS)]
267 Palindrome Permutation II [ Counting + Next Permutation ]
266 Palindrome Permutation [ Counting ]
265 Paint House II [ O(nk) DP ]
262 Trips and Users [ Solution ]
261 Graph Valid Tree [ Union-find Set ]
259 3Sum Smaller [ Two-pointer ]
256 Paint House [ DP ]
255 Verify Preorder Sequence in Binary Search Tree [ Construction Method | RMQ + Binary Search ]
254 Factor Combinations [ Backtracking ]
253 Meeting Rooms II [ Sweepline Algorithm ]
252 Meeting Rooms [ Sweepline ]
251 Flatten 2D Vector [ Iterator ]
250 Count Univalue Subtrees [ DFS ]
249 Group Shifted Strings [ Keyed-by ]
246 Strobogrammatic Number [ Brute-force ]
245 Shortest Word Distance III [ Linear Scan ]
244 Shortest Word Distance II [ Solution ]
218 The Skyline Problem [ Sweepline | Java Code]
214 Shortest Palindrome [ Rabin-Karp rolling hash | KMP ]
212 Word Search II [ DP + Trie ]
206 Reverse Linked List [ Solution ]
197 Rising Temperature [ Solution ]
196 Delete Duplicate Emails [ Solution ]
186 Reverse Words in a String II [ In-place Solution ]
184 Department Highest Salary [ Solution ]
183 Customers Who Never Order [ Solution ]
182 Duplicate Emails [ Solution ]
181 Employees Earning More Than Their Managers [ Solution ]
180 Consecutive Numbers [ Solution ]
178 Rank Scores [ Solution ]
177 Nth Highest Salary [ Solution ]
176 Second Highest Salary [ Solution ]
175 Combine Two Tables [ Solution ]
170 Two Sum III - Data structure design [ Hash Table ]
167 Two Sum II - Input array is sorted [ Two-pointer ]
165 Compare version Numbers [ Lexicographical Order ]
163 Missing Ranges [ Sorting ]
161 One Edit Distance [ Short Java Code ]
159 Longest Substring with At Most Two Distinct Characters [ Sliding-window ]
158 Read N Characters Given Read4 II - Call multiple times [ Reader ]
157 Read N Characters Given Read4 [ Reader ]
156 Binary Tree Upside Down [ Post-order Traversal ]
155 Min Stack [ More Memory, Shorter Code | As Little Memory as Possible ]
151 Reverse Words in a String [ Java Solution | C In-place Solution ]
146 LRU Cache [ Doubly Connected Linked List + Hash Table ]
140 Word Break II [ DP + DFS ]
139 Word Break [ Short DP | DP + Trie ]
136 Single Number [ Xor | Randomized Partition ]
128 Longest Consecutive Sequence [ O(n) HashSet ]
127 Word Ladder [ BFS ]
96 Unique Binary Search Trees [ DP ]
79 Word Search [ DFS ]
76 Minimum Window Substring [ Sliding-window ]
72 Edit Distance [ DP ]
47 Permutations II [ next_permutation ]
44 Wildcard Matching [ DP | Greedy ]
40 Combination Sum II [ DP Knapsack ]
39 Combination Sum [ DP + Backtracking ]
37 Sudoku Solver [ Greedy Backtracking Using Bitmask | Greedy Backtracking ]
36 Valid Sudoku [ Solution ]
22 Generate Parentheses [ DP | Recursion ]
10 Regular Expression Matching [ DP ]
4 Median of Two Sorted Arrays [ Binary Search ]
2 Add Two Numbers [ Not in-place | In-place ]
1 Two Sum [ Two Pointers | Hash Table ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.