From 76b64615a073dba9aac0925399749bb2b77b8198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=B6rmet=20Yiltiz?= Date: Fri, 24 May 2024 18:29:07 -0400 Subject: [PATCH 1/7] Add Swift implementation --- .../README_EN.md | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) 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..f58fa0b8f997d 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 @@ -371,6 +371,52 @@ func jobScheduling(startTime []int, endTime []int, profit []int) int { return dp[n] } ``` +#### 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!) + } +} +``` #### TypeScript From 71e31b07601bc7c89d112a19cc8e03bd404b7b0d Mon Sep 17 00:00:00 2001 From: hyiltiz Date: Fri, 24 May 2024 22:45:33 +0000 Subject: [PATCH 2/7] style: format code and docs with prettier --- .../README_EN.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 f58fa0b8f997d..bf18b43ffc044 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 @@ -371,6 +371,7 @@ func jobScheduling(startTime []int, endTime []int, profit []int) int { return dp[n] } ``` + #### Swift ```swift @@ -385,9 +386,9 @@ func binarySearch(inputArr: [T], searchItem: T) -> Int? { if (inputArr[currentIndex] <= searchItem) { lowerIndex = currentIndex + 1 } else { - upperIndex = currentIndex + upperIndex = currentIndex }} - + if (inputArr[upperIndex] <= searchItem) {return upperIndex + 1} return lowerIndex @@ -397,13 +398,13 @@ func binarySearch(inputArr: [T], searchItem: T) -> Int? { 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) @@ -412,7 +413,7 @@ func jobScheduling(_ startTime: [Int], _ endTime: [Int], _ profit: [Int]) -> Int } else { profits.append(profits.last!) } - } + } return (profits.last!) } } From 9f5dbf2725f71aa4aebf31f3520080a53068040d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=B6rmet=20Yiltiz?= Date: Thu, 30 May 2024 00:59:23 -0400 Subject: [PATCH 3/7] Update README.md --- .../README.md | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) 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..e0a95a11170b6 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 @@ -380,6 +380,53 @@ func jobScheduling(startTime []int, endTime []int, profit []int) int { } ``` +#### 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!) + } +} +``` + #### TypeScript ```ts From 34b6e7312710ba3c1a0d14c006d7077c6ad69027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=B6rmet=20Yiltiz?= Date: Thu, 30 May 2024 00:59:56 -0400 Subject: [PATCH 4/7] Create Solution.swift --- .../Solution.swift | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift 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..16fa85f0c04c4 --- /dev/null +++ b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift @@ -0,0 +1,42 @@ +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!) + } +} From 2decdbbb614fe4c6c3f26d87f3991cbe647d8d75 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 30 May 2024 14:54:36 +0800 Subject: [PATCH 5/7] Update Solution.swift --- .../Solution.swift | 79 ++++++++++--------- 1 file changed, 40 insertions(+), 39 deletions(-) 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 index 16fa85f0c04c4..0dbb12f4712bf 100644 --- a/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift +++ b/solution/1200-1299/1235.Maximum Profit in Job Scheduling/Solution.swift @@ -1,42 +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!) - } + 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! + } } From 1ac57550ec7f2715546c17473a8fab040e93fd39 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 30 May 2024 14:55:13 +0800 Subject: [PATCH 6/7] Update README_EN.md --- .../README_EN.md | 95 ++++++++++--------- 1 file changed, 48 insertions(+), 47 deletions(-) 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 bf18b43ffc044..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 @@ -372,53 +372,6 @@ func jobScheduling(startTime []int, endTime []int, profit []int) int { } ``` -#### 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!) - } -} -``` - #### TypeScript ```ts @@ -451,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! + } +} +``` + From cbaa86e22497f14a13aee944e03eb6f9d403b048 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 30 May 2024 14:55:41 +0800 Subject: [PATCH 7/7] Update README.md --- .../README.md | 95 ++++++++++--------- 1 file changed, 48 insertions(+), 47 deletions(-) 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 e0a95a11170b6..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 @@ -380,53 +380,6 @@ func jobScheduling(startTime []int, endTime []int, profit []int) int { } ``` -#### 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!) - } -} -``` - #### TypeScript ```ts @@ -459,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! + } +} +``` +