<p>解决方案爪哇语:你知道吗</p>
<pre><code>public class Ress {
Set<Interval> intervals = new HashSet<>();
public static void main(String[] args) {
String[] input = {"16 1","2 16","10 25","31 26","15 31"};
System.out.println(minimumLogins(input));
}
public static int minimumLogins(String[] input) {
Ress r = new Ress();
for (String s : input) {
Scanner sc = new Scanner(s);
Bill b = new Bill(sc.nextInt(), sc.nextInt());
r.addToIntervals(b);
}
//If we have to print the dates on which to pay the bills, then it will be any date inside the different intervals.
return r.intervals.size();
}
public void addToIntervals (Bill b) {
Interval i = null;
for(Interval in:intervals){
if(in.canAddBill(b)) {
i = in;
break;
}
}
if(i==null) {
i = new Interval(b);
intervals.add(i);
}
i.addBill(b);
}
}
class Bill{
int receiveDate;
int dueDate;
public Bill(int a, int b) {
receiveDate = a;
if(b<a) {
// adding to represent the date in next month
b += 31;
}
dueDate = b;
}
}
class Interval{
int startDate;;
int endDate;
List<Bill> bills;
public Interval(Bill b) {
startDate = b.receiveDate;
endDate = b.dueDate;
bills = new ArrayList<>();
}
public void addBill(Bill b) {
bills.add(b);
//Reset interval boundaries based on the newly added bill
if(b.dueDate<this.endDate) {
endDate = b.dueDate;
}
if(b.receiveDate>this.startDate) {
startDate = b.receiveDate;
}
}
public boolean canAddBill(Bill b) {
/*
* Bill can be added if it lies in the interval.
*/
if(b.dueDate<=this.endDate || b.receiveDate<=this.endDate) {
return true;
}
return false;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + endDate;
result = prime * result + startDate;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Interval other = (Interval) obj;
if (endDate != other.endDate)
return false;
if (startDate != other.startDate)
return false;
return true;
}
}
</code></pre>