-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Hyun] Week 6 Solution Explanation #123
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
fbff392
feat: Add solution for LeetCode problem 11
WhiteHyun c957612
feat: Add solution for LeetCode problem 3
WhiteHyun a142303
refactor: Refactor code of problem 3
WhiteHyun 24f4a4d
feat: Add solution for LeetCode problem 424
WhiteHyun 3629c89
feat: Add solution for LeetCode problem 153
WhiteHyun c331bae
feat: Add solution for LeetCode problem 33
WhiteHyun 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
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,30 @@ | ||
// | ||
// 11. Container With Most Water | ||
// https://leetcode.com/problems/container-with-most-water/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/06/08. | ||
// | ||
|
||
class Solution { | ||
func maxArea(_ height: [Int]) -> Int { | ||
var totalArea = 0 | ||
var left = 0 | ||
var right = height.endIndex - 1 | ||
|
||
while left < right { | ||
let area = (right - left) * min(height[left], height[right]) | ||
if area > totalArea { | ||
totalArea = area | ||
} | ||
|
||
if height[left] < height[right] { | ||
left += 1 | ||
} else { | ||
right -= 1 | ||
} | ||
} | ||
|
||
return totalArea | ||
} | ||
} |
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,25 @@ | ||
// | ||
// 153. Find Minimum in Rotated Sorted Array | ||
// https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/06/09. | ||
// | ||
|
||
class Solution { | ||
func findMin(_ nums: [Int]) -> Int { | ||
var left = 0 | ||
var right = nums.count - 1 | ||
while left < right { | ||
let mid = (left + right) >> 1 | ||
|
||
if nums[mid] > nums[right] { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
|
||
return nums[left] | ||
} | ||
} |
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,30 @@ | ||
// | ||
// 424. Longest Repeating Character Replacement | ||
// https://leetcode.com/problems/longest-repeating-character-replacement/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/06/08. | ||
// | ||
|
||
final class Solution { | ||
func characterReplacement(_ s: String, _ k: Int) -> Int { | ||
var maxFrequency = 0 | ||
var i = 0 | ||
var counter: [Character: Int] = [:] | ||
let array = Array(s) | ||
|
||
for index in array.indices { | ||
counter[array[index], default: 0] += 1 | ||
if maxFrequency < counter[array[index]]! { | ||
maxFrequency = counter[array[index]]! | ||
} | ||
|
||
if index - i + 1 > maxFrequency + k { | ||
counter[array[i]]! -= 1 | ||
i += 1 | ||
} | ||
} | ||
|
||
return array.count - i | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
longest-substring-without-repeating-characters/WhiteHyun.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,40 @@ | ||
// | ||
// 3. Longest Substring Without Repeating Characters | ||
// https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/06/08. | ||
// | ||
|
||
class Solution { | ||
func lengthOfLongestSubstring(_ s: String) -> Int { | ||
var longest: Int = .min | ||
Invidam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let array = Array(s) | ||
|
||
var set: Set<Character> = [] | ||
var startIndex = 0 | ||
for index in array.indices { | ||
if set.contains(array[index]) == false { | ||
set.insert(array[index]) | ||
continue | ||
} | ||
|
||
if longest < index - startIndex { | ||
longest = index - startIndex | ||
} | ||
|
||
while array[startIndex] != array[index] { | ||
set.remove(array[startIndex]) | ||
startIndex += 1 | ||
} | ||
startIndex += 1 | ||
} | ||
|
||
if longest < array.endIndex - startIndex { | ||
longest = array.endIndex - startIndex | ||
} | ||
|
||
|
||
return longest | ||
} | ||
} |
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,41 @@ | ||
// | ||
// 33. Search in Rotated Sorted Array | ||
// https://leetcode.com/problems/search-in-rotated-sorted-array/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/06/10. | ||
// | ||
|
||
final class Solution { | ||
func search(_ nums: [Int], _ target: Int) -> Int { | ||
guard nums.count != 1 | ||
else { | ||
return nums[0] == target ? 0 : -1 | ||
} | ||
|
||
var left = 0 | ||
var right = nums.count - 1 | ||
|
||
while left <= right { | ||
let mid = (left + right) >> 1 | ||
|
||
if nums[mid] == target { return mid } | ||
|
||
if nums[left] <= nums[mid] { | ||
if target > nums[mid] || target < nums[left] { | ||
left = mid + 1 | ||
} else { | ||
right = mid - 1 | ||
} | ||
} else { | ||
if target < nums[mid] || target > nums[right] { | ||
right = mid - 1 | ||
} else { | ||
left = mid + 1 | ||
} | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.