Greedy description:
Greedy algorithms always make the best choice for the moment. That is to say, the greedy algorithm does not take the overall optimization into consideration. All it makes is a local optimal choice in a certain sense.
In some cases, even if the greedy algorithm cannot obtain the overall optimal solution, the final result is a good approximation of the optimal solution.
Problem description:
You can schedule a group of activities, but you are required to schedule as many activities as possible in the shortest time.
The Code is as follows:
The activity class is used to encapsulate activity objects, including the Activity start time and end time. In addition, the comparable interface is implemented to sort the objects by the end time in non-descending order.
Public class activity implements comparable <activity> {private string starttime; // Activity start time private string endtime; // activity end time public activity (string starttime, string endtime) {This. starttime = starttime; this. endtime = endtime;}/*** sort by activity end time in non-descending order */@ overridepublic int compareto (activity OBJ) {string targetendtime = obj. getendtime (); int endhour = integer. parseint (endtime. split (":") [0]); int endmin = integer. parseint (E Ndtime. split (":") [1]); int targetendhour = integer. parseint (targetendtime. split (":") [0]); int targetendmin = integer. parseint (targetendtime. split (":") [1]); int I = endmin> targetendmin? 1 :( endmin <targetendmin? -1:0); Return endhour> targetendhour? 1 :( endhour <targetendhour? -1 :( I);} Public String getstarttime () {return starttime;} public void setstarttime (string starttime) {This. starttime = starttime;} Public String getendtime () {return endtime;} public void setendtime (string endtime) {This. endtime = endtime ;}@ overridepublic string tostring () {return starttime + "~ "+ Endtime ;}}
The activityplan class is a specific execution class and uses its layout method to schedule activities.
Public class activityplan {list <activity> res = new arraylist <activity> (); public list <activity> layout (iterator <activity> Activities) {If (! Activities. hasnext () {return res;} activity = activities. next (); int size = res. size (); If (size = 0) {res. add (activity);} else {activity preactivity = res. get (size-1); string preendtime = preactivity. getendtime (); string thisstarttime = activity. getstarttime (); If (Legal (preendtime, thisstarttime) {res. add (activity) ;}} return layout (activities) ;}/ *** whether the start time of the current activity is after the end time of the previous activity */private Boolean legal (string preen Dtime, string thisstarttime) {int prehour = integer. parseint (preendtime. split (":") [0]); int premin = integer. parseint (preendtime. split (":") [1]); int thishour = integer. parseint (thisstarttime. split (":") [0]); int thismin = integer. parseint (thisstarttime. split (":") [1]); int I = premin> thismin? -1 :( premin <thismin? 1:0); Int J = prehour> thishour? -1 :( prehour <thishour? 1 :( I); Return j> = 0 ;}}
Main class for code Testing
public class Main {public static void main(String[] args) {Set<Activity> activitys=new TreeSet<Activity>();activitys.add(new Activity("9:30","10:30"));activitys.add(new Activity("7:30","8:50"));activitys.add(new Activity("10:40","11:30"));activitys.add(new Activity("8:00","10:30"));activitys.add(new Activity("8:30","11:40"));activitys.add(new Activity("10:30","12:30"));ActivityPlan plan=new ActivityPlan();List<Activity> res=plan.layout(activitys.iterator());for(Activity activity:res){System.out.print("\t"+activity);}}}
Program output: 7: 30 ~ AM to AM ~ Am ~ 40 AM ~