[랜덤 마라톤] 점수 계산(25710)

lhs's avatar
Dec 07, 2024
[랜덤 마라톤] 점수 계산(25710)
notion image
notion image

1. 문제 풀이 아이디어

  • 중복되는 경우는 최대 2번까지만 허용하도록 배열을 만들어 점수를 계산하면 문제를 해결할 수 있다.

2. 나의 정답 코드

public class Main { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine()); boolean[] muls = new boolean[1000000]; int[] dup = new int[1000]; List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { int a = Integer.parseInt(stringTokenizer.nextToken()); if (dup[a] == 2) continue; dup[a]++; list.add(a); } int result = 0; for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { int mul = list.get(i) * list.get(j); if (muls[mul]) { continue; } muls[mul] = true; int sum = 0; while (mul > 0) { sum += mul % 10; mul /= 10; } result = Math.max(result, sum); } } System.out.println(result); bufferedReader.close(); } }

3. 정리

  • 입력받은 값을 중복 횟수가 2번 이상이면 제외하고, 그렇지 않으면 list에 저장한다.
  • 이중 for문을 사용하여 두 숫자를 곱한 값을 구하고, while문을 통해 그 값의 자릿수를 모두 더한다.
  • 자릿수의 합 중 최대값을 구해 문제를 해결한다.
  • muls 배열을 사용하여 중복된 곱셈 결과를 효율적으로 처리한다.

4. 느낀 점

notion image
  • muls 배열을 사용하지 않았을 때는 속도도 느리고 메모리 사용량도 더 컸다.
  • muls 배열이 차지하는 공간보다 중복으로 인한 연산 및 메모리 사용량이 더 크므로, 효율적인 자료구조를 사용해야겠다고 느꼈다.
Share article

LHS's Study Space