Skip to content

Commit 17d2bc6

Browse files
committed
Added swift solutions
1 parent ca2f02e commit 17d2bc6

File tree

149 files changed

+6979
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+6979
-67
lines changed

README.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
1111
1212
##
13-
* [SQL I](#sql-i)
1413
* [Level 1](#level-1)
1514
* [Level 2](#level-2)
1615
* [Udemy](#udemy)
@@ -24,58 +23,7 @@
2423
* [Programming Skills I](#programming-skills-i)
2524
* [Programming Skills II](#programming-skills-ii)
2625
* [Graph Theory I](#graph-theory-i)
27-
28-
### SQL I
29-
30-
#### Day 1 Select
31-
32-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
33-
|-|-|-|-|-|-|-
34-
35-
#### Day 2 Select and Order
36-
37-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
38-
|-|-|-|-|-|-|-
39-
40-
#### Day 3 String Processing Functions
41-
42-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
43-
|-|-|-|-|-|-|-
44-
45-
#### Day 4 Union and Select
46-
47-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
48-
|-|-|-|-|-|-|-
49-
50-
#### Day 5 Union
51-
52-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
53-
|-|-|-|-|-|-|-
54-
55-
#### Day 6 Union
56-
57-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
58-
|-|-|-|-|-|-|-
59-
60-
#### Day 7 Function
61-
62-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
63-
|-|-|-|-|-|-|-
64-
65-
#### Day 8 Function
66-
67-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
68-
|-|-|-|-|-|-|-
69-
70-
#### Day 9 Control of Flow
71-
72-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
73-
|-|-|-|-|-|-|-
74-
75-
#### Day 10 Where
76-
77-
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
78-
|-|-|-|-|-|-|-
26+
* [SQL I](#sql-i)
7927

8028
### Level 1
8129

@@ -1406,6 +1354,58 @@
14061354
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14071355
|-|-|-|-|-|-|-
14081356

1357+
### SQL I
1358+
1359+
#### Day 1 Select
1360+
1361+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1362+
|-|-|-|-|-|-|-
1363+
1364+
#### Day 2 Select and Order
1365+
1366+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1367+
|-|-|-|-|-|-|-
1368+
1369+
#### Day 3 String Processing Functions
1370+
1371+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1372+
|-|-|-|-|-|-|-
1373+
1374+
#### Day 4 Union and Select
1375+
1376+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1377+
|-|-|-|-|-|-|-
1378+
1379+
#### Day 5 Union
1380+
1381+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1382+
|-|-|-|-|-|-|-
1383+
1384+
#### Day 6 Union
1385+
1386+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1387+
|-|-|-|-|-|-|-
1388+
1389+
#### Day 7 Function
1390+
1391+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1392+
|-|-|-|-|-|-|-
1393+
1394+
#### Day 8 Function
1395+
1396+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1397+
|-|-|-|-|-|-|-
1398+
1399+
#### Day 9 Control of Flow
1400+
1401+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1402+
|-|-|-|-|-|-|-
1403+
1404+
#### Day 10 Where
1405+
1406+
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
1407+
|-|-|-|-|-|-|-
1408+
14091409
## Algorithms
14101410

14111411
| # | Title | Language | Difficulty | Tag | Time, ms | Time, %

src/main/swift/g0001_0100/s0002_add_two_numbers/Solution.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
33
// #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_06_17_Time_17_ms_(76.59%)_Space_15.7_MB_(14.57%)
44

5-
/*
5+
/**
66
* Definition for singly-linked list.
77
* public class ListNode {
88
* public var val: Int
@@ -13,7 +13,6 @@
1313
* }
1414
*/
1515
class Solution {
16-
1716
func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
1817
return addNumbers(l1, l2, 0)
1918
}

src/main/swift/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ To solve the Add Two Numbers problem in Swift using a `Solution` class, we'll fo
5050
Here's the implementation:
5151

5252
```swift
53-
/*
53+
/**
5454
* Definition for singly-linked list.
5555
* public class ListNode {
5656
* public var val: Int

src/main/swift/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,3 @@ public class Solution {
3838
return 0.0
3939
}
4040
}
41-

src/main/swift/g0001_0100/s0005_longest_palindromic_substring/Solution.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,3 @@ public class Solution {
4545
return String(s[s.index(s.startIndex, offsetBy: start)..<s.index(s.startIndex, offsetBy: end)])
4646
}
4747
}
48-

src/main/swift/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Algorithm_I_Day_5_Two_Pointers #Level_2_Day_3_Linked_List #Big_O_Time_O(L)_Space_O(L)
33
// #2024_06_18_Time_0_ms_(100.00%)_Space_15.5_MB_(48.03%)
44

5-
/*
5+
/**
66
* Definition for singly-linked list.
77
* public class ListNode {
88
* public var val: Int

src/main/swift/g0001_0100/s0021_merge_two_sorted_lists/Solution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// #Level_1_Day_3_Linked_List #Udemy_Linked_List #Big_O_Time_O(m+n)_Space_O(m+n)
44
// #2024_06_18_Time_3_ms_(96.41%)_Space_15.9_MB_(6.39%)
55

6-
/*
6+
/**
77
* Definition for singly-linked list.
88
* public class ListNode {
99
* public var val: Int

src/main/swift/g0001_0100/s0023_merge_k_sorted_lists/Solution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Divide_and_Conquer #Merge_Sort #Big_O_Time_O(k*n*log(k))_Space_O(log(k))
33
// #2024_06_19_Time_25_ms_(94.57%)_Space_17.3_MB_(6.09%)
44

5-
/*
5+
/**
66
* Definition for singly-linked list.
77
* public class ListNode {
88
* public var val: Int

src/main/swift/g0001_0100/s0024_swap_nodes_in_pairs/Solution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
33
// #2024_06_19_Time_0_ms_(100.00%)_Space_15.8_MB_(10.29%)
44

5-
/*
5+
/**
66
* Definition for singly-linked list.
77
* public class ListNode {
88
* public var val: Int

src/main/swift/g0001_0100/s0024_swap_nodes_in_pairs/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ To solve the "Swap Nodes in Pairs" problem in Swift with a `Solution` class, we
4747
Here's the implementation:
4848

4949
```swift
50-
/*
50+
/**
5151
* Definition for singly-linked list.
5252
* public class ListNode {
5353
* public var val: Int

src/main/swift/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(k)
33
// #2024_06_19_Time_12_ms_(87.05%)_Space_15.9_MB_(48.20%)
44

5-
/*
5+
/**
66
* Definition for singly-linked list.
77
* public class ListNode {
88
* public var val: Int

src/main/swift/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ To solve the "Reverse Nodes in k-Group" problem in Swift with a `Solution` class
6363
Here's the implementation:
6464

6565
```swift
66-
/*
66+
/**
6767
* Definition for singly-linked list.
6868
* public class ListNode {
6969
* public var val: Int

src/main/swift/g0001_0100/s0042_trapping_rain_water/Solution.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ class Solution {
2626
}
2727
}
2828
return ans
29-
}
30-
29+
}
3130
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Math
2+
// #Combinatorics #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_15
3+
// #Level_1_Day_11_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
4+
// #2024_06_24_Time_0_ms_(100.00%)_Space_15.4_MB_(70.05%)
5+
6+
public class Solution {
7+
public func uniquePaths(_ m: Int, _ n: Int) -> Int {
8+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
9+
10+
for i in 0..<m {
11+
dp[i][0] = 1
12+
}
13+
14+
for j in 0..<n {
15+
dp[0][j] = 1
16+
}
17+
18+
for i in 1..<m {
19+
for j in 1..<n {
20+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
21+
}
22+
}
23+
24+
return dp[m - 1][n - 1]
25+
}
26+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
62\. Unique Paths
2+
3+
Medium
4+
5+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
6+
7+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
8+
9+
How many possible unique paths are there?
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
14+
15+
**Input:** m = 3, n = 7
16+
17+
**Output:** 28
18+
19+
**Example 2:**
20+
21+
**Input:** m = 3, n = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
28+
1. Right -> Down -> Down
29+
2. Down -> Down -> Right
30+
3. Down -> Right -> Down
31+
32+
**Example 3:**
33+
34+
**Input:** m = 7, n = 3
35+
36+
**Output:** 28
37+
38+
**Example 4:**
39+
40+
**Input:** m = 3, n = 3
41+
42+
**Output:** 6
43+
44+
**Constraints:**
45+
46+
* `1 <= m, n <= 100`
47+
* It's guaranteed that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
48+
49+
To solve the "Unique Paths" problem in Swift with the Solution class, follow these steps:
50+
51+
1. Define a method `uniquePaths` in the `Solution` class that takes two integers `m` and `n` as input and returns the number of unique paths from the top-left corner to the bottom-right corner of an `m x n` grid.
52+
2. Initialize a 2D array `dp` of size `m x n` to store the number of unique paths for each position in the grid.
53+
3. Initialize the first row and first column of `dp` to 1 since there is only one way to reach any position in the first row or column (by moving only right or down).
54+
4. Iterate over each position `(i, j)` in the grid, starting from the second row and second column:
55+
- Update `dp[i][j]` by adding the number of unique paths from the cell above `(i-1, j)` and the cell to the left `(i, j-1)`.
56+
5. Return the value of `dp[m-1][n-1]`, which represents the number of unique paths to reach the bottom-right corner of the grid.
57+
58+
Here's the implementation of the `uniquePaths` method in Swift:
59+
60+
```swift
61+
public class Solution {
62+
public func uniquePaths(_ m: Int, _ n: Int) -> Int {
63+
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)
64+
65+
for i in 0..<m {
66+
dp[i][0] = 1
67+
}
68+
69+
for j in 0..<n {
70+
dp[0][j] = 1
71+
}
72+
73+
for i in 1..<m {
74+
for j in 1..<n {
75+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
76+
}
77+
}
78+
79+
return dp[m - 1][n - 1]
80+
}
81+
}
82+
```
83+
84+
This implementation efficiently calculates the number of unique paths using dynamic programming, with a time complexity of O(m * n) and a space complexity of O(m * n).
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Matrix
2+
// #Dynamic_Programming_I_Day_16 #Udemy_Dynamic_Programming #Big_O_Time_O(m*n)_Space_O(m*n)
3+
// #2024_06_24_Time_17_ms_(92.63%)_Space_16_MB_(21.05%)
4+
5+
class Solution {
6+
func minPathSum(_ grid: [[Int]]) -> Int {
7+
var matrix: [[Int]] = grid
8+
let n = grid.count - 1
9+
let m = grid[0].count - 1
10+
11+
for i in 0...n {
12+
for j in 0...m {
13+
var step = matrix[i][j]
14+
15+
if i > 0 && j > 0 {
16+
step += min(matrix[i - 1][j], matrix[i][j - 1])
17+
} else if i > 0 && j == 0 {
18+
step += matrix[i - 1][j]
19+
} else if j > 0 && i == 0 {
20+
step += matrix[i][j - 1]
21+
}
22+
23+
matrix[i][j] = step
24+
}
25+
}
26+
27+
return matrix[n][m]
28+
}
29+
}

0 commit comments

Comments
 (0)