-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: Add Swift implementation 1235.Maximum Profit in Job Scheduling #2909
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
8 commits
Select commit
Hold shift + click to select a range
76b6461
Add Swift implementation
hyiltiz 71e31b0
style: format code and docs with prettier
hyiltiz 9f5dbf2
Update README.md
hyiltiz 34b6e73
Create Solution.swift
hyiltiz a71d956
Merge branch 'main' into patch-1
hyiltiz 2decdbb
Update Solution.swift
yanglbme 1ac5755
Update README_EN.md
yanglbme cbaa86e
Update README.md
yanglbme 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
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
43 changes: 43 additions & 0 deletions
solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.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,43 @@ | ||
| class Solution { | ||
|
|
||
| func binarySearch<T: Comparable>(inputArr: [T], searchItem: T) -> Int? { | ||
| var lowerIndex = 0 | ||
| var upperIndex = inputArr.count - 1 | ||
|
|
||
| while lowerIndex < upperIndex { | ||
| let currentIndex = (lowerIndex + upperIndex) / 2 | ||
| if inputArr[currentIndex] <= searchItem { | ||
| lowerIndex = currentIndex + 1 | ||
| } else { | ||
| upperIndex = currentIndex | ||
| } | ||
| } | ||
|
|
||
| if inputArr[upperIndex] <= searchItem { | ||
| return upperIndex + 1 | ||
| } | ||
| return lowerIndex | ||
| } | ||
|
|
||
| func jobScheduling(_ startTime: [Int], _ endTime: [Int], _ profit: [Int]) -> Int { | ||
| let zipList = zip(zip(startTime, endTime), profit) | ||
| var table: [(startTime: Int, endTime: Int, profit: Int, cumsum: Int)] = [] | ||
|
|
||
| for ((x, y), z) in zipList { | ||
| table.append((x, y, z, 0)) | ||
| } | ||
| table.sort(by: { 0ドル.endTime < 1ドル.endTime }) | ||
| let sortedEndTime = endTime.sorted() | ||
|
|
||
| var profits: [Int] = [0] | ||
| for iJob in table { | ||
| let index: Int! = binarySearch(inputArr: sortedEndTime, searchItem: iJob.startTime) | ||
| if profits.last! < profits[index] + iJob.profit { | ||
| profits.append(profits[index] + iJob.profit) | ||
| } else { | ||
| profits.append(profits.last!) | ||
| } | ||
| } | ||
| return profits.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.