Skip to content

Commit 24cb090

Browse files
author
Boudhayan Biswas
committed
Add remove islands
1 parent c096d9d commit 24cb090

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// RemoveIslands.swift
3+
// Algorithm Solutions In Swift
4+
//
5+
// Created by Boudhayan Biswas on 04/07/22.
6+
//
7+
8+
import Foundation
9+
10+
// T: O(mn), S: (mn)
11+
func removeIslands(_ matrix: inout [[Int]]) -> [[Int]] {
12+
// go through the border and find 1.
13+
// if found 1, try to come inside and make every adjacent 1's to 2's
14+
//connected border 1's will be 2's
15+
// islands 1 will still exist as a
16+
// iterate through matrix
17+
// change 2 to 1 and 1 to 0
18+
for row in 0..<matrix.count {
19+
for col in 0..<matrix[0].count {
20+
if !isBorder(row, col, matrix) {
21+
continue
22+
}
23+
if row == 1 && col == 5 {
24+
print("")
25+
26+
if matrix[row][col] == 0 || matrix[row][col] == 2 {
27+
continue
28+
}
29+
}
30+
changeBorderOnesToTwos(row, col, &matrix)
31+
}
32+
}
33+
34+
for row in 0..<matrix.count {
35+
for col in 0..<matrix[0].count {
36+
if matrix[row][col] == 2 {
37+
matrix[row][col] = 1
38+
} else if matrix[row][col] == 1 {
39+
matrix[row][col] = 0
40+
} else {
41+
42+
}
43+
}
44+
}
45+
return matrix
46+
}
47+
48+
func isBorder(_ row: Int, _ col: Int, _ matrix: [[Int]]) -> Bool {
49+
return row == 0 || col == 0 || row == matrix.count - 1 || col == matrix[0].count - 1
50+
}
51+
52+
func changeBorderOnesToTwos(_ row: Int, _ col: Int, _ matrix: inout [[Int]]) {
53+
var stack = [(Int, Int)]()
54+
stack.append((row, col))
55+
while !stack.isEmpty {
56+
let (r, c) = stack.removeLast()
57+
if matrix[r][c] == 0 || matrix[r][c] == 2 {
58+
continue
59+
}
60+
matrix[r][c] = 2
61+
stack += validNeighbours(r, c, matrix)
62+
}
63+
}
64+
65+
66+
func validNeighbours(_ row: Int, _ col: Int, _ matrix: [[Int]]) -> [(Int, Int)] {
67+
var neighbours = [(Int, Int)]()
68+
if row - 1 >= 0 {
69+
neighbours.append((row - 1, col))
70+
}
71+
if row + 1 <= matrix.count - 1 {
72+
neighbours.append((row + 1, col))
73+
}
74+
if col - 1 >= 0 {
75+
neighbours.append((row, col - 1))
76+
}
77+
if col + 1 <= matrix[0].count - 1 {
78+
neighbours.append((row, col + 1))
79+
}
80+
return neighbours
81+
}
82+
83+
//var matrix = [
84+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
85+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
86+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
87+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
88+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
89+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
90+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
91+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
92+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
93+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
94+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
95+
// [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
96+
// [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
97+
// ]
98+
//print(removeIslands(&matrix))
99+
//
100+
///**
101+
// [
102+
// [2, 0, 0, 0, 0, 0],
103+
// [0, 1, 0, 2, 2, 2],
104+
// [0, 0, 1, 0, 2, 0],
105+
// [2, 2, 0, 0, 2, 0],
106+
// [2, 0, 1, 1, 0, 0],
107+
// [2, 0, 0, 0, 0, 2]
108+
// ]
109+
//
110+
//
111+
// */

Algorithm Solutions In Swift.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
6A09C88627EDC26600BB9A38 /* ApartmentHunting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A09C88527EDC26600BB9A38 /* ApartmentHunting.swift */; };
1616
6A09C88827EDC29200BB9A38 /* ApartmentHunting+.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A09C88727EDC29200BB9A38 /* ApartmentHunting+.swift */; };
1717
6A09C88A27F03DF400BB9A38 /* Solution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A09C88927F03DF400BB9A38 /* Solution.swift */; };
18+
6A2D1F072872CF600045048E /* RemoveIslands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2D1F062872CF600045048E /* RemoveIslands.swift */; };
1819
6A4364AB27F4E5DA00F09D92 /* ValidParentheses.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A4364AA27F4E5DA00F09D92 /* ValidParentheses.swift */; };
1920
6A4364AE27F4E9B300F09D92 /* BalancedBrackets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A4364AD27F4E9B300F09D92 /* BalancedBrackets.swift */; };
2021
6A4364B127F6F6EC00F09D92 /* SpiralMatrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A4364B027F6F6EC00F09D92 /* SpiralMatrix.swift */; };
@@ -266,6 +267,7 @@
266267
6A09C88527EDC26600BB9A38 /* ApartmentHunting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApartmentHunting.swift; sourceTree = "<group>"; };
267268
6A09C88727EDC29200BB9A38 /* ApartmentHunting+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ApartmentHunting+.swift"; sourceTree = "<group>"; };
268269
6A09C88927F03DF400BB9A38 /* Solution.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Solution.swift; sourceTree = "<group>"; };
270+
6A2D1F062872CF600045048E /* RemoveIslands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveIslands.swift; sourceTree = "<group>"; };
269271
6A4364AA27F4E5DA00F09D92 /* ValidParentheses.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidParentheses.swift; sourceTree = "<group>"; };
270272
6A4364AD27F4E9B300F09D92 /* BalancedBrackets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalancedBrackets.swift; sourceTree = "<group>"; };
271273
6A4364B027F6F6EC00F09D92 /* SpiralMatrix.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpiralMatrix.swift; sourceTree = "<group>"; };
@@ -515,6 +517,14 @@
515517
path = "Apartment Hunting";
516518
sourceTree = "<group>";
517519
};
520+
6A2D1F052872CF500045048E /* Remove Islands */ = {
521+
isa = PBXGroup;
522+
children = (
523+
6A2D1F062872CF600045048E /* RemoveIslands.swift */,
524+
);
525+
path = "Remove Islands";
526+
sourceTree = "<group>";
527+
};
518528
6A4364A927F4E5C800F09D92 /* Valid Parentheses */ = {
519529
isa = PBXGroup;
520530
children = (
@@ -1489,6 +1499,7 @@
14891499
6AF5788326A2E674007B37CA /* AlgoExpert */ = {
14901500
isa = PBXGroup;
14911501
children = (
1502+
6A2D1F052872CF500045048E /* Remove Islands */,
14921503
6A67AE55286B132500DCD992 /* Number of Ways to Make Change */,
14931504
6A67AE52286B125D00DCD992 /* Sort Stacks */,
14941505
6A67AE4F2869CDAE00DCD992 /* Sunset Views */,
@@ -2218,6 +2229,7 @@
22182229
6AF5789826A2E728007B37CA /* BranchSums.swift in Sources */,
22192230
6AF5791526A74CC6007B37CA /* CountPairs.swift in Sources */,
22202231
6A88CF0826C915B100A3746B /* MergeSortRecursion.swift in Sources */,
2232+
6A2D1F072872CF600045048E /* RemoveIslands.swift in Sources */,
22212233
6AF5791926A75668007B37CA /* AlmostSorted.swift in Sources */,
22222234
6A88CF0C26C98D8300A3746B /* ReverseLinkedList.swift in Sources */,
22232235
6AF5794A26AAC047007B37CA /* MergeSort.swift in Sources */,

Algorithm Solutions In Swift.xcodeproj/xcuserdata/boudhayan.xcuserdatad/xcschemes/xcschememanagement.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<key>Algorithm Solutions In Swift Tests.xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
10-
<integer>0</integer>
10+
<integer>1</integer>
1111
</dict>
1212
<key>Algorithm Solutions In Swift.xcscheme_^#shared#^_</key>
1313
<dict>
1414
<key>orderHint</key>
15-
<integer>1</integer>
15+
<integer>0</integer>
1616
</dict>
1717
</dict>
1818
</dict>

Pramp/Nth Root of Number/NthRoot.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ func accuracyTest(root: Double, x: Double, n: Double) -> Double {
3434
return pow(root, n) - x
3535
}
3636

37-
//print(root(x: 7.0, n: 3.0))
37+
//;.lo;,iko ≤m    print(root(x: 7.0, n: 3.0))

0 commit comments

Comments
 (0)