Iterate through every pair of numbers using two nested loops. For each pair, check if they sum to the target. Return the indices when a match is found.
TimeComplexity:O(n2)
SpaceComplexity:O(1)
Solution 2 - Brute Force using enumerate()
Same brute force logic, but uses Python's enumerate() for cleaner index tracking instead of range(len(...)).
TimeComplexity:O(n2)
SpaceComplexity:O(1)
Solution 1 - Counting Edges (Iterative)
For each land cell, start with 4 edges. Then subtract 1 for each adjacent land neighbor (up, down, left, right). Sum across all land cells to get the total perimeter.
TimeComplexity:O(m×n)
SpaceComplexity:O(1)
Solution 2 - Counting Land and Neighbors
Count the total number of land cells and the total number of shared edges between adjacent land cells. The perimeter is 4 * land - 2 * neighbors.
TimeComplexity:O(m×n)
SpaceComplexity:O(1)
Solution 3 - DFS (Depth First Search)
Start DFS from any land cell. At each cell, if we step out of bounds or onto water, that's a perimeter edge (+1). Use a visited set to avoid re-counting cells.
TimeComplexity:O(m×n)
SpaceComplexity:O(m×n)
""" 0001.1 - Solution 1 - Brute Force Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0001.1 - Solution 1 - Using Enumerate """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""Two Sum Function"""
for key1, num1 in enumerate(nums):
for key2, num2 in enumerate(nums[key1 + 1 :], key1 + 1):
# enumerate(iterable, start=1) -> start value determines starting index
# Printing value and key will output 1 i , 2 j, 3 k, 4 l
if num1 + num2 == target:
return [key1, key2]
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().twoSum([3, 3], 6))
print(Solution().twoSum([3, 2, 4], 6))
print(Solution().twoSum([2, 7, 11, 15], 9))
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0463.1 - Solution 1 - Counting Edges (Iterative) """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def islandPerimeter(self, grid: List[List[int]]) -> int:
"""Island Perimeter Function"""
rows, cols = len(grid), len(grid[0])
perimeter = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
perimeter += 4
if r > 0 and grid[r - 1][c] == 1:
perimeter -= 2
if c > 0 and grid[r][c - 1] == 1:
perimeter -= 2
return perimeter
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]])) # 16
print(Solution().islandPerimeter([[1]])) # 4
print(Solution().islandPerimeter([[1, 0]])) # 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0463.2 - Solution 2 - Counting Land and Neighbors """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def islandPerimeter(self, grid: List[List[int]]) -> int:
"""Island Perimeter Function"""
rows, cols = len(grid), len(grid[0])
land = 0
neighbors = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
land += 1
if r < rows - 1 and grid[r + 1][c] == 1:
neighbors += 1
if c < cols - 1 and grid[r][c + 1] == 1:
neighbors += 1
return 4 * land - 2 * neighbors
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]])) # 16
print(Solution().islandPerimeter([[1]])) # 4
print(Solution().islandPerimeter([[1, 0]])) # 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0463.3 - Solution 3 - DFS Approach """
#####################################################################################
# Imports
#####################################################################################
from typing import List
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def islandPerimeter(self, grid: List[List[int]]) -> int:
"""Island Perimeter Function"""
rows, cols = len(grid), len(grid[0])
visited = set()
def dfs(r: int, c: int) -> int:
if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
return 1
if (r, c) in visited:
return 0
visited.add((r, c))
return dfs(r + 1, c) + dfs(r - 1, c) + dfs(r, c + 1) + dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
return dfs(r, c)
return 0
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().islandPerimeter([[0, 1, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [1, 1, 0, 0]])) # 16
print(Solution().islandPerimeter([[1]])) # 4
print(Solution().islandPerimeter([[1, 0]])) # 4
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()