[Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10569392.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
We have jobs: difficulty[i]
is the difficulty of the i
th job, and profit[i]
is the profit of the i
th job.
Now we have some workers. worker[i]
is the ability of the i
th worker, which means that this worker can only complete a job with difficulty at most worker[i]
.
Every worker can be assigned at most one job, but one job can be completed multiple times.
For example, if 3 people attempt the same job that pays $1, then the total profit will be $3. If a worker cannot complete any job, his profit is $0.
What is the most profit we can make?
Example 1:
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately.
Notes:
1 <= difficulty.length = profit.length <= 10000
1 <= worker.length <= 10000
difficulty[i], profit[i], worker[i]
are in range[1, 10^5]
有一些工作:difficulty[i]
表示第i
个工作的难度,profit[i]
表示第i
个工作的收益。
现在我们有一些工人。worker[i]
是第i
个工人的能力,即该工人只能完成难度小于等于worker[i]
的工作。
每一个工人都最多只能安排一个工作,但是一个工作可以完成多次。
举个例子,如果3个工人都尝试完成一份报酬为1的同样工作,那么总收益为 $3。如果一个工人不能完成任何工作,他的收益为 $0 。
我们能得到的最大收益是多少?
示例:
输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] 输出: 100 解释: 工人被分配的工作难度是 [4,4,6,6] ,分别获得 [20,20,30,30] 的收益。
提示:
1 <= difficulty.length = profit.length <= 10000
1 <= worker.length <= 10000
difficulty[i], profit[i], worker[i]
的范围是[1, 10^5]
580ms
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 var tasks = [(Int, Int)]() 4 for i in 0..<difficulty.count { 5 tasks.append((profit[i], difficulty[i])) 6 } 7 tasks = tasks.sorted {$0.0 > $1.0} 8 var workers = worker.sorted {$0 > $1} 9 var t = 0, w = 0 10 var res = 0 11 while (w < worker.count && t < tasks.count) { 12 if tasks[t].1 <= workers[w] { 13 res += tasks[t].0 14 w += 1 15 } else { 16 t += 1 17 } 18 } 19 return res 20 } 21 }
588ms
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 let dp = zip(difficulty, profit).sorted(by: {$0.0 < $1.0}) 4 5 var p = 0 6 var i = 0, maxp = 0 7 for w in worker.sorted(by: <) { 8 while i < dp.count && w >= dp[i].0 { 9 maxp = max(maxp, dp[i].1) 10 i += 1 11 } 12 p += maxp 13 } 14 return p 15 } 16 }
616ms
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 var dp = [Int : Int]() 4 for i in 0..<difficulty.count { 5 let d = difficulty[i] 6 let p = profit[i] 7 if dp[d, default: 0] < p { 8 dp[d] = p 9 } 10 } 11 let keys = dp.keys.sorted() 12 var maxSoFar = 0 13 var sanitizedD = [Int]() 14 var sanitizedP = [Int]() 15 for i in 0..<keys.count { 16 let key = keys[i] 17 if dp[key]! > maxSoFar { 18 maxSoFar = dp[key]! 19 sanitizedD.append(key) 20 sanitizedP.append(dp[key]!) 21 } 22 } 23 24 var result = 0 25 for i in 0..<worker.count { 26 let w = worker[i] 27 result += maxFittingProfit(forWorker: w, inDifficulties: sanitizedD, withProfits:sanitizedP) 28 } 29 30 return result 31 } 32 33 func maxFittingProfit(forWorker worker: Int, inDifficulties difficulties: [Int], withProfits profits: [Int]) -> Int 34 { 35 var lower = 0, upper = difficulties.count - 1 36 var i = profits.count / 2 37 while (lower <= upper) { 38 if difficulties[i] > worker { 39 upper = i - 1 40 i = (lower + upper) / 2 41 } 42 else if (i != difficulties.count - 1) && (difficulties[i+1] <= worker) { 43 lower = i + 1 44 i = (lower + upper) / 2 45 } 46 else { 47 return profits[i] 48 } 49 } 50 return 0 51 } 52 }
620ms
1 class Solution { 2 func binarySearch(_ difficulty: inout [Int], _ worker: Int) -> Int? { 3 if difficulty.count == 0 || difficulty[0] > worker { 4 return nil 5 } 6 7 var left = 0, right = difficulty.count - 1 8 while left < right { 9 let mid = left + (right - left) / 2 10 if worker >= difficulty[mid] && 11 worker < difficulty[mid + 1] { 12 return mid 13 } 14 15 if worker > difficulty[mid] { 16 left = mid + 1 17 } else { 18 right = mid 19 } 20 } 21 22 return left 23 } 24 25 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 26 var p = 0 27 28 let ed = difficulty.enumerated().sorted(by: { $0.element < $1.element }) 29 var dp = [Int: Int]() 30 var maxp = 0 31 for (index, d) in ed { 32 maxp = max(maxp, profit[index]) 33 dp[d] = maxp 34 } 35 36 var d = difficulty.sorted() 37 for w in worker { 38 let di = binarySearch(&d, w) 39 if let di = di { 40 p += dp[d[di]]! 41 } 42 } 43 return p 44 } 45 }
628ms
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 var jobs = [Int: Int]() // [Difficulty, Profit] 4 for i in 0..<difficulty.count { 5 let d = difficulty[i] 6 if let existing = jobs[d] { 7 jobs[d] = max(existing, profit[i]) 8 } else { 9 jobs[d] = profit[i] 10 } 11 } 12 let sorted = jobs.sorted { (a, b) -> Bool in 13 if a.value == b.value { 14 return a.key > b.key 15 } else { 16 return a.value > b.value 17 } 18 } 19 var current = 0 20 let worker = worker.sorted().reversed() 21 var maxProfit = 0 22 23 for w in worker { 24 while current < sorted.count, sorted[current].key > w { 25 current += 1 26 } 27 28 if current == sorted.count { 29 return maxProfit 30 } else { 31 maxProfit += sorted[current].value 32 } 33 } 34 return maxProfit 35 } 36 }
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 var res:Int = 0 4 var n:Int = profit.count 5 var dp:[Int] = [Int](repeating:0,count:100001) 6 for i in 0..<n 7 { 8 dp[difficulty[i]] = max(dp[difficulty[i]], profit[i]) 9 } 10 for i in 1..<dp.count 11 { 12 dp[i] = max(dp[i], dp[i - 1]) 13 } 14 for ability in worker 15 { 16 res += dp[ability] 17 } 18 return res 19 } 20 }
772ms
1 class Solution { 2 func maxProfitAssignment(_ difficulty: [Int], _ profit: [Int], _ worker: [Int]) -> Int { 3 var max = 0 as Int 4 let count = difficulty.count 5 6 var sortedCache = [[Int]:Int]() 7 for i in 0...count-1{ 8 sortedCache[[difficulty[i],i]] = profit[i] 9 } 10 11 var difficulty_ = sortedCache.keys.sorted{ 12 return $0[0] < $1[0] 13 } 14 15 var maxProfitCache = [Int:Int]() 16 var sortedMPKeys = [Int]() 17 var runningMaxProfit = 0 18 19 for i in 0...count-1{ 20 21 let nextK = difficulty_[i] 22 let dif = nextK[0] 23 var profit = sortedCache[nextK] as! Int 24 25 var nextMax = maxProfitCache[dif] ?? 0 26 let nextMax_ = nextMax 27 28 if runningMaxProfit > profit{ 29 profit = runningMaxProfit 30 } 31 32 if profit > nextMax{ 33 34 runningMaxProfit = profit 35 nextMax = profit 36 maxProfitCache[dif] = nextMax 37 38 if nextMax_ == 0{ 39 sortedMPKeys.append(dif) 40 } 41 } 42 } 43 44 sortedMPKeys = sortedMPKeys.sorted() 45 46 for w in worker{ 47 var maxW = 0 as Int 48 let index = binarySearch(sortedMPKeys, key: w, range: 0 ..< sortedMPKeys.count) 49 if index != nil && index! >= 0{ 50 maxW = maxProfitCache[sortedMPKeys[index!]]! 51 }else if index == nil && index != -1{ 52 maxW = maxProfitCache[sortedMPKeys.last!]! 53 } 54 55 max += maxW 56 } 57 return max 58 } 59 } 60 61 func binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>) -> Int? { 62 return _binarySearch(a, key: key, range: range, -1) 63 } 64 65 func _binarySearch<T: Comparable>(_ a: [T], key: T, range: Range<Int>, _ maxIdxResult : Int) -> Int? { 66 67 var maxIdxResult = maxIdxResult 68 69 if range.lowerBound >= range.upperBound { 70 return maxIdxResult 71 72 } else { 73 let midIndex = range.lowerBound + (range.upperBound - range.lowerBound) / 2 74 if a[midIndex] > key { 75 return _binarySearch(a, key: key, range: range.lowerBound ..< midIndex,maxIdxResult) 76 } else if a[midIndex] < key { 77 maxIdxResult = midIndex 78 return _binarySearch(a, key: key, range: midIndex + 1 ..< range.upperBound,maxIdxResult) 79 } else { 80 return midIndex 81 } 82 } 83 } 84 85 func println(_ format : String, _ args : CVarArg...) { 86 let s = String.init(format: format, arguments: args) 87 print(s, separator: "", terminator: "\n") 88 }
转载于:https://www.cnblogs.com/strengthen/p/10569392.html
[Swift]LeetCode826. 安排工作以达到最大收益 | Most Profit Assigning Work相关推荐
- Leetcode--826. 安排工作以达到最大收益
有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[i]是第i个工人的能力,即该工人只能完成难度小于等于worke ...
- LeetCode:安排工作以达到最大收益【455】
LeetCode:安排工作以达到最大收益[455] 题目描述 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[ ...
- Java实现 LeetCode 826 安排工作以达到最大收益(暴力DP)
826. 安排工作以达到最大收益 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[i]是第i个工人的能力,即该 ...
- LeetCode 826. 安排工作以达到最大收益(map)
1. 题目 有一些工作:difficulty[i] 表示第i个工作的难度,profit[i]表示第i个工作的收益. 现在我们有一些工人.worker[i]是第i个工人的能力,即该工人只能完成难度小于等 ...
- LeetCode刷题之826. 安排工作以达到最大收益
你有 n 个工作和 m 个工人.给定三个数组: difficulty, profit 和 worker ,其中: difficulty[i] 表示第 i 个工作的难度,profit[i] 表示第 i ...
- 化学博士6次投毒同事,只因对方给自己安排工作太多....
点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文募格学术撰写.参考资料来源:抖音长春市中级人民法院.募格课堂.知 ...
- 职场上,怎么对待那些总是以领导口吻给自己安排工作的同事
看到有一个粉丝问了我这样一个问题:某同事总是已领导的口吻给自己安排工作,自己要怎么怼回去? 我对于这个问题的看法如下:这个事情需要看情况而定,但是直接怼回去不见得是处理问题的好方法. 第一种情况,同事 ...
- 如何给下属安排工作?
1 明确任务目标 领导安排任务,一定要把任务目标说清楚,不仅是领导自己单方面清楚,而是双方对任务目标都清楚,这是安排工作的第一步,也是关键的一步. 2 下属主动承诺 领导分派任务之后,不是只管把任务发 ...
- 利用Google日历安排工作计划
适应范围: 建议只使用在2-10人的小型团队或个人生活使用,适合于大项目的向下分解新任务,如果更多的人使用需要更好的规划,比较明显的作用是消息的提醒通知和反馈 目的: 1 清晰每个人的工作任 ...
最新文章
- 有關window.showModalDialog的應用11/30
- GMQ发行稳定币将进一步打破稳定币市场垄断格局
- BNU 34974 MATLAB大法好
- 下坠的小鸟(flappy bird)速算版
- 记一次特别的往事 while 循环
- 关于JS的传递方式的小理解
- 美团NLP中心算法实习生招聘
- 我在富士康挨踢了七年(七. 激情与暴力3)
- L1-034 点赞 (20 分)—团体程序设计天梯赛
- Seasonality Core for mac(世界天气预报)
- Microsoft SQL Server 2008 R2 软件安装
- linux如何安装github下载的东西,Linux如何安装使用GitHub
- iOS相册权限、相机权限、麦克风权限
- 前端基础——html5新增标签
- Java身份证号码校验
- 苹果开发者账户续费 支付授权失败(终极解决方案)
- 高数 | 洛必达法则的隐藏细节、广义洛必达法则(分母无穷直接洛必达)使用条件
- android怎样实现手机触屏,Android触屏过程详解和实现view随手指移动而移动功能
- tp5微信公众号开发(2) ---- 微信被动回复,图文回复,图片回复等 demo实例
- MTL多目标学习介绍综述等
热门文章
- B - Greg's Workout CodeForces - 255A(思维)
- 原生JDBC和工具类的基本实现
- *PAT_B_1052_C++(20分)
- mvc html.display,Asp.Net MVC中的Html.DisplayFor()用于项目列表
- 动画专业艺术里最懂计算机的,美国数字媒体艺术专业了解一下!
- 【sklearn学习】集成算法之梯度提升树GBDT
- codeforce 266c Below the Diagonal 矩阵变换 (思维题)
- 【Ubuntu-Tensorflow】InvalidArgumentError GPU不能使用的问题
- float,double等精度丢失问题
- DSP Builder