강의로 돌아가기
youggi

질문하기에 올라와있는 테스트 케이스는 전부 통과하는데 45 79 문항이 통과가 안되네요..

기준점을 기준으로 양옆으로 A가 아닌 값을 찾고 해당 왼쪽 오른쪽 값을 변환 시키기 위해 필요한 이동값+변환값 비교해서 더 작은 쪽으로 이동해서 기준을 이동합니다. 이동한 기준의 값은 A로 변경해줍니다. 또한 이동값+변환값이 양쪽다 동일할 경우 더욱 더 탐색하여 A가 새로 나올때 까지의 값들을 총 더해 비교하여 값이 더적은 쪽의 기준으로 이동하는 코드입니다.
어떤 것이 잘못된건가요..ㅠ
실행한 테스트 케이스는
"ZAAAZAAAAAA", 6
"NNAAAAANNN", 70
ABAAAAAAABA": 6
"AAB": 2
"AABAAAAAAABBB": 12
"BBBBAAAAAB": 10
"ZZZ": 5
"AAABAAAA" : 4
입니다.

작성중인 코드―Solution.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.util.*;

class Solution {
    public int solution(String name) {
        ArrayList<Integer> new_dist = new ArrayList<Integer>();
        Map<Character,Integer> map=new HashMap<>();

        map.put('A',0);
        map.put('B',1);
        map.put('C',2);
        map.put('D',3);
        map.put('E',4);
        map.put('F',5);
        map.put('G',6);
        map.put('H',7);
        map.put('I',8);
        map.put('J',9);
        map.put('K',10);
        map.put('L',11);
        map.put('M',12);
        map.put('N',13);
        map.put('O',12);
        map.put('P',11);
        map.put('Q',10);
        map.put('R',9);
        map.put('S',8);
        map.put('T',7);
        map.put('U',6);
        map.put('V',5);
        map.put('W',4);
        map.put('X',3);
        map.put('Y',2);
        map.put('Z',1);

        char[] arr_str = name.toCharArray(); //name 배열을 한글자씩 나눠서 배열에 할당

        for(int i = 0; i<arr_str.length; i++){
            new_dist.add(map.get(arr_str[i]));
        } // name 배열을 사전에 대입하여 맞는 value값을 arraylist에 할당

        int[] arr_dist = new int[arr_str.length];//arraylist를 배열에 할당

        System.out.println(new_dist.size());
        for(int j = 0; j < new_dist.size(); j++){
            arr_dist[j] = new_dist.get(j);
        }
        int answer2 = 0;
        answer2 = arr_dist[0]; //배열 0번쨰 값 추가
        arr_dist[0] = 0;
        int right_stand = 0;
        int left_stand = 0;

        int l_count = 0; //왼쪽으로 이동하는 값
        int r_count = 0; //오른쪽으로 이동하는 값
        int l_value = -1; //좌 이동 값 + 상하 이동값
        int r_value = -1; //우 이동 값 + 상하 이동값

        int l_value2 = 0; //l_value와 r_value 값이 동일할 때 추가로 받을 좌측 값
        int r_value2 = 0; //l_value와 r_value 값이 동일할 때 추가로 받을 우측 값
        int l_count2 = 0;
        int r_count2 = 0;
        int l_stand2 = 0;
        int r_stand2 = 0;
        int l_switch = 0; // 2차 좌측 이동을 멈출 스위치 변수
        int r_switch = 0; // 2차 우측 이동을 멈출 스위치변수
        int sum = 0;
        int check_count = 0;//몇번 실행됬는지 확인 변수
        while(true){
            check_count++;
            for(int sum_count = 0; sum_count< arr_dist.length; sum_count++){
                sum += arr_dist[sum_count];
            } //모든 배열이 0(A)인지 확인
            if(sum == 0){
                return answer2;
            }else{
                sum = 0;
            }

            if(r_value == -1){
                right_stand++;
                r_count++;
            } //r_value값이 아직 할당이 안되어있으면 계속 탐색

            if(l_value == -1){
                left_stand--;
                l_count++;
            }//l_value값이 아직 할당이 안되어있으면 계속 탐색

            if(left_stand == -1){
                left_stand = arr_dist.length - 1;
            }//인덱스가 -1일일때 조정
            if(right_stand == arr_dist.length){
                right_stand = 0;
            }//인덱스가 배열의 길이일때 조정

            if(arr_dist[right_stand] != 0){
                r_value = r_count + arr_dist[right_stand];
            }//A가 아닐떄 값 할당

            if(arr_dist[left_stand] != 0){
                l_value = l_count + arr_dist[left_stand];
            }//A가 아닐떄 값 할당

            if(r_value != -1 && l_value != -1){
                if(r_value < l_value){//비교해서 더 작은 값에 기준을 이동
                    left_stand = right_stand;
                    answer2 += r_value;
                    arr_dist[right_stand] = 0;
                }else if(r_value > l_value) {
                    right_stand = left_stand;
                    answer2 += l_value;
                    arr_dist[left_stand] = 0;
                }else{//값이 동일할떄 더욱 탐색을 진행하여 A가 나올떄까지의 좌우 이동,상하 이동값 확인하는 과정

                    for(int find_count = 0; find_count<arr_dist.length; find_count++){

                        if(find_count == 0){
                         l_stand2 = left_stand - 1;
                         r_stand2 = right_stand + 1;   
                         l_count2++;
                         r_count2++;  
                        }else{
                            if(l_switch == 0){
                             l_stand2--;
                             l_count2++;
                            }
                            if(r_switch == 0){
                             r_stand2++;
                             r_count2++;
                            }
                        }
                        if(l_stand2 == -1){
                            l_stand2 = arr_dist.length-1;
                        }
                        if(r_stand2 == arr_dist.length){
                            r_stand2 = 0;
                        }
                        if(arr_dist[l_stand2] == 0){
                            l_value2 += arr_dist[l_stand2] + l_count2;
                            l_switch = 1;
                        }else{
                            l_value2 += arr_dist[l_stand2];
                        }
                        if(arr_dist[r_stand2] == 0){
                            r_value2 += arr_dist[r_stand2] + r_count2;
                            r_switch = 1;
                        }else{
                            r_value2 += arr_dist[r_stand2];
                        }

                        if((r_switch + l_switch) == 2){
                            System.out.println(check_count + " " +r_value2 + " "+ l_value2);
                            if(r_value2 < l_value2){
                                left_stand = right_stand;
                                answer2 += r_value;
                                arr_dist[right_stand] = 0;
                            }else{
                                right_stand = left_stand;
                                answer2 += l_value;
                                arr_dist[left_stand] = 0;
                            }
                        l_value2 = 0;
                        r_value2 = 0;
                        l_count2 = 0;
                        r_count2 = 0;
                        l_stand2 = 0;
                        r_stand2 = 0;
                        l_switch = 0;
                        r_switch = 0;
                        break;
                        }

                    }
                }
                l_count = 0;
                r_count = 0;
                l_value = -1;
                r_value = -1;

            }

        }

    }
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.