-
-
Notifications
You must be signed in to change notification settings - Fork 339
[tedkimdev] WEEK 07 Solutions #2534
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // TC: O(n) | ||
| // SC: O(m) | ||
| func lengthOfLongestSubstring(s string) int { | ||
| charIndex := map[byte]int{} | ||
|
|
||
| maxLen := 0 | ||
| left := 0 | ||
| for right := 0; right < len(s); right++ { | ||
| if idx, ok := charIndex[s[right]]; ok && idx >= left { | ||
| left = idx + 1 | ||
| } | ||
| charIndex[s[right]] = right | ||
| if right-left+1 > maxLen { | ||
| maxLen = right - left + 1 | ||
| } | ||
| } | ||
|
|
||
| return maxLen | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(m * n) | ||
| func numIslands(grid [][]byte) int { | ||
| rows, cols := len(grid), len(grid[0]) | ||
| islands := 0 | ||
|
|
||
| var dfs func(r, c int) | ||
| dfs = func(r, c int) { | ||
| if r < 0 || c < 0 || | ||
| r >= rows || c >= cols || | ||
| grid[r][c] == '0' { | ||
| return | ||
| } | ||
| grid[r][c] = '0' | ||
| dfs(r+1, c) | ||
| dfs(r-1, c) | ||
| dfs(r, c+1) | ||
| dfs(r, c-1) | ||
| } | ||
|
|
||
| for r := 0; r < rows; r++ { | ||
| for c := 0; c < cols; c++ { | ||
| if grid[r][c] == '1' { | ||
| dfs(r, c) | ||
| islands++ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return islands | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * type ListNode struct { | ||
| * Val int | ||
| * Next *ListNode | ||
| * } | ||
| */ | ||
|
|
||
| // TC: O(n) | ||
| // SC: O(1) | ||
| func reverseList(head *ListNode) *ListNode { | ||
| var prev *ListNode | ||
| cur := head | ||
|
|
||
| for cur != nil { | ||
| temp := cur.Next | ||
| cur.Next = prev | ||
| prev = cur | ||
| cur = temp | ||
| } | ||
| return prev | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(1) | ||
| func setZeroes(matrix [][]int) { | ||
| rowNum, colNum := len(matrix), len(matrix[0]) | ||
|
|
||
| firstRowIsZero := false | ||
|
|
||
| for r := 0; r < rowNum; r++ { | ||
| for c := 0; c < colNum; c++ { | ||
| if matrix[r][c] == 0 { | ||
| matrix[0][c] = 0 | ||
| if r > 0 { | ||
| matrix[r][0] = 0 | ||
| } else { | ||
| firstRowIsZero = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for r := 1; r < rowNum; r++ { | ||
| for c := 1; c < colNum; c++ { | ||
| if matrix[r][0] == 0 || matrix[0][c] == 0 { | ||
| matrix[r][c] = 0 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if matrix[0][0] == 0 { // first column is zero | ||
| for r := 0; r < rowNum; r++ { | ||
| matrix[r][0] = 0 | ||
| } | ||
| } | ||
|
|
||
| if firstRowIsZero { | ||
| for c := 0; c < colNum; c++ { | ||
| matrix[0][c] = 0 | ||
| } | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(m * n) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 각 행의 계산이 바로 아래 행에만 의존하므로, 일차원 배열로 최적화하면
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇네요! 감사합니다. 다음에 시도해보겠습니다. |
||
| func uniquePaths(m int, n int) int { | ||
| dp := make([][]int, m+1) | ||
| for i := range dp { | ||
| dp[i] = make([]int, n+1) | ||
| } | ||
| dp[m-1][n-1] = 1 | ||
|
|
||
| for i := m - 1; i >= 0; i-- { | ||
| for j := n - 1; j >= 0; j-- { | ||
| dp[i][j] += dp[i+1][j] + dp[i][j+1] | ||
| } | ||
| } | ||
|
|
||
| return dp[0][0] | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석