https://school.programmers.co.kr/learn/courses/30/lessons/120876
문제는 위와 같다.
class Solution {
public int solution(int[][] lines) {
int[] firstLine = new int[2];
int[] secondLine = new int[2];
int[] thirdLine = new int[2];
if (lines[0][0] < lines[1][0]) {
if (lines[0][1] > lines[1][0]) {
firstLine[0] = lines[1][0];
firstLine[1] = Math.min(lines[0][1], lines[1][1]);
}
} else if (lines[0][0] > lines[1][0]) {
if (lines[1][1] > lines[0][0]) {
firstLine[0] = lines[0][0];
firstLine[1] = Math.min(lines[0][1], lines[1][1]);
}
} else {
firstLine[0] = lines[0][0];
firstLine[1] = Math.min(lines[0][1], lines[1][1]);
}
if (lines[0][0] < lines[2][0]) {
if (lines[0][1] > lines[2][0]) {
secondLine[0] = lines[2][0];
secondLine[1] = Math.min(lines[0][1], lines[2][1]);
}
} else if (lines[0][0] > lines[2][0]) {
if (lines[2][1] > lines[0][0]) {
secondLine[0] = lines[0][0];
secondLine[1] = Math.min(lines[0][1], lines[2][1]);
}
} else {
secondLine[0] = lines[0][0];
secondLine[1] = Math.min(lines[0][1], lines[2][1]);
}
if (lines[1][0] < lines[2][0]) {
if (lines[1][1] > lines[2][0]) {
thirdLine[0] = lines[2][0];
thirdLine[1] = Math.min(lines[1][1], lines[2][1]);
}
} else if (lines[1][0] > lines[2][0]) {
if (lines[2][1] > lines[1][0]) {
thirdLine[0] = lines[1][0];
thirdLine[1] = Math.min(lines[1][1], lines[2][1]);
}
} else {
thirdLine[0] = lines[1][0];
thirdLine[1] = Math.min(lines[1][1], lines[2][1]);
}
int sum = firstLine[1] - firstLine[0] + secondLine[1] - secondLine[0] + thirdLine[1] - thirdLine[0];
if (firstLine[1] - firstLine[0] != 0 && secondLine[1] - secondLine[0] != 0) {
if (firstLine[0] < secondLine[0]) {
if (firstLine[1] > secondLine[0]) {
sum -= Math.min(firstLine[1], secondLine[1]) - secondLine[0];
}
} else if (firstLine[0] > secondLine[0]) {
if (secondLine[1] > firstLine[0]) {
sum -= Math.min(firstLine[1], secondLine[1]) - firstLine[0];
}
} else {
sum -= Math.min(firstLine[1], secondLine[1]) - firstLine[0];
}
}
if (firstLine[1] - firstLine[0] != 0 && thirdLine[1] - thirdLine[0] != 0) {
if (firstLine[0] < thirdLine[0]) {
if (firstLine[1] > thirdLine[0]) {
sum -= Math.min(firstLine[1], thirdLine[1]) - thirdLine[0];
}
} else if (firstLine[0] > thirdLine[0]) {
if (thirdLine[1] > firstLine[0]) {
sum -= Math.min(firstLine[1], thirdLine[1]) - firstLine[0];
}
} else {
sum -= Math.min(firstLine[1], thirdLine[1]) - firstLine[0];
}
}
int[] temp = new int[2];
if (thirdLine[1] - thirdLine[0] != 0 && secondLine[1] - secondLine[0] != 0) {
if (thirdLine[0] < secondLine[0]) {
if (thirdLine[1] > secondLine[0]) {
sum -= Math.min(thirdLine[1], secondLine[1]) - secondLine[0];
temp[0] = secondLine[0];
temp[1] = Math.min(thirdLine[1], secondLine[1]);
}
} else if (thirdLine[0] > secondLine[0]) {
if (secondLine[1] > thirdLine[0]) {
sum -= Math.min(thirdLine[1], secondLine[1]) - thirdLine[0];
temp[0] = thirdLine[0];
temp[1] = Math.min(thirdLine[1], secondLine[1]);
}
} else {
sum -= Math.min(thirdLine[1], secondLine[1]) - thirdLine[0];
temp[0] = thirdLine[0];
temp[1] = Math.min(thirdLine[1], secondLine[1]);
}
}
if (firstLine[1] - firstLine[0] != 0 && temp[1] - temp[0] != 0) {
if (firstLine[0] < temp[0]) {
if (firstLine[1] > temp[0]) {
sum -= Math.min(firstLine[1], temp[1]) - temp[0];
}
} else if (thirdLine[0] > temp[0]) {
if (temp[1] > firstLine[0]) {
sum -= Math.min(firstLine[1], temp[1]) - firstLine[0];
}
} else {
sum += Math.min(firstLine[1], temp[1]) - firstLine[0];
}
}
return sum;
}
}
import java.util.HashMap;
import java.util.Map;
class Solution {
public int solution(int[][] lines) {
Map<Integer, Integer> map = new HashMap<>();
for (int i=0; i<lines.length; i++) {
int min = Math.min(lines[i][0], lines[i][1]);
int max = Math.max(lines[i][0], lines[i][1]);
for (int j=min; j<max; j++) {
map.put(j, map.getOrDefault(j, 0) + 1);
}
}
int answer = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() >= 2) {
answer++;
}
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int[][] lines) {
Map<Integer, Integer> map = new HashMap<>();
for (int[] line : lines) {
int from = Math.min(line[0], line[1]);
int to = Math.max(line[0], line[1]);
for (int i = from; i < to; i++) {
map.merge(i, 1, Integer::sum);
}
}
return (int) map.values().stream().filter(i -> i > 1).count();
}
}
ㅋㅋㅋ 내가 봐도 내 코드가 어이가 없다. 솔직히 어디서 하나 오류가 났어도 이상하지 않다.
그래도 많은 것을 얻어갈 수 있는 문제였다. "겹치다"의 개념을 다음부터는 좀 더 생각해 볼 수 있을 것 같다.