728x90
실패원인
int형을 long값으로 바꾸어야함
maxX , minX, maxY, minY를 Long.MAX_VALUE, Long.MIN_VALUE로 초기화해야함
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import java.util.HashSet; import java.util.Arrays; class Solution { static long minx= Long.MAX_VALUE, miny= Long.MAX_VALUE; static long maxx= Long.MIN_VALUE, maxy= Long.MIN_VALUE; public String[] solution(int[][] line) { String[] answer = {}; HashSet<Point> pSet= new HashSet<>(); //x= (bf-ed)/(ad-bc) //y= (ec-af)/(ad-bc) long x, y; for(int i=0; i<line.length-1; i++){ long a= line[i][0]; long b= line[i][1]; long e= line[i][2]; for(int j=i+1; j<line.length; j++){ long c= line[j][0]; long d= line[j][1]; long f= line[j][2]; long adbc= a*d-b*c; if(adbc==0) continue; //비교대상 직선과 평행함 long bfed= b*f-e*d; if(bfed%adbc!=0) continue; //x가 정수가 아님 long ecaf= e*c-a*f; if(ecaf%adbc!=0) continue; //y가 정수가 아님 x= bfed/adbc; y= ecaf/adbc; pSet.add(new Point(x, y)); minx= Math.min(minx, x); miny= Math.min(miny, y); maxx= Math.max(maxx, x); maxy= Math.max(maxy, y); } } long height= maxy-miny+1; long width= maxx-minx+1; answer= new String[(int)height]; StringBuilder sb= new StringBuilder(); for(int i=0; i<width; i++){ sb.append("."); } Arrays.fill(answer, sb.toString()); long nx, ny; for(Point p: pSet){ ny= maxy-p.y; nx= p.x-minx; answer[(int)ny]= answer[(int)ny].substring(0, (int)nx)+"*" +answer[(int)ny].substring((int)nx+1); } return answer; } public class Point{ long x; long y; public Point(long x, long y){ this.x= x; this.y= y; } } } | cs |
실패코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import java.util.*;
class Solution {
public String[] solution(int[][] line) {
Set<Point> set = new HashSet<>();
int minX = 1001;
int maxX = -1001;
int minY = 1001;
int maxY = -1001;
for(int i=0; i<line.length; i++){
for(int j=i+1; j<line.length; j++){
int A = line[i][0];
int B = line[i][1];
int E = line[i][2];
int C = line[j][0];
int D = line[j][1];
int F = line[j][2];
int ADBC = A*D-B*C;
if(ADBC==0) continue;
int BFED = B*F-E*D;
if(BFED%ADBC!=0) continue;
int ECAF = E*C-A*F;
if(ECAF%ADBC!=0) continue;
int x = BFED/ADBC;
int y = ECAF/ADBC;
set.add(new Point(x,y));
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
int height = maxY-minY+1;
int width = maxX-minX+1;
// System.out.println(height+" "+width);
char[][] charArr = new char[height][width];
for(char[] cArr : charArr){
Arrays.fill(cArr,'.');
}
for(Point p : set){
int r = -p.y+maxY;
int c = p.x-minX;
// System.out.println(p.x+" "+p.y);
// System.out.println(r+" "+c);
// System.out.println();
charArr[r][c]='*';
}
String[] answer = new String[charArr.length];
for(int i=0; i<charArr.length; i++){
answer[i] = new String(charArr[i]);
}
return answer;
}
public class Point{
int x;
int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
}
|
cs |
메모리 초과남 2차원 char배열로 하면 안되는듯
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import java.util.*;
class Solution {
public String[] solution(int[][] line) {
Set<Point> set = new HashSet<>();
int minX = 1001;
int maxX = -1001;
int minY = 1001;
int maxY = -1001;
for(int i=0; i<line.length; i++){
for(int j=i+1; j<line.length; j++){
int A = line[i][0];
int B = line[i][1];
int E = line[i][2];
int C = line[j][0];
int D = line[j][1];
int F = line[j][2];
int ADBC = A*D-B*C;
if(ADBC==0) continue;
int BFED = B*F-E*D;
if(BFED%ADBC!=0) continue;
int ECAF = E*C-A*F;
if(ECAF%ADBC!=0) continue;
int x = BFED/ADBC;
int y = ECAF/ADBC;
set.add(new Point(x,y));
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
int height = maxY-minY+1;
int width = maxX-minX+1;
// System.out.println(height+" "+width);
String[] answer = new String[height];
StringBuilder sb = new StringBuilder();
for(int i=0; i<width; i++){
sb.append('.');
}
for(int i=0; i<height; i++){
answer[i] = new String(sb);
}
for(Point p : set){
int r = -p.y+maxY;
int c = p.x-minX;
answer[r] = answer[r].substring(0, c) + "*" + answer[r].substring(c+1,width);
}
return answer;
}
public class Point{
int x;
int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
}
|
cs |
또 초과
'알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][JAVA] 옹알이 (String) (0) | 2022.10.31 |
---|---|
[프로그래머스][JAVA] 전력망을 둘로 나누기 (그래프) (0) | 2022.04.11 |
[프로그래머스][JAVA] 구명보트 (그리디) (0) | 2022.04.03 |
[프로그래머스][JAVA] 주식가격 (완전탐색) (0) | 2022.04.03 |
[프로그래머스][JAVA] 영어 끝말잇기 (HashSet) (0) | 2022.04.03 |