Skip to content

Commit 612cdd2

Browse files
committed
issue #28 Grid Challenge w/ Explanation
1 parent 5839472 commit 612cdd2

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

src/hackerrank/GridChallenge.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@
33
import java.util.Arrays;
44

55
public class GridChallenge {
6+
67
static String gridChallenge(String[] grid) {
7-
char ll[] = new char[grid.length];
8-
char b[] = new char[grid.length];
9-
boolean xx=true;
10-
int i=0;
11-
while(xx && i<grid[0].length()){
12-
for(int j=0 ; j<grid.length;j++ ){
13-
ll[j]=grid[j].charAt(i);
14-
}
15-
16-
b= Arrays.copyOf(ll,grid.length);
17-
Arrays.sort(ll);
18-
19-
if(!(Arrays.equals(b,ll))){
20-
xx=false;
21-
break;
22-
}
23-
i++;
24-
}
8+
//그리드가 열이 오름차순 정렬이면 YES출력하는 문제
259

26-
if(xx){
27-
return "YES";
28-
}
29-
else{
30-
return "NO";
10+
char charArr[] = new char[grid.length];
11+
char copyCharArr[] = new char[grid.length];
12+
boolean isAscending = true;
13+
14+
for (int i = 0; i < grid.length; i++) {
15+
//주어진 grid를 char arr로 바꾼뒤 오름차순 정렬한다.
16+
char[] chars = grid[i].toCharArray();
17+
System.out.println("char arr: "+Arrays.toString(chars));
18+
Arrays.sort(chars);
19+
20+
//정렬된 char arr를 String arr로 만든다
21+
grid[i] = new String(chars);
22+
System.out.println("grid[i]: "+Arrays.toString(grid));
23+
24+
//compareTo
25+
if ( i != 0 && grid[i].compareTo(grid[i-1]) < 0 )
26+
System.out.println("compareTo: "+ (grid[i].compareTo(grid[i-1]) < 0) );
27+
return "NO";
3128
}
29+
return "YES";
3230
}
3331

3432
public static void main(String[] args) {
35-
System.out.println(gridChallenge(new String[]{"abc", "ade", "efg"})+", ans:YES");
36-
System.out.println(gridChallenge(new String[]{"ebacd", "fghij", "olmkn", "trpqs", "xywuv"})+", ans:YES");
33+
// System.out.println(gridChallenge(new String[]{"abc", "ade", "efg"}) + ", ans:YES");
34+
// System.out.println(gridChallenge(new String[]{"ebacd", "fghij", "olmkn", "trpqs", "xywuv"}) + ", ans:YES");
35+
System.out.println(gridChallenge(new String[]{"kc", "iu"}) + ", ans:YES");
36+
// System.out.println(gridChallenge(new String[]{"uxf", "vof", "hmp"}) + ", ans:NO");
37+
// System.out.println(gridChallenge(new String[]{"ppp", "ypp", "wyw"}) + ", ans:YES");
3738
}
3839
}

0 commit comments

Comments
 (0)