Skip to content

Commit dc7b997

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

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package io.github.imsejin.study.programmers;
2+
3+
import java.time.LocalDate;
4+
import java.util.*;
5+
6+
/**
7+
* <a href="https://school.programmers.co.kr/learn/courses/30/lessons/150370">개인정보 수집 유효기간</a>
8+
*/
9+
public class L150370 {
10+
11+
static int[] solve0(String today, String[] terms, String[] privacies) {
12+
LocalDate todayDate = parseDate(today);
13+
14+
Map<String, Integer> termMap = new HashMap<>();
15+
for (String term : terms) {
16+
StringTokenizer tokenizer = new StringTokenizer(term);
17+
String type = tokenizer.nextToken();
18+
int month = Integer.parseInt(tokenizer.nextToken());
19+
20+
termMap.put(type, month);
21+
}
22+
23+
List<Integer> privaciesToDelete = new ArrayList<>();
24+
for (int i = 0; i < privacies.length; i++) {
25+
String privacy = privacies[i];
26+
27+
StringTokenizer tokenizer = new StringTokenizer(privacy);
28+
LocalDate joinDate = parseDate(tokenizer.nextToken());
29+
String type = tokenizer.nextToken();
30+
31+
Integer month = termMap.get(type);
32+
LocalDate deleteDate = joinDate.plusMonths(month);
33+
34+
if (deleteDate.compareTo(todayDate) <= 0) {
35+
privaciesToDelete.add(i + 1);
36+
}
37+
}
38+
39+
return privaciesToDelete.stream().mapToInt(it -> it).toArray();
40+
}
41+
42+
static int[] solve1(String today, String[] terms, String[] privacies) {
43+
DateString todayString = new DateString(today);
44+
45+
Map<String, Integer> termMap = new HashMap<>();
46+
for (String term : terms) {
47+
StringTokenizer tokenizer = new StringTokenizer(term);
48+
String type = tokenizer.nextToken();
49+
int month = Integer.parseInt(tokenizer.nextToken());
50+
51+
termMap.put(type, month);
52+
}
53+
54+
List<Integer> privaciesToDelete = new ArrayList<>();
55+
for (int i = 0; i < privacies.length; i++) {
56+
String privacy = privacies[i];
57+
58+
StringTokenizer tokenizer = new StringTokenizer(privacy);
59+
DateString joinDate = new DateString(tokenizer.nextToken());
60+
String type = tokenizer.nextToken();
61+
62+
Integer month = termMap.get(type);
63+
DateString deleteDate = joinDate.plusMonth(month);
64+
65+
if (deleteDate.compareTo(todayString) <= 0) {
66+
privaciesToDelete.add(i + 1);
67+
}
68+
}
69+
70+
return privaciesToDelete.stream().mapToInt(it -> it).toArray();
71+
}
72+
73+
// -------------------------------------------------------------------------------------------------
74+
75+
private static LocalDate parseDate(String date) {
76+
String[] fragments = date.split("\\.");
77+
int year = Integer.parseInt(fragments[0]);
78+
int month = Integer.parseInt(fragments[1]);
79+
int day = Integer.parseInt(fragments[2]);
80+
81+
return LocalDate.of(year, month, day);
82+
}
83+
84+
private static class DateString implements Comparable<DateString> {
85+
private final int year;
86+
87+
private final int month;
88+
89+
private final int day;
90+
91+
public DateString(String date) {
92+
String[] fragments = date.split("\\.");
93+
this.year = Integer.parseInt(fragments[0]);
94+
this.month = Integer.parseInt(fragments[1]);
95+
this.day = Integer.parseInt(fragments[2]);
96+
}
97+
98+
private DateString(int year, int month, int day) {
99+
if (year < 0) throw new IllegalArgumentException("Invalid year: " + year);
100+
if (month < 1 || month > 12) throw new IllegalArgumentException("Invalid month: " + month);
101+
if (day < 1 || day > 28) throw new IllegalArgumentException("Invalid day: " + day);
102+
103+
this.year = year;
104+
this.month = month;
105+
this.day = day;
106+
}
107+
108+
public DateString plusMonth(int monthToAdd) {
109+
int month = (this.month + monthToAdd) % 12;
110+
if (month == 0) month = 12;
111+
112+
int year = this.year;
113+
if (this.month + monthToAdd > 12) {
114+
year += (this.month + monthToAdd) / 12;
115+
}
116+
117+
return new DateString(year, month, this.day);
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return String.format("%04d.%02d.%02d", year, month, day);
123+
}
124+
125+
@Override
126+
public int compareTo(DateString o) {
127+
return this.toString().compareTo(o.toString());
128+
}
129+
}
130+
131+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.imsejin.study.programmers
2+
3+
import spock.lang.Specification
4+
5+
class L150370Spec extends Specification {
6+
7+
def "test"() {
8+
when:
9+
def actual = L150370.solve0(today, terms as String[], privacies as String[])
10+
11+
then:
12+
actual == expected as int[]
13+
14+
where:
15+
today | terms | privacies || expected
16+
"2022.05.19" | ["A 6", "B 12", "C 3"] | ["2021.05.02 A", "2021.07.01 B", "2022.02.19 C", "2022.02.20 C"] || [1, 3]
17+
"2020.01.01" | ["Z 3", "D 5"] | ["2019.01.01 D", "2019.11.15 Z", "2019.08.02 D", "2019.07.01 D", "2018.12.28 Z"] || [1, 4, 5]
18+
"2020.12.28" | ["A 1"] | ["2020.12.01 A"] || []
19+
"2020.01.05" | ["B 13", "C 30"] | ["2018.12.06 B", "2018.12.01 B", "2017.06.05 C", "2017.07.06 C"] || [2, 3]
20+
}
21+
22+
}

0 commit comments

Comments
 (0)