카테고리 없음

4월 18일 화요일 TIL 회고록

tft4rollz 2023. 4. 18. 22:30

갈수록 어려워지는 코테.. 1단계도 너무 어렵다 ㅠㅠ


두 개 뽑아서 더하기


문제 설명, 제한사항, 입출력 예

풀이

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

class Solution {
    public List<Integer> solution(int[] numbers) {
    // 값을 넣을 list를 만든다.
      List<Integer> list = new ArrayList<>();
      
      // 반복문을 사용해서 numbers[i]번째와 numbers[j]번째를 더한 값을 list에 넣는다.
      for (int i = 0 ; i < numbers.length; i++) {
        for (int j = i+1 ; j < numbers.length; j++) {
            list.add(numbers[i]+numbers[j]);
        }
        // Collections.sort를 사용하여 list에 있는 값을 오름차순으로 정렬한다.
        Collections.sort(list);
      }
      // stream().distinct().collect(Collectors.toList())를 사용하여 list의 중복 값을 제거한 후 리턴시킨다.
      return list.stream().distinct().collect(Collectors.toList());
    }
  }

중복을 제거하는게 되게 머리가 어지러웠는데.. 블로그를 보고 참고했다.

처음에는 HastSet을 사용했었는데 이건 오름차순으로 만드는 법을 몰라서 list + stream을 사용했다.

https://hianna.tistory.com/582

 

[Java] List 중복 제거하는 2가지 방법

Java에서 List는 중복된 데이터를 가질 수 있습니다. List에 중복된 데이터가 있을 경우, 중복된 데이터를 제거하는 2가지 방법을 소개합니다. Set 이용하기 Stream 이용하기 - Java 8 이상 1. Set 이용하기

hianna.tistory.com


모의고사


문제 설명, 제한조건, 입출력 예

풀이

import java.util.ArrayList;
import java.util.List;

  class Solution {
    public List<Integer> solution(int[] answers) {
      // 리턴 값을 담을 list2를 만들었다.
      List<Integer> list2 = new ArrayList<>();
      // answers의 값을 담을 list를 만들었다.
      List<Integer> list = new ArrayList<>();
      // 수포자1을 카운트하는 supoja1Count1을 선언했다.
      int supoja1Count1 = 0;
      // 수포자2을 카운트하는 supoja1Count2을 선언했다.
      int supoja1Count2 = 0;
       // 수포자3을 카운트하는 supoja1Count3을 선언했다.
      int supoja1Count3 = 0;
      int[] supoja1 = {1,2,3,4,5};
      int[] supoja2 = {2,1,2,3,2,4,2,5};
      int[] supoja3 = {3,3,1,1,2,2,4,4,5,5};
      
      // for문을 사용해 answers의 값을 list에 넣어준다.
      for (int i = 0; i < answers.length; i++) {
        list.add(answers[i]);
      }
      // for문을 사용해서 list.get(i)가 supoja1[i%5]의 값과 같으면 카운트를 1 증가시킨다.
      for (int i = 0; i < answers.length; i++ ) {
        if (list.get(i).equals(supoja1[i%5])) {
          supoja1Count1++;
        }
      }
      // for문을 사용해서 list.get(i)가 supoja1[i%8]의 값과 같으면 카운트를 1 증가시킨다.
      for (int i = 0; i < answers.length; i++ ) {
       if (list.get(i).equals(supoja2[i%8])) {
          supoja1Count2++;
        }
        }
      // for문을 사용해서 list.get(i)가 supoja1[i%10]의 값과 같으면 카운트를 1 증가시킨다.
      for (int i = 0; i < answers.length; i++ ) {
        if (list.get(i).equals(supoja3[i%10])) {
          supoja1Count3++;
        }
    }
    
    // if문을 사용해 list2의 값을 넣어준 후 list2를 리턴시켰다.     
    if (supoja1Count1 > supoja1Count2 && supoja1Count1 > supoja1Count3) {
        list2.add(1);
      } else if (supoja1Count2 > supoja1Count1 && supoja1Count2 > supoja1Count3) {
        list2.add(2);
      } else if (supoja1Count3 > supoja1Count1 && supoja1Count3 > supoja1Count2) {
        list2.add(3);
      } else if (supoja1Count1 == supoja1Count2 && supoja1Count2 == supoja1Count3) {
        list2.add(1);
        list2.add(2);
        list2.add(3);
      } else if (supoja1Count1 == supoja1Count2 && supoja1Count2 != supoja1Count3) {
        list2.add(1);
        list2.add(2);
      } else if (supoja1Count2 == supoja1Count3 && supoja1Count1 != supoja1Count3) {
        list2.add(2);
        list2.add(3);
      } else if (supoja1Count1 == supoja1Count3 && supoja1Count1 != supoja1Count2) {
        list2.add(1);
        list2.add(3);
      }
      return list2;
    }
    
  }

문제가 너무 어려워서 두시간정도 소요했다.. 코드도 너무 지저분하고 리팩토링을 해야할 것 같다.

처음에 테스트 2,3,4 제외하고 전부 런타임에러가 나와 검색해봤더니 answers는 최대 10000개이기에 수포자 1/2/3의 배열 인덱스는 i값이 아니라 i % 배열크기로 나누어주어야 한다고 하길래 나누어줬더니 런타임에러가 나오지 않았다.

내일 한번 리팩토링 해봐야겠다. for문이 너무 많고 if문이 너무너무너무 지저분하다 ㅠㅠ..