📍 내가 살게, 아냐 내가 살게

https://www.acmicpc.net/problem/18229

 

 

 

✨ 방법1 ✨

첫 번째 테스트케이스

 

 

 

N은 사람 수(행), M은 반복 횟수(열)이다.

각 사람의 누적금액이 K보다 커졌을 때 사람 번호랑 반복 횟수를 출력하면 된다.

 

1. 이차원 배열 array에 입력값을 받는다.

2. 일차원 배열 people에 각 사람의 누적금액을 넣는다.

3. 누적금액이 K보다 커졌을 때, 반복문을 빠져나가고 출력한다.

 

 

 

✨ 전체코드 ✨

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(sc.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        // 1. 이차원 배열 array에 입력값을 받는다.
        int[][] array = new int[N][M];
        for(int i=0; i<N; i++){
            st = new StringTokenizer(sc.readLine());
            for(int j=0; j<M; j++){
                array[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        // 2. 일차원 배열 people에 각 사람의 누적금액을 넣는다.
        int[] people = new int[N+1];
        L : for(int j=0; j<M; j++){
            for(int i=0; i<N; i++){
                people[i+1] += array[i][j];
                
                // 3. 누적금액이 K보다 커졌을 때, 반복문을 빠져나가고 출력한다.
                if(people[i+1] >= K) {
                    sb.append(i+1).append(" ").append(j+1);
                    break L;
                }
            }
        } System.out.println(sb);
    }
}

 

 

 

🌀 성능

  • 메모리 : 16,648 KB
  • 시간 : 152 ms

 

 

 

🌟 방법 2 🌟

첫 번째 테스트케이스



 

1. 처음 입력 받을 때부터 누적합으로 입력 받는다.

2. 반복문을 돌며 누적금액이 K보다 커졌을 때 사람 번호랑 반복 횟수를 출력한다.

 

 

 

🌟 전체코드 🌟

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(sc.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());

        int[][] array = new int[N][M];
        for(int i=0; i<N; i++) {
            st = new StringTokenizer(sc.readLine());

            for (int j = 0; j < M; j++) {
                int num = Integer.parseInt(st.nextToken());

                if (j == 0) array[i][j] = num;
                else array[i][j] = array[i][j-1] + num;
            }
        }

        L : for(int j=0; j<M; j++) {
            for (int i = 0; i < N; i++) {
                if (array[i][j] >= K) {
                    sb.append(i + 1).append(" ").append(j + 1);
                    break L;
                }
            }
        } System.out.println(sb);
    }
}

 

 

 

🌀 성능

  • 메모리 : 16,220 KB
  • 시간 : 176 ms

 

 

 


예제 설명 때문에 오히려 더 헷갈렸다 ㅡㅡ