Skip to content

Commit a5ef6aa

Browse files
committed
Add: algorithm of programmers
1 parent dc7b997 commit a5ef6aa

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/main/java/io/github/imsejin/study/programmers/L150370.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int[] solve0(String today, String[] terms, String[] privacies) {
3131
Integer month = termMap.get(type);
3232
LocalDate deleteDate = joinDate.plusMonths(month);
3333

34-
if (deleteDate.compareTo(todayDate) <= 0) {
34+
if (!deleteDate.isAfter(todayDate)) {
3535
privaciesToDelete.add(i + 1);
3636
}
3737
}
@@ -112,6 +112,7 @@ public DateString plusMonth(int monthToAdd) {
112112
int year = this.year;
113113
if (this.month + monthToAdd > 12) {
114114
year += (this.month + monthToAdd) / 12;
115+
if (month == 12) year--;
115116
}
116117

117118
return new DateString(year, month, this.day);
@@ -124,7 +125,13 @@ public String toString() {
124125

125126
@Override
126127
public int compareTo(DateString o) {
127-
return this.toString().compareTo(o.toString());
128+
int comparison = Integer.compare(this.year, o.year);
129+
if (comparison != 0) return comparison;
130+
131+
comparison = Integer.compare(this.month, o.month);
132+
if (comparison != 0) return comparison;
133+
134+
return Integer.compare(this.day, o.day);
128135
}
129136
}
130137

src/test/groovy/io/github/imsejin/study/programmers/L150370Spec.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class L150370Spec extends Specification {
1010

1111
then:
1212
actual == expected as int[]
13+
actual == L150370.solve1(today, terms as String[], privacies as String[])
1314

1415
where:
1516
today | terms | privacies || expected

0 commit comments

Comments
 (0)