Skip to content

Working my brain a bit. My leetcode.com trainings in Swift.

License

Notifications You must be signed in to change notification settings

atereshkov/leetcode-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 

Repository files navigation

leetcode

Table of Contents

  1. Arrays & Hashing
  2. Two Pointers
  3. Stack
  4. Binary Search
  5. Sliding Window
  6. Linked List
  7. Trees
  8. Heap / Priority Queue
  9. Intervals
  10. Greedy
  11. Backtracking
  12. Graphs
  13. Advanced Graphs
  14. 1-D DP
  15. 2-D DP
  16. Bit Manipulation
  17. Math & Geometry

Arrays & Hashing

Problem Leetcode Difficulty Topics Notes
217 - Contains Duplicate Leetcode 🟢 Array, Hash Table, Sorting
⚠️ NotesUse dictionary (hashmap) to check for duplicates
242 - Valid Anagram Leetcode 🟢 Hash Table, String, Sorting
⚠️ NotesTwo dictionaries (hashmaps), char:count and then compare two hashmaps
1 - Two Sum Leetcode 🟢 Array, Hash Table
⚠️ NotesTBD
49 - Group Anagrams Leetcode 🟡 Array, Hash Table, String, Sorting
⚠️ NotesUse dictionary (hashmap) with sorted letters of a word (key) and array of anagrams (value)
347 - Top K Frequent Elements Leetcode 🟡 Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect TBD
238 - Product of Array Except Self Leetcode 🟡 Array, Prefix Sum
⚠️ NotesTricky to understand the principle. Make two passes to compute products: first in-order, second in-reverse.
36 - Valid Sudoku Leetcode 🟡 Array, Hash Table, Matrix
⚠️ NotesUse 3 hashmaps (dictionaries): rows, columns and subsets (3x3, use 'row / 3', 'column / 3' as a key). Check for duplicates in sets
271 - Encode and Decode Strings Leetcode 🟡 TBD
⚠️ NotesSeparator is a word length + a sign (e.g., 5,3#hellohey). Loop through the counts, not through each character and append words calculating start/end indexes.
128 - Longest Consecutive Sequence Leetcode 🟡 Array, Hash Table, Union Find
⚠️ NotesLoop and find out if the num is a start of a sequence (nums[i] - 1 tells that it's a lowest value of a sequence). Then calculate the length of the sequence using while and find the longest sequence.

Two Pointers

Problem Leetcode Difficulty Topics Notes
125 - Valid Palindrome Leetcode 🟢 Two Pointers, String
⚠️ NotesLeft and right pointer. Compare left/right chars (lowercased) in while. Increase left or decrease right pointer unless there's a letter/number (ascii) char found to compare.
167 - Two Sum II - Input Array Is Sorted Leetcode 🟡 Array, Two Pointers, Binary Search
⚠️ NotesIn while calculate the sum (left and right pointers). If sum > target, decrease right pointer. If sum < target, increase left pointer. If sum == target, return.
15 - 3Sum Leetcode 🟡 Array, Two Pointers, Sorting
⚠️ NotesSimilar to the "2 Sum" approach. Sort the array first. Skip dups while iterating. Then apply "2 Sum" method, for each num and update left pointer (while no dup value found).
11 - Container With Most Water Leetcode 🟡 Array, Two Pointers, Greedy
⚠️ NotesThe left p is the start, and the right p is the end of the array. Calculate the max area ((r - l) * min(h[l], h[r])) and update the left or right pointer depending on whether h[l] is < or > than h[r].
42 - Trapping Rain Water Leetcode 🔴 Array, Two Pointers, DP, Stack, Monotonic Stack
⚠️ NotesTBD

Stack

Problem Leetcode Difficulty Topics Notes
20 - Valid Parentheses Leetcode 🟢 String, Stack
⚠️ NotesUse simple stack structure (Character). Declare Char:Char hashmap [")": "(", "}": "{", "]": "["]. Go through the string and check if char is an open bracket (if so, push). If not, check if top char is an open bracket for char (if yes, then pop; if not, then return false).
155 - Min Stack Leetcode 🟡 Stack, Design
⚠️ NotesStore two arrays (one for numbers, another for min values). push - append value to stack & append minStack value (min(val, minStack.last ?? val)). pop should pop from both. top - return last from stack, getMin - last from minStack.
150 - Evaluate Reverse Polish Notation Leetcode 🟡 Array, Math, Stack
⚠️ NotesIterate through the strings and use switch. In case of an operator, pop 2 values, calcuate the expression (mind the order) and push to the stack. In case of a number (default case), just push it to the stack. Return top value.
22 - Generate Parentheses Leetcode 🟡 String, DP, Backtracking
⚠️ NotesImplement a backtracking function and call it recursively. Pass n, openN, closedN, char stack and res array. In it, check for the valid result first (n == openN == closedN), join stack, append to res and return. Push open paranthesis if open < n, backtrack (openN + 1) and pop. Push close paranthesis if closedN < openN, backtrack (closedN +1) and pop.
739 - Daily Temperatures Leetcode 🟡 Array, Stack, Monotonic Stack
⚠️ NotesTBD to deep dive
853 - Car Fleet Leetcode 🟡 Array, Stack, Sorting, Monotonic Stack
⚠️ NotesNotes
84 - Largest Rectangle In Histogram Leetcode 🔴 TBD
⚠️ NotesTBD

Binary Search

Problem Leetcode Difficulty Topics Notes
704 - Binary Search Leetcode 🟢 Array, Binary Search
⚠️ NotesTBD
74 - Search a 2D Matrix Leetcode 🟡 Array, Binary Search, Matrix
⚠️ NotesTBD
875 - Koko Eating Bananas Leetcode 🟡 Binary Search, Binary Tree
⚠️ NotesTBD

Sliding Window

Problem Leetcode Difficulty Topics Notes
121 - Best Time to Buy and Sell Stock Leetcode 🟢 Array, Dynamic Programming
⚠️ NotesTBD
3 - Longest Substring Without Repeating Characters Leetcode 🟡 Hash Table, String, Sliding Window
⚠️ NotesTBD

Linked List

Problem Leetcode Difficulty Topics Notes
206 - Reverse Linked List Leetcode 🟢 Linked List, Recursion
⚠️ NotesTBD
21 - Merge Two Sorted Lists Leetcode 🟢 Linked List, Recursion
⚠️ NotesTBD
143 - Reorder List Leetcode 🟡 Linked List, Two Pointers, Stack, Recursion
⚠️ NotesTBD
19 - Remove Nth Node From End of List Leetcode 🟡 Linked List, Two Pointers
⚠️ NotesTBD

Trees

Problem Leetcode Difficulty Topics Notes
226 - Invert Binary Tree Leetcode 🟢 Tree, Depth-First Search, Breadth-First Search, Binary Tree
⚠️ NotesTBD
104 - Maximum Depth of Binary Tree Leetcode 🟢 Tree, Depth-First Search, Breadth-First Search, Binary Tree
⚠️ NotesTBD
543 - Diameter of Binary Tree Leetcode 🟢 Tree, Depth-First Search, Binary Tree
⚠️ NotesTBD
110 - Balanced Binary Tree Leetcode 🟢 Tree, Depth-First Search, Binary Tree
⚠️ NotesTBD
100 - Same Tree Leetcode 🟢 Tree, Depth-First Search, Binary Tree
⚠️ NotesTBD
572 - Subtree of Another Tree Leetcode 🟢 Tree, Depth-First Search, Binary Tree
⚠️ NotesTBD

Heap / Priority Queue

Intervials

Greedy

Backtracking

Graphs

Advanced Graphs

1-D DP

2-D DP

Bit Manipulation

Math & Geometry

About

Working my brain a bit. My leetcode.com trainings in Swift.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages