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

54 Commits
 
 
 
 
 
 
 
 

Repository files navigation

leetcode

Arrays & Hashing

Problem Leetcode Difficulty Topics Notes
217 - Contains Duplicate Leetcode 🟢 Array, Hash Table, Sorting
⚠️ Notes (solution spoiler alert)Use dictionary (hashmap) to check for duplicates
242 - Valid Anagram Leetcode 🟢 Hash Table, String, Sorting
⚠️ Notes (solution spoiler alert)Two dictionaries (hashmaps), char:count and then compare two hashmaps
1 - Two Sum Leetcode 🟢 Array, Hash Table
⚠️ Notes (solution spoiler alert)TBD
49 - Group Anagrams Leetcode 🟡 Array, Hash Table, String, Sorting
⚠️ Notes (solution spoiler alert)Use 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
⚠️ Notes (solution spoiler alert)Tricky to understand the principle. Make two passes to compute products: first in-order, second in-reverse.
36 - Valid Sudoku Leetcode 🟡 Array, Hash Table, Matrix
⚠️ Notes (solution spoiler alert)Use 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
⚠️ Notes (solution spoiler alert)Separator 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
⚠️ Notes (solution spoiler alert)Loop 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
⚠️ Notes (solution spoiler alert)Left 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
⚠️ Notes (solution spoiler alert)In 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
⚠️ Notes (solution spoiler alert)Similar 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
⚠️ Notes (solution spoiler alert)The 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
⚠️ Notes (solution spoiler alert)TBD

Stack

Problem Leetcode Difficulty Topics Notes
20 - Valid Parentheses Leetcode 🟢 String, Stack
⚠️ Notes (solution spoiler alert)Use 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
⚠️ Notes (solution spoiler alert)Store 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
⚠️ Notes (solution spoiler alert)Iterate 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
⚠️ Notes (solution spoiler alert)Implement 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
⚠️ Notes (solution spoiler alert)TBD to deep dive
853 - Car Fleet Leetcode 🟡 Array, Stack, Sorting, Monotonic Stack
⚠️ Notes (solution spoiler alert)Notes
84 - Largest Rectangle In Histogram Leetcode 🔴 TBD
⚠️ Notes (solution spoiler alert)TBD

Binary Search

Problem Leetcode Difficulty Topics Notes
704 - Binary Search Leetcode 🟢 Array, Binary Search
⚠️ Notes (solution spoiler alert)TBD

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