Skip to content

[doitduri] Week 07 solutions #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions longest-substring-without-repeating-characters/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
func lengthOfLongestSubstring(_ s: String) -> Int {
var charIndexMap = [Character: Int]()
var maxLength = 0
var startIndex = 0

for (i, char) in Array(s).enumerated() {
if let lastIndex = charIndexMap[char], lastIndex >= startIndex {
startIndex = lastIndex + 1
}

let currentLength = i - startIndex + 1
maxLength = max(maxLength, currentLength)

charIndexMap[char] = i
}

return maxLength
}
}
34 changes: 34 additions & 0 deletions number-of-islands/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
func numIslands(_ grid: [[Character]]) -> Int {
guard !grid.isEmpty else { return 0 }

let rows = grid.count
let colums = grid[0].count
var visited = grid
var islandCount = 0

func dfs(_ row: Int, _ col: Int) {
guard row >= 0 && row < rows && col >= 0 && col < colums && visited[row][col] == "1" else {
return
}

visited[row][col] = "0"

dfs(row - 1, col)
dfs(row + 1, col)
dfs(row, col - 1)
dfs(row, col + 1)
Comment on lines +16 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀를 사용하면 공간복잡도는 줄어들겠네요!!

}

for row in 0..<rows {
for col in 0..<colums {
if visited[row][col] == "1" {
islandCount += 1
dfs(row, col)
}
}
}

return islandCount
}
}
14 changes: 14 additions & 0 deletions reverse-linked-list/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
var current = head, previous: ListNode? = nil

while current != nil {
let next = current?.next
current?.next = previous
previous = current
current = next
}

return previous
}
}
60 changes: 60 additions & 0 deletions set-matrix-zeroes/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Solution {
func setZeroes(_ matrix: inout [[Int]]) {
let m = matrix.count
let n = matrix[0].count

var firstRowHasZero = false
var firstColHasZero = false

for i in 0..<m {
if matrix[i][0] == 0 {
firstColHasZero = true
break
}
}

for j in 0..<n {
if matrix[0][j] == 0 {
firstRowHasZero = true
break
}
}

for i in 1..<m {
for j in 1..<n {
if matrix[i][j] == 0 {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}

for i in 1..<m {
if matrix[i][0] == 0 {
for j in 1..<n {
matrix[i][j] = 0
}
}
}

for j in 1..<n {
if matrix[0][j] == 0 {
for i in 1..<m {
matrix[i][j] = 0
}
}
}

if firstRowHasZero {
for j in 0..<n {
matrix[0][j] = 0
}
}

if firstColHasZero {
for i in 0..<m {
matrix[i][0] = 0
}
}
}
}
21 changes: 21 additions & 0 deletions unique-paths/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
func uniquePaths(_ m: Int, _ n: Int) -> Int {
var dp = Array(repeating: Array(repeating: 0, count: n), count: m)

for j in 0..<n {
dp[0][j] = 1
}

for i in 0..<m {
dp[i][0] = 1
}

for i in 1..<m {
for j in 1..<n {
dp[i][j] = dp[i-1][j] + dp[i][j-1]
}
}

return dp[m-1][n-1]
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 bfs를 사용해서 문제를 풀었는데 DP로도 풀어지는 군요!
한번 시도해보아야 겠네요