Skip to content

Commit c175bcf

Browse files
committed
23주차 알고리즘 완료
1 parent e7eadf1 commit c175bcf

3 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package programmers.level2.week_23;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* 영어 끝말잇기
9+
* https://programmers.co.kr/learn/courses/30/lessons/12981?language=java
10+
*/
11+
public class Solution001 {
12+
public static void main(String[] args) {
13+
Solution001 sol = new Solution001();
14+
int n = 3;
15+
String[] words = { "tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank" };
16+
System.out.println(Arrays.toString(sol.solution(n, words)));
17+
}
18+
19+
public int[] solution(int n, String[] words) {
20+
int[] answer = new int[2];
21+
List<String> wordList = new ArrayList<>();
22+
wordList.add(words[0]);
23+
String tmp = words[0];
24+
for (int i = 1; i < words.length; i++) {
25+
if (tmp.endsWith(words[i].charAt(0) + "") == false || wordList.contains(words[i])) {
26+
answer[0] = (i + 1) % n == 0 ? n : (i + 1) % n;
27+
answer[1] = (i / n) + 1;
28+
break;
29+
}
30+
wordList.add(words[i]);
31+
tmp = words[i];
32+
}
33+
34+
return answer;
35+
}
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package programmers.level2.week_23;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* [1차] 캐시
8+
* https://programmers.co.kr/learn/courses/30/lessons/17680?language=java
9+
*/
10+
public class Solution002 {
11+
public static void main(String[] args) {
12+
Solution002 sol = new Solution002();
13+
int cacheSize = 3;
14+
String[] cities = { "Jeju", "Pangyo", "Seoul", "NewYork", "LA", "Jeju", "Pangyo", "Seoul", "NewYork", "LA" };
15+
System.out.println(sol.solution(cacheSize, cities));
16+
}
17+
18+
public int solution(int cacheSize, String[] cities) {
19+
int answer = 0;
20+
Queue<String> queue = new LinkedList<>();
21+
22+
if (cacheSize == 0) {
23+
return cities.length * 5;
24+
}
25+
26+
for (int i = 0; i < cities.length; i++) {
27+
cities[i] = cities[i].toLowerCase();
28+
if (queue.contains(cities[i])) {
29+
answer++;
30+
queue.remove(cities[i]);
31+
queue.add(cities[i]);
32+
} else {
33+
answer += 5;
34+
if (queue.size() == cacheSize) {
35+
queue.poll();
36+
}
37+
queue.add(cities[i]);
38+
}
39+
}
40+
41+
return answer;
42+
}
43+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package programmers.level2.week_23;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* [3차] 파일명 정렬
11+
* https://programmers.co.kr/learn/courses/30/lessons/17686?language=java
12+
*/
13+
public class Solution003 {
14+
public static void main(String[] args) {
15+
Solution003 sol = new Solution003();
16+
String[] files = { "img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG" };
17+
System.out.println(Arrays.toString(sol.solution(files)));
18+
}
19+
20+
class FileObject implements Comparable<FileObject> {
21+
String head;
22+
int number;
23+
String tail;
24+
int origIdx;
25+
String origTxt;
26+
27+
public FileObject() {
28+
29+
}
30+
31+
public FileObject(String head, int number, String tail, int origIdX, String origTxt) {
32+
this.head = head;
33+
this.number = number;
34+
this.tail = tail;
35+
this.origIdx = origIdX;
36+
this.origTxt = origTxt;
37+
}
38+
39+
public String getHead() {
40+
return head;
41+
}
42+
43+
public void setHead(String head) {
44+
this.head = head;
45+
}
46+
47+
public int getNumber() {
48+
return number;
49+
}
50+
51+
public void setNumber(int number) {
52+
this.number = number;
53+
}
54+
55+
public String getTail() {
56+
return tail;
57+
}
58+
59+
public void setTail(String tail) {
60+
this.tail = tail;
61+
}
62+
63+
public int getOrigIdx() {
64+
return origIdx;
65+
}
66+
67+
public void setOrigIdx(int origIdx) {
68+
this.origIdx = origIdx;
69+
}
70+
71+
public String getOrigTxt() {
72+
return origTxt;
73+
}
74+
75+
public void setOrigTxt(String origTxt) {
76+
this.origTxt = origTxt;
77+
}
78+
79+
@Override
80+
public int compareTo(FileObject o) {
81+
int comH = o.getHead().compareTo(getHead());
82+
boolean comN = o.getNumber() < getNumber();
83+
boolean comO = o.getOrigIdx() < getOrigIdx();
84+
if (comH < 0) {
85+
return 1;
86+
} else {
87+
if (comH == 0) {
88+
if (comN == true) {
89+
return 1;
90+
} else {
91+
if (comH == 0 && o.getNumber() == getNumber() && comO == true) {
92+
return 1;
93+
}
94+
}
95+
}
96+
}
97+
return -1;
98+
}
99+
}
100+
101+
public String[] solution(String[] files) {
102+
String[] answer = new String[files.length];
103+
List<FileObject> list = new ArrayList<>();
104+
105+
for (int k = 0; k < files.length; k++) {
106+
String f = files[k].toLowerCase();
107+
String head = "";
108+
int number = 0;
109+
String tail = "";
110+
for (int i = 0; i < f.length(); i++) {
111+
if (Pattern.matches("[0-9]", f.charAt(i) + "")) {
112+
head = f.substring(0, i);
113+
int stopIdx = 0;
114+
for (int j = i + 1; j < f.length(); j++) {
115+
if (Pattern.matches("[0-9]", f.charAt(j) + "") == false) {
116+
stopIdx = j;
117+
break;
118+
}
119+
}
120+
121+
if (stopIdx == 0) {
122+
number = Integer.parseInt(f.substring(i));
123+
} else {
124+
number = Integer.parseInt(f.substring(i, stopIdx));
125+
tail = f.substring(stopIdx);
126+
}
127+
128+
list.add(new FileObject(head, number, tail, k, files[k]));
129+
break;
130+
}
131+
}
132+
}
133+
134+
Collections.sort(list);
135+
136+
for (int i = 0; i < list.size(); i++) {
137+
answer[i] = list.get(i).getOrigTxt();
138+
}
139+
140+
return answer;
141+
}
142+
}

0 commit comments

Comments
 (0)