swift-problems-and-solutions

Data Structures

Array

Title Solution Difficulty Time Space
209. Minimum Size Subarray Sum Swift Medium O(n) O(1)
904. Fruit Into Baskets Swift Medium O(n) O(1)
118. Pascal's Triangle Swift Easy O(n2) O(n2)
406. Queue Reconstruction by Height Swift Medium O(n2) O(n)
167. Two Sum II - Input array is sorted Swift Easy O(n) O(1)
26. Remove Duplicates from Sorted Array Swift Easy O(n) O(1)
27. Remove Element Swift Easy O(n) O(1)
238. Product of Array Except Self Swift Medium O(n) O(1)
48. Rotate Image Swift Medium O(n2) O(1)
977. Squares of a Sorted Array Swift Easy O(n) O(n)
15. 3Sum Swift Medium O(n2) O(n)
53. Maximum Subarray Swift Easy O(n) O(1)
259. 3Sum Smaller Swift Medium O(n2) O(n)
121. Best Time to Buy and Sell Stock Swift Easy O(n) O(1)
42. Trapping Rain Water Swift Hard O(n) O(1)
56. Merge Intervals Swift Medium O(nlogn) O(n)
57. Insert Interval Swift Medium O(n) O(n)
986. Interval List Intersections Swift Medium O(m + n) O(m + n)
252. Meeting Rooms Swift Medium O(nlogn) O(n)
268. Missing Number Swift Easy O(n) O(1)
448. Find All Numbers Disappeared in an Array Swift Easy O(n) O(1)
287. Find the Duplicate Number Swift Medium O(n) O(1)
41. First Missing Positive Swift Hard O(n) O(1)
215. Kth Largest Element in an Array Swift Medium O(n) O(1)
54. Spiral Matrix Swift Medium O(n) O(1)
73. Set Matrix Zeroes Swift Medium O(nm) O(1)
560. Subarray Sum Equals K Swift Medium O(n) O(n)
349. Intersection of Two Arrays Swift Easy O(n) O(n)
128. Longest Consecutive Sequence Swift Hard O(n) O(n)
78. Subsets Swift Medium O(n * 2^n) O(n * 2^n)
90. Subsets II Swift Medium O(n * 2^n) O(n * 2^n)
Find Word Swift O(n) O(n)
1089. Duplicate Zeros Swift Easy On) O(1)
1985. Find the Kth Largest Integer in the Array Swift Medium Onlogn) O(1)

Backtracking

Title Solution Difficulty Time Space
79. Word Search Swift Medium O(mnl^4) O(mn)
17. Letter Combinations of a Phone Number Swift Medium O(3^n * 4^m) O(3^n * 4^m)
47. Permutations II Swift Medium O(n!) O(n)
46. Permutations Swift Medium O(n!) O(n!)

BFS

Title Solution Difficulty Time Space
102. Binary Tree Level Order Traversal Swift Medium O(n) O(n)
103. Binary Tree Zigzag Level Order Traversal Swift Medium O(n) O(n)
515. Find Largest Value in Each Tree Row Swift Medium O(n) O(n)
515. Find Largest Value in Each Tree Row Swift Medium O(n) O(n)
784. Letter Case Permutation Swift Medium O(n * 2^n) O(n * 2^n)

BinarySearch

Title Solution Difficulty Time Space
33. Search in Rotated Sorted Array Swift Medium O(logn) O(1)
153. Find Minimum in Rotated Sorted Array Swift Medium O(logn) O(1)
704. Binary Search Swift Easy O(logn) O(1)
744. Find Smallest Letter Greater Than Target Swift Easy O(logn) O(1)
702. Search in a Sorted Array of Unknown Size Swift Medium O(logT) O(1)
2187. Minimum Time to Complete Trips Swift Medium O(nlogD) O(1)
2226. Maximum Candies Allocated to K Children Swift Medium O(nlogD) O(1)

BitManipulation

Title Solution Difficulty Time Space
421. Maximum XOR of Two Numbers in an Array Swift Medium O(n) O(1)
136. Single Number Swift Easy O(n) O(1)
260. Single Number III Swift Medium O(n) O(1)
832. Flipping an Image Swift Easy O(n^2) O(1)
1009. Complement of Base 10 Integer Swift Easy O(logN) O(1)
231. Power of Two Swift Easy O(1) O(1)
371. Sum of Two Integers Swift Medium O(1) O(1)
1371. Find the Longest Substring Containing Vowels in Even Counts Swift Medium O(n) O(1)
1915. Number of Wonderful Substrings Swift Medium O(n) O(1)

Design

Title Solution Difficulty Time Space
208. Implement Trie (Prefix Tree) Swift Medium O(n) O(n)
146. LRU Cache Swift Medium O(1) O(n)
295. Find Median from Data Stream Swift Hard O(logn) O(n)

DP

Title Solution Difficulty Time Space
416. Partition Equal Subset Sum Swift Medium O(mn) O(m)
64. Minimum Path Sum Swift Medium O(mn) O(1)
96. Unique Binary Search Trees Swift Medium O(n2) O(n)
309. Best Time to Buy and Sell Stock with Cooldown Swift Medium O(n) O(1)
152. Maximum Product Subarray Swift Medium O(n) O(1)
91. Decode Ways Swift Medium O(n) O(1)
279. Perfect Squares Swift Medium O(n√n) O(n)
1143. Longest Common Subsequence Swift Medium O(nm) O(nm)
322. Coin Change Swift Medium O(nm) O(n)
300. Longest Increasing Subsequence Swift Medium O(nlogn) O(n)

DFS

Title Solution Difficulty Time Space
200. Number of Islands Swift Medium O(mn) O(mn)
257. Binary Tree Paths Swift Easy O(n) O(n)
129. Sum Root to Leaf Numbers Swift Medium O(n) O(h)
1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree Swift Medium O(n) O(h)
437. Path Sum III Swift Medium O(n) O(n)
417. Pacific Atlantic Water Flow Swift Medium O(mn) O(mn)

FenwickTree

Title Solution Difficulty Time Space
307. Range Sum Query - Mutable Swift Medium O(nlogn) O(n)

Greedy

Title Solution Difficulty Time Space
621. Task Scheduler Swift Medium O(n) O(1)
435. Non-overlapping Intervals Swift Medium O(nlogn) O(1)
406. Queue Reconstruction by Height Swift Medium O(n^2) O(n)
Maximum Even Sum With K Number Swift Medium O(nlogn) O(n)

Graph

Title Solution Difficulty Time Space
133. Clone Graph Swift Medium O(n + m) O(n)
261. Graph Valid Tree Swift Medium O(n) O(1)
269. Alien Dictionary Swift Hard O() O()

Heap

Title Solution Difficulty Time Space
1167. Minimum Cost to Connect Sticks Swift Medium O(nlogn) O(n)
347. Top K Frequent Elements Swift Medium O(n) O(n)

LinkedList

Title Solution Difficulty Time Space
160. Intersection of Two Linked Lists Swift Easy O(n + m) O(1)
203. Remove Linked List Elements Swift Easy O(n) O(1)
141. Linked List Cycle Swift Easy O(n) O(1)
142. Linked List Cycle II Swift Medium O(n) O(1)
876. Middle of the Linked List Swift Easy O(n) O(1)
234. Palindrome Linked List Swift Easy O(n) O(1)
143. Reorder List Swift Medium O(n) O(1)
92. Reverse Linked List II Swift Medium O(n) O(1)
19. Remove Nth Node From End of List Swift Medium O(n) O(1)
23. Merge k Sorted Lists Swift Hard O(nlogk) O(1)

Math

Title Solution Difficulty Time Space
168. Excel Sheet Column Title Swift Easy O(n) O(1)
171. Excel Sheet Column Number Swift Easy O(n) O(1)
172. Factorial Trailing Zeroes Swift Easy O(log5 n) O(1)
204. Count Primes Swift Easy O(nlog log n) O(n)
(時間複雜度很難算)202. Happy Number Swift Easy O(logn) O(1)
50. Pow(x, n) Swift Medium O(log n) O(1)
338. Counting Bits Swift Medium O(n) O(n)
1823. Find the Winner of the Circular Game Swift Medium O(n) O(1)
2141. Maximum Running Time of N Computers Swift Hard O(nlogn) O(1)

Stack

Title Solution Difficulty Time Space
394. Decode String Swift Medium O(n) O(n)
20. Valid Parentheses Swift Easy O(n) O(n)
84. Largest Rectangle in Histogram Swift Hard O(n) O(n)

Sort

Title Solution Difficulty Time Space
253. Meeting Rooms II Swift Medium O(nlogn) O(n)
Merge Sort Swift Medium O(nlogn) O(n)

String

Title Solution Difficulty Time Space
424. Longest Repeating Character Replacement Swift Medium O(n) O(1)
290. Word Pattern Swift Easy O(n) O(m)
76. Minimum Window Substring Swift Hard O(S+T) O(T)
49. Group Anagrams Swift Medium O(nk) O(nk)
647. Palindromic Substrings Swift Medium O(n^2) O(1)
451. Sort Characters By Frequency Swift Medium O(n) O(n)
763. Partition Labels Swift Medium O(n) O(1)
1694. Reformat Phone Number Swift Easy O(n) O(n)
224. Basic Calculator Swift Hard O(n) O(1)
227. Basic Calculator II Swift Medium O(n) O(1)
125. Valid Palindrome Swift Easy O(n) O(1)
212. Word Search II Swift Hard O() O()
271. Encode and Decode Strings Swift Medium O(n) O(1)

Tree

Title Solution Difficulty Time Space
110. Balanced Binary Tree Swift Easy O(n) O(n)
235. Lowest Common Ancestor of a Binary Search Tree Swift Easy O(h) O(1)
105. Construct Binary Tree from Preorder and Inorder Traversal Swift Medium O(n) O(n)
236. Lowest Common Ancestor of a Binary Tree Swift Medium O(n) O(n)
111. Minimum Depth of Binary Tree Swift Easy O(n) O(n)
98. Validate Binary Search Tree Swift Medium O(n) O(n)
98. Validate Binary Search Tree Swift Medium O(n) O(n)
112. Path Sum Swift Easy O(n) O(n)
113. Path Sum II Swift Medium O(n) O(n)
124. Binary Tree Maximum Path Sum Swift Hard O(n) O(h)
572. Subtree of Another Tree Swift Easy O(max(m,n)) O(max(m,n))
297. Serialize and Deserialize Binary Tree Swift Hard O(n) O(n)