package GreedyAlgo; import java.util.Arrays; import java.util.Comparator; public class JobSequenceProblem { public static void main(String[] args) { int job [][]= {{4,20},{2,10},{1,40},{3,30}}; Arrays.sort(job, Comparator.comparingDouble(o -> o[1])); int pro= job[job.length-1][1]; int lastend= job[job.length-1][0]; for(int i = job.length-1;i>=0;i--){ if(lastend < job[i][0]){ pro+=job[i][1]; } } System.out.println(pro); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter