diff --git a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README.md b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README.md index 8e41b9e9c8e62..15e0a6f28e407 100644 --- a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README.md +++ b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README.md @@ -412,6 +412,54 @@ function jobScheduling(startTime: number[], endTime: number[], profit: number[]) } ``` +#### Swift + +```swift +class Solution { + + func binarySearch(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! + } +} +``` + diff --git a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README_EN.md b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README_EN.md index ba9ee03430293..d0fb9a25a120b 100644 --- a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README_EN.md +++ b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/README_EN.md @@ -404,6 +404,54 @@ function jobScheduling(startTime: number[], endTime: number[], profit: number[]) } ``` +#### Swift + +```swift +class Solution { + + func binarySearch(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! + } +} +``` + diff --git a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift new file mode 100644 index 0000000000000..0dbb12f4712bf --- /dev/null +++ b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift @@ -0,0 +1,43 @@ +class Solution { + + func binarySearch(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! + } +}