{"id":17160625,"url":"https://github.com/atereshkov/leetcode-swift","last_synced_at":"2025-04-13T14:11:24.799Z","repository":{"id":174385040,"uuid":"434848674","full_name":"atereshkov/leetcode-swift","owner":"atereshkov","description":"Working my brain a bit. My leetcode.com trainings in Swift.","archived":false,"fork":false,"pushed_at":"2024-04-07T19:08:38.000Z","size":56,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-08T17:22:08.865Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/atereshkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2021-12-04T08:36:05.000Z","updated_at":"2024-04-14T20:34:26.480Z","dependencies_parsed_at":"2024-03-16T19:54:53.185Z","dependency_job_id":"a4412ec1-6f3a-40e2-8f14-bdc2397b6ffc","html_url":"https://github.com/atereshkov/leetcode-swift","commit_stats":null,"previous_names":["atereshkov/leetcode-swift"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atereshkov%2Fleetcode-swift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atereshkov%2Fleetcode-swift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atereshkov%2Fleetcode-swift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atereshkov%2Fleetcode-swift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atereshkov","download_url":"https://codeload.github.com/atereshkov/leetcode-swift/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724629,"owners_count":21151561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T22:25:27.360Z","updated_at":"2025-04-13T14:11:24.778Z","avatar_url":"https://github.com/atereshkov.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# leetcode\n\n# Table of Contents\n1. [Arrays \u0026 Hashing](#arrays--hashing)\n2. [Two Pointers](#two-pointers)\n3. [Stack](#stack)\n4. [Binary Search](#binary-search)\n5. [Sliding Window](#sliding-window)\n6. [Linked List](#linked-list)\n7. [Trees](#trees)\n8. [Heap / Priority Queue](#heap--priority-queue)\n9. [Intervals](#intervals)\n10. [Greedy](#greedy)\n11. [Backtracking](#backtracking)\n12. [Graphs](#graphs)\n13. [Advanced Graphs](#advanced-graphs)\n14. [1-D DP](#)\n15. [2-D DP](#)\n16. [Bit Manipulation](#bit-manipulation)\n17. [Math \u0026 Geometry](#)\n\n### Arrays \u0026 Hashing\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [217 - Contains Duplicate](/solutions/Problems/Easy/217_Contains_Duplicate.swift) | [Leetcode](https://leetcode.com/problems/contains-duplicate/) | 🟢 | Array, Hash Table, Sorting | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eUse dictionary (hashmap) to check for duplicates\u003c/details\u003e |\n| [242 - Valid Anagram](/solutions/Problems/Easy/242_Valid_Anagram.swift) | [Leetcode](https://leetcode.com/problems/valid-anagram/) | 🟢 | Hash Table, String, Sorting | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTwo dictionaries (hashmaps), `char:count` and then compare two hashmaps\u003c/details\u003e |\n| [1 - Two Sum](/solutions/Problems/Easy/1_TwoSum.swift) | [Leetcode](https://leetcode.com/problems/two-sum/) | 🟢 | Array, Hash Table | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [49 - Group Anagrams](/solutions/Problems/Medium/49_Group_Anagrams.swift) | [Leetcode](https://leetcode.com/problems/group-anagrams/) | 🟡 | Array, Hash Table, String, Sorting | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eUse dictionary (hashmap) with sorted letters of a word (key) and array of anagrams (value)\u003c/details\u003e |\n| [347 - Top K Frequent Elements](/solutions/Problems/Medium/347_Top_K_Frequent_Elements.swift) | [Leetcode](https://leetcode.com/problems/top-k-frequent-elements) | 🟡 | Array, Hash Table, Divide and Conquer, Sorting, Heap (Priority Queue), Bucket Sort, Counting, Quickselect | TBD |\n| [238 - Product of Array Except Self](/solutions/Problems/Medium/238_Product_of_Array_Except_Self.swift) | [Leetcode](https://leetcode.com/problems/product-of-array-except-self) | 🟡 | Array, Prefix Sum | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTricky to understand the principle. Make two passes to compute products: first in-order, second in-reverse.\u003c/details\u003e |\n| [36 - Valid Sudoku](/solutions/Problems/Medium/36_Valid_Sudoku.swift) | [Leetcode](https://leetcode.com/problems/valid-sudoku) | 🟡 | Array, Hash Table, Matrix | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eUse 3 hashmaps (dictionaries): rows, columns and subsets (3x3, use 'row / 3', 'column / 3' as a key). Check for duplicates in sets\u003c/details\u003e |\n| [271 - Encode and Decode Strings](/solutions/Problems/Medium/271_Encode_and_Decode_Strings.swift) | [Leetcode](https://leetcode.com/problems/encode-and-decode-strings) | 🟡 | TBD | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eSeparator 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.\u003c/details\u003e |\n| [128 - Longest Consecutive Sequence](/solutions/Problems/Medium/128_Longest_Consecutive_Sequence.swift) | [Leetcode](https://leetcode.com/problems/longest-consecutive-sequence) | 🟡 | Array, Hash Table, Union Find | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eLoop 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.\u003c/details\u003e |\n\n### Two Pointers\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [125 - Valid Palindrome](/solutions/Problems/Easy/125_Valid_Palindrome.swift) | [Leetcode](https://leetcode.com/problems/valid-palindrome) | 🟢 | Two Pointers, String | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eLeft 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.\u003c/details\u003e |\n| [167 - Two Sum II - Input Array Is Sorted](/solutions/Problems/Medium/167_Two_Sum_II_Input_Array_Is_Sorted.swift) | [Leetcode](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | 🟡 | Array, Two Pointers, Binary Search | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eIn `while` calculate the sum (left and right pointers). If `sum \u003e target`, decrease right pointer. If `sum \u003c target`, increase left pointer. If `sum == target`, return.\u003c/details\u003e |\n| [15 - 3Sum](/solutions/Problems/Medium/15_3Sum.swift) | [Leetcode](https://leetcode.com/problems/3sum) | 🟡 | Array, Two Pointers, Sorting | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eSimilar 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).\u003c/details\u003e |\n| [11 - Container With Most Water](/solutions/Problems/Medium/11_Container_With_Most_Water.swift) | [Leetcode](https://leetcode.com/problems/container-with-most-water) | 🟡 | Array, Two Pointers, Greedy | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eThe 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 `\u003c` or `\u003e` than `h[r]`.\u003c/details\u003e |\n| [42 - Trapping Rain Water](/solutions/Problems/Hard/42_Trapping_Rain_Water.swift) | [Leetcode](https://leetcode.com/problems/trapping-rain-water) | 🔴 | Array, Two Pointers, DP, Stack, Monotonic Stack | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Stack\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [20 - Valid Parentheses](/solutions/Problems/Easy/20_Valid_Parentheses.swift) | [Leetcode](https://leetcode.com/problems/valid-parentheses) | 🟢 | String, Stack | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eUse 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).\u003c/details\u003e |\n| [155 - Min Stack](/solutions/Problems/Medium/155_Min_Stack.swift) | [Leetcode](https://leetcode.com/problems/min-stack) | 🟡 | Stack, Design | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eStore two arrays (one for numbers, another for min values). `push` - append value to `stack` \u0026 append `minStack` value (`min(val, minStack.last ?? val)`). `pop` should pop from both. `top` - return last from `stack`, `getMin` - last from `minStack`.\u003c/details\u003e |\n| [150 - Evaluate Reverse Polish Notation](/solutions/Problems/Medium/150_Evaluate_Reverse_Polish_Notation.swift) | [Leetcode](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | 🟡 | Array, Math, Stack | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eIterate 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.\u003c/details\u003e |\n| [22 - Generate Parentheses](/solutions/Problems/Medium/22_Generate_Parentheses.swift) | [Leetcode](https://leetcode.com/problems/generate-parentheses/) | 🟡 | String, DP, Backtracking | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eImplement 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 \u003c n`, backtrack (`openN + 1`) and `pop`. `Push` close paranthesis if `closedN \u003c openN`, backtrack (`closedN +1`) and `pop`.\u003c/details\u003e |\n| [739 - Daily Temperatures](/solutions/Problems/Medium/739_Daily_Temperatures.swift) | [Leetcode](https://leetcode.com/problems/daily-temperatures/description/) | 🟡 | Array, Stack, Monotonic Stack | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD to deep dive\u003c/details\u003e |\n| [853 - Car Fleet](/solutions/Problems/Medium/853_Car_Fleet.swift) | [Leetcode](https://leetcode.com/problems/car-fleet/) | 🟡 | Array, Stack, Sorting, Monotonic Stack | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eNotes\u003c/details\u003e |\n| [84 - Largest Rectangle In Histogram](/solutions/Problems/Hard/84_Largest_Rectangle_In_Histogram.swift) | [Leetcode](https://leetcode.com/problems/largest-rectangle-in-histogram/) | 🔴 | TBD | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Binary Search\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [704 - Binary Search](/solutions/Problems/Easy/704_Binary_Search.swift) | [Leetcode](https://leetcode.com/problems/binary-search) | 🟢 | Array, Binary Search | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [74 - Search a 2D Matrix](/solutions/Problems/Medium/74_Search_2D_Matrix.swift) | [Leetcode](https://leetcode.com/problems/search-a-2d-matrix) | 🟡 | Array, Binary Search, Matrix | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [875 - Koko Eating Bananas](/solutions/Problems/Medium/875_Koko_Eating_Bananas.swift) | [Leetcode](https://leetcode.com/problems/koko-eating-bananas) | 🟡 | Binary Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Sliding Window\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [121 - Best Time to Buy and Sell Stock](/solutions/Problems/Easy/121_Best_Time_to_Buy_and_Sell_Stock.swift) | [Leetcode](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | 🟢 | Array, Dynamic Programming | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [3 - Longest Substring Without Repeating Characters](/solutions/Problems/Medium/3_Longest_Substring_Without_Repeating_Characters.swift) | [Leetcode](https://leetcode.com/problems/longest-substring-without-repeating-characters) | 🟡 | Hash Table, String, Sliding Window | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Linked List\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [206 - Reverse Linked List](/solutions/Problems/Easy/206_Reverse_Linked_List.swift) | [Leetcode](https://leetcode.com/problems/reverse-linked-list/) | 🟢 | Linked List, Recursion | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [21 - Merge Two Sorted Lists](/solutions/Problems/Easy/21_Merge_Two_Sorted_Lists.swift) | [Leetcode](https://leetcode.com/problems/merge-two-sorted-lists/) | 🟢 | Linked List, Recursion | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [143 - Reorder List](/solutions/Problems/Medium/143_Reorder_List.swift) | [Leetcode](https://leetcode.com/problems/reorder-list/) | 🟡 | Linked List, Two Pointers, Stack, Recursion | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [19 - Remove Nth Node From End of List](/solutions/Problems/Medium/19_Remove_Nth_Node_From_End_of_List.swift) | [Leetcode](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | 🟡 | Linked List, Two Pointers | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Trees\n\n| Problem | Leetcode | Difficulty | Topics | Notes | \n|---|---|---|---|---|\n| [226 - Invert Binary Tree](/solutions/Problems/Easy/226_Invert_Binary_Tree.swift) | [Leetcode](https://leetcode.com/problems/invert-binary-tree/) | 🟢 | Tree, Depth-First Search, Breadth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [104 - Maximum Depth of Binary Tree](/solutions/Problems/Easy/104_Maximum_Depth_of_Binary_Tree.swift) | [Leetcode](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | 🟢 | Tree, Depth-First Search, Breadth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [543 - Diameter of Binary Tree](/solutions/Problems/Easy/543_Diameter_of_Binary_Tree.swift) | [Leetcode](https://leetcode.com/problems/diameter-of-binary-tree/) | 🟢 | Tree, Depth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [110 - Balanced Binary Tree](/solutions/Problems/Easy/110_Balanced_Binary_Tree.swift) | [Leetcode](https://leetcode.com/problems/balanced-binary-tree/) | 🟢 | Tree, Depth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [100 - Same Tree](/solutions/Problems/Easy/100_Same_Tree.swift) | [Leetcode](https://leetcode.com/problems/same-tree/) | 🟢 | Tree, Depth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n| [572 - Subtree of Another Tree](/solutions/Problems/Easy/572_Subtree_of_Another_Tree.swift) | [Leetcode](https://leetcode.com/problems/subtree-of-another-tree/) | 🟢 | Tree, Depth-First Search, Binary Tree | \u003cdetails\u003e\u003csummary\u003e⚠️ Notes\u003c/summary\u003eTBD\u003c/details\u003e |\n\n### Heap / Priority Queue\n\n\n### Intervials\n\n\n### Greedy\n\n\n### Backtracking\n\n\n### Graphs\n\n\n### Advanced Graphs\n\n\n### 1-D DP\n\n\n### 2-D DP\n\n\n### Bit Manipulation\n\n\n### Math \u0026 Geometry\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatereshkov%2Fleetcode-swift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatereshkov%2Fleetcode-swift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatereshkov%2Fleetcode-swift/lists"}