0

I am not able to get the correct answer for the folowing question . I saw other approaches but i want to see the mistake i did .I used greedy method to solve the question .Can anyone tell what is wrong with the code ? please...it would be really helpfull.Here is the link to the question - https://practice.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1

int[] JobScheduling(Job arr[], int n)
{
    ArrayList<Job> l = new ArrayList<>();
    for(int i =0;i<arr.length ;i++){
        l.add(arr[i]);
    }
    Collections.sort(l, (a,b)-> b.profit-a.profit);
    
    int time = 0;
    int profit =0;
    int jobDone =0;
    for(int i =0; i<l.size();i++){
        Job j = l.get(i);
        if(j.deadline > time){
            jobDone++;
            profit += j.profit;
        }
    }
    int res[] = new int[2];
    int flag =0;
    res[flag++] = jobDone;
    res[flag++] = profit;
    return res;
}
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    In what way is your code not working as expected? Please elaborate on the specific problem you are observing and what debugging you have done. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 23 '23 at 14:12
  • The answer returned is not correct – Saikat Mandal Feb 23 '23 at 14:20
  • This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 23 '23 at 14:21
  • Although links are OK for background information, your question should have the essentials parts to understand the question. Don't assume that readers will click links. We cannot tell what is wrong with the code when you haven't explained what it is supposed to do, how you call that function, and how the returned value is different from what you expected. – trincot Mar 11 '23 at 14:06

0 Answers0