[Java] SWEA Flatten & 회문1

Lee GaEun·2025년 5월 1일

[Java] 알고리즘

목록 보기
70/93

1208 Flatten 문제 링크

#1

30분


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;

/*
   사용하는 클래스명이 Solution 이어야 하므로, 가급적 Solution.java 를 사용할 것을 권장합니다.
   이러한 상황에서도 동일하게 java Solution 명령으로 프로그램을 수행해볼 수 있습니다.
 */
class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            int[] arr = new int[101];
            for(int i=0; i<100; i++) {
                arr[sc.nextInt()]++;
            }

            int start = 0;
            int end = 100;
            while (N>=0) {
                if(arr[start] != 0) {
                    if(arr[end] != 0) {
                        arr[start+1]++;
                        arr[start]--;
                        arr[end-1]++;
                        arr[end]--;
                        N--;
                    }
                    else end--;
                }
                else start++;
            }

            System.out.println("#"+test_case+" "+(end-start));
        }
    }
}

  • 높낮이가 다 다른 계단을 그대로 배열로 만들면 답이 없을 것 같았음

  • List에서 sort()는 중복 허용 정렬이라 이걸로 해도 될 거 같긴한데..
  • 그럼 값을 올리거나 내릴 때마다 sort()를 해줘야 해서 오래 걸림

  • 계단 크기를 위치값으로 넣어서 해당 계단 크기인 값의 개수로 배열 생성
  • 투포인터로 start랑 end 위치의 값을 하나씩 올리고 내리면서 계단을 옮김
  • 시간 복잡도 O(N)으로 완성

  • 와 근데 다른 벨로그 보니까 다들 sort()로 풀었네...
  • 시간초과 안 걸리는 듯,,

1215 회문1 문제 링크

#1


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;

/*
   사용하는 클래스명이 Solution 이어야 하므로, 가급적 Solution.java 를 사용할 것을 권장합니다.
   이러한 상황에서도 동일하게 java Solution 명령으로 프로그램을 수행해볼 수 있습니다.
 */
class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            String a;
            char[][] arr = new char[8][8];
            for(int i=0; i<8; i++) {
                a = sc.next();
                for(int j=0; j<8; j++) {
                    arr[i][j] = a.charAt(j);
                }
            }

            int answer = 0;
            boolean cca = true;
            boolean ccb = true;
            for (int i = 0; i < 8; i++) {
                for(int j=0; j<=8-N; j++) {
                    for(int x=0; x<N/2; x++) {
                        if (arr[i][j+x] != arr[i][j+N-1-x]) {
                            cca = false;
                        }

                        if(arr[j+x][i] != arr[j+N-1-x][i]) {
                            ccb = false;
                        }
                    }
                    if(cca) answer++;
                    if(ccb) answer++;
                    cca = true;
                    ccb = true;
                }
            }

            System.out.println("#"+test_case+" "+answer);
        }
    }
}

  • 아니 진짜 바보인게 다른 문제 output 값이랑 비교하면서 대체 어디가 틀린 건지 고민하고 있었음;;;;

  • 뭔가 예전에 풀었던 것보다 더 어려운 방법으로 푼 것 같긴 한데
  • 기본적인 로직은 똑같음
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글