The Greedy method describes:
The greedy algorithm always makes the best choice in the current view. In other words, the greedy algorithm does not take the overall optimal consideration, the choice that it makes is only in some sense the local optimal choice.
In some cases, even if the greedy algorithm can not get the whole optimal solution, the final result is a good approximation of the optimal solution.
Problem Description:
The existing set of activities is arranged by you, but it requires you to arrange as many activities as possible within the shortest time.
The code is as follows:
The activity class is used to encapsulate the active object, including the start and end times of the activity, and the comparable interface to sort by end time, not descending
Public class activity implements comparable<activity> {private string starttime;//active start time private string endtime;/
/Activity End time public activities (String starttime,string endtime) {this.starttime=starttime;
This.endtime=endtime;
/** * By activity end time non-descending sort/@Override public int compareTo (active obj) {String targetendtime=obj.getendtime ();
int Endhour=integer.parseint (Endtime.split (":") [0]);
int Endmin=integer.parseint (Endtime.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; } @Override
Public String toString () {return starttime + "~" + endtime; }
}
The Activityplan class is a concrete execution class that arranges activities through its layout method
public class Activityplan {list<activity> res=new arraylist<activity> ();
Public list<activity> layout (iterator<activity> activities) {if (!activities.hasnext ()) {return res;
Activity 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 last activity/private Boolean legal (string preendtime, String thisstarttime) {int prehour=i
Nteger.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~8:50 9:30~10:30 10:40~11:30