-
-
Notifications
You must be signed in to change notification settings - Fork 245
[SeongA] WEEK 07 solutions #1884
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
longest-substring-without-repeating-characters/delight010.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution { | ||
// Time complexity O(N) | ||
// Space complexity O(min(m,n)) | ||
func lengthOfLongestSubstring(_ s: String) -> Int { | ||
if s.isEmpty { | ||
return 0 | ||
} | ||
var maxLength = 0 | ||
var startIndex = 0 | ||
var charSet: Set<Character> = [] | ||
let charArray = Array(s) | ||
|
||
for right in 0..<charArray.count { | ||
while charSet.contains(charArray[right]) { | ||
charSet.remove(charArray[startIndex]) | ||
startIndex += 1 | ||
} | ||
|
||
charSet.insert(charArray[right]) | ||
maxLength = max(maxLength, right - startIndex + 1) | ||
} | ||
|
||
return maxLength | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
public class ListNode { | ||
public var val: Int | ||
public var next: ListNode? | ||
public init() { self.val = 0; self.next = nil; } | ||
public init(_ val: Int) { self.val = val; self.next = nil; } | ||
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } | ||
} | ||
|
||
class Solution { | ||
// Time complexity O(n) | ||
// Space complexity O(1) | ||
func reverseList(_ head: ListNode?) -> ListNode? { | ||
var reverseList: ListNode? = nil | ||
var currentHead = head | ||
while let node = currentHead { | ||
currentHead = node.next | ||
node.next = reverseList | ||
reverseList = node | ||
} | ||
return reverseList | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Solution { | ||
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. 저와는 다른 접근법으로 푸셔서 많이 배워갑니다..!!! 공간 복잡도나 시간 복잡도를 올려주셔도 좋을 것 같아요! 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. 추가하였습니다!! |
||
// Time complexity O(MN) | ||
// Space complexity O(1) | ||
func setZeroes(_ matrix: inout [[Int]]) { | ||
var firstRowHasZero = false | ||
var firstColHasZero = false | ||
|
||
for col in 0..<matrix[0].count { | ||
if matrix[0][col] == 0 { | ||
firstRowHasZero = true | ||
break | ||
} | ||
} | ||
|
||
for row in 0..<matrix.count { | ||
if matrix[row][0] == 0 { | ||
firstColHasZero = true | ||
break | ||
} | ||
} | ||
|
||
// marking | ||
for row in 0..<matrix.count { | ||
for col in 0..<matrix[row].count { | ||
if matrix[row][col] == 0 { | ||
matrix[row][0] = 0 | ||
matrix[0][col] = 0 | ||
} | ||
} | ||
} | ||
|
||
// row | ||
for row in 1..<matrix.count { | ||
if matrix[row][0] == 0 { | ||
for col in 0..<matrix[row].count { | ||
matrix[row][col] = 0 | ||
} | ||
} | ||
} | ||
|
||
// column | ||
for col in 1..<matrix[0].count { | ||
if matrix[0][col] == 0 { | ||
for row in 0..<matrix.count { | ||
matrix[row][col] = 0 | ||
} | ||
} | ||
} | ||
|
||
if firstRowHasZero { | ||
for col in 0..<matrix[0].count { | ||
matrix[0][col] = 0 | ||
} | ||
} | ||
|
||
if firstColHasZero { | ||
for row in 0..<matrix.count { | ||
matrix[row][0] = 0 | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
// Time complexity O(MN) | ||
// Space complexity O(MN) | ||
func uniquePaths(_ m: Int, _ n: Int) -> Int { | ||
let column = Array(repeating: 1, count: n) | ||
var grid: [[Int]] = Array(repeating: column, count: m) | ||
for i in 1..<m { | ||
for j in 1..<n { | ||
grid[i][j] = grid[i - 1][j] + grid[i][j - 1] | ||
} | ||
} | ||
return grid.last!.last! | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
let charArray = Array(s)를 하고 있기 때문에 공간 복잡도에서 무조건 String의 길이는 사용이 될거라 O(n) 이지 않을까요?
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.
다시 보니 그러네요!! 아직 복잡도 계산하는 데 익숙치않아서 클로드의 도움을 받았는 데, 한 번 더 유심히 봐야겠어요. 감사합니다