zoukankan      html  css  js  c++  java
  • LeetCode 826. Most Profit Assigning Work

    原题链接在这里:https://leetcode.com/problems/most-profit-assigning-work/

    题目:

    We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job. 

    Now we have some workers. worker[i] is the ability of the ith 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]

    题解:

    Pair the difficulty and profit into job.

    Sort jobs based on difficulty. And sort the worker.

    Get the first worker, go through jobs from beginning and update its best profit as long as worker could accomplish job difficulity.

    For the next worker, it doesn't need to iterate from beginning again, it could start from previous position, because it can do more difficult job than previous worker. So, we only need to iterate jobs once.

    Like there are two pointers, one pointing at job and the other pointing at worker. For next pointer at worker, job pointer only need to move forward, not backword.

    Time Complexity: O(nlogn + wlogw). n = difficulty.length. w = worker.length.

    Space: O(n).

    AC Java: 

     1 class Solution {
     2     public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
     3         int n = difficulty.length;
     4         Job [] jobs = new Job[n];
     5         for(int i = 0; i<n; i++){
     6             jobs[i] = new Job(difficulty[i], profit[i]);
     7         }
     8         
     9         Arrays.sort(jobs, (a, b) -> a.diff-b.diff);
    10         Arrays.sort(worker);
    11         int i = 0;
    12         int best = 0;
    13         int res = 0;
    14         for(int w : worker){
    15             while(i<n && w>=jobs[i].diff){
    16                 best = Math.max(best, jobs[i].profit);
    17                 i++;
    18             }
    19             
    20             res += best;
    21         }
    22         
    23         return res;
    24     }
    25 }
    26 
    27 class Job{
    28     int diff;
    29     int profit;
    30     public Job(int diff, int profit){
    31         this.diff = diff;
    32         this.profit = profit;
    33     }
    34 }
  • 相关阅读:
    浅谈Javascript数据属性与访问器属性
    深入浅析JavaScript中的constructor
    javascript 继承
    很认真的聊一聊程序员的自我修养
    JavaScript数据属性与访问器属性
    Js中的数据属性和访问器属性
    [javascript基础]constructor与prototype
    C# System.Net.Mail
    Execl (转)导入导出execl 全
    delegate 委托方法
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11376555.html
Copyright © 2011-2022 走看看