Skip to content

Commit 421e82f

Browse files
committed
Improved task 200
1 parent d6328a9 commit 421e82f

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7171
|-|-|-|-|-|-
72-
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
72+
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 152 | 70.92
7373

7474
#### Day 10 Dynamic Programming
7575

@@ -337,7 +337,7 @@
337337

338338
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
339339
|-|-|-|-|-|-
340-
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
340+
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 152 | 70.92
341341

342342
#### Udemy Dynamic Programming
343343

@@ -712,7 +712,7 @@
712712

713713
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
714714
|-|-|-|-|-|-
715-
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
715+
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 152 | 70.92
716716

717717
#### Day 7 Breadth First Search Depth First Search
718718

@@ -1279,7 +1279,7 @@
12791279

12801280
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12811281
|-|-|-|-|-|-
1282-
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
1282+
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 152 | 70.92
12831283

12841284
#### Day 2 Matrix Related Problems
12851285

@@ -1432,7 +1432,7 @@
14321432
| 0208 |[Implement Trie (Prefix Tree)](src/main/swift/g0201_0300/s0208_implement_trie_prefix_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Design, Trie, Level_2_Day_16_Design, Udemy_Trie_and_Heap, Big_O_Time_O(word.length())_or_O(prefix.length())_Space_O(N) | 131 | 97.26
14331433
| 0207 |[Course Schedule](src/main/swift/g0201_0300/s0207_course_schedule)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Graph, Topological_Sort, Big_O_Time_O(N)_Space_O(N) | 30 | 92.23
14341434
| 0206 |[Reverse Linked List](src/main/swift/g0201_0300/s0206_reverse_linked_list)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
1435-
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 4 | 89.47
1435+
| 0200 |[Number of Islands](src/main/swift/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Big_O_Time_O(M\*N)_Space_O(M\*N) | 152 | 70.92
14361436
| 0198 |[House Robber](src/main/swift/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
14371437
| 0189 |[Rotate Array](src/main/swift/g0101_0200/s0189_rotate_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 118 | 91.36
14381438
| 0169 |[Majority Element](src/main/swift/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 38 | 96.59

src/main/swift/g0101_0200/s0200_number_of_islands/readme.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,34 @@ An **island** is surrounded by water and is formed by connecting adjacent lands
4545
## Solution
4646

4747
```swift
48-
/**
49-
* Definition for singly-linked list.
50-
* public class ListNode {
51-
* public var val: Int
52-
* public var next: ListNode?
53-
* public init() { self.val = 0; self.next = nil; }
54-
* public init(_ val: Int) { self.val = val; self.next = nil; }
55-
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
56-
* }
57-
*/
5848
class Solution {
59-
func reverseList(_ head: ListNode?) -> ListNode? {
60-
var prev: ListNode? = nil
61-
var curr = head
62-
var next = head?.next
63-
64-
while curr != nil {
65-
66-
// reverse the current nodes link
67-
curr?.next = prev
68-
69-
// update variables
70-
prev = curr
71-
curr = next
72-
next = next?.next
49+
func numIslands(_ grid: [[Character]]) -> Int {
50+
var grid = grid
51+
var islands = 0
52+
53+
if !grid.isEmpty && !grid[0].isEmpty {
54+
for i in 0..<grid.count {
55+
for j in 0..<grid[0].count {
56+
if grid[i][j] == "1" {
57+
dfs(&grid, i, j)
58+
islands += 1
59+
}
60+
}
61+
}
62+
}
63+
return islands
64+
}
65+
66+
private func dfs(_ grid: inout [[Character]], _ x: Int, _ y: Int) {
67+
if x < 0 || x >= grid.count || y < 0 || y >= grid[0].count || grid[x][y] != "1" {
68+
return
7369
}
7470

75-
return prev
71+
grid[x][y] = "x"
72+
dfs(&grid, x + 1, y)
73+
dfs(&grid, x - 1, y)
74+
dfs(&grid, x, y + 1)
75+
dfs(&grid, x, y - 1)
7676
}
7777
}
7878
```

src/main/swift/g0501_0600/s0560_subarray_sum_equals_k/readme.md

+11-12
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ Given an array of integers `nums` and an integer `k`, return _the total number o
3030
```swift
3131
class Solution {
3232
func subarraySum(_ nums: [Int], _ k: Int) -> Int {
33-
var result = 0
34-
var dict = [Int: Int]()
35-
dict[0] = 1
33+
var result = 0
34+
var dict = [Int: Int]()
35+
dict[0] = 1
3636

37-
var sum = 0
38-
for num in nums {
39-
sum += num
40-
if let val = dict[sum - k] {
41-
result += val
37+
var sum = 0
38+
for num in nums {
39+
sum += num
40+
if let val = dict[sum - k] {
41+
result += val
42+
}
43+
dict[sum, default: 0] += 1
4244
}
43-
dict[sum, default: 0] += 1
44-
}
45-
46-
return result
45+
return result
4746
}
4847
}
4948
```

0 commit comments

Comments
 (0)