ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] 20546 기적의 매매법 - Java
    알고리즘 문제 풀이 2024. 4. 11. 23:42

    문제 - 기적의 매매법

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

     

     

    문제 설명

     

    접근방법

    BNP 

    1. 남아 있는 돈이 현재 가격보다 많을때 무조건 산다

    2. 없으면 못산다

    3. 팔지도 않는다

     

    TIMING : 각 날마다 어제 가격과 비교해서 상승, 하락 카운트를 변수로 선언하여 카운트를 올리거나 초기화 해주는 방식

    1. 상승일때 -> 상승 카운트 up, 하락 카운트 초기화

    2. 하락일때 -> 하락 카운트 up, 상승 카운트 초기화

    3. 상승 카운트가 3 이상이 되었을때 전에 샀었던 주식을 현재 가격을 판다 (현재 남아있는돈 + (현재시세 * 주식 양))

    4. 하락 카운트가 3 이상이 되었을때 현재 남아있는 돈으로 산다

     

     

    코드 작성

    1. 시간 제한은 1초라 O(n)일 것이다 여기에서 반복문을 한번만 사용하므로 O(n)이 될것이다 .

    2. 입력받을때 대부분 Scanner를 사용할 것이다 하지만 나는 입력받을때 시간을 조금이라도 줄이기 위해 BufferedReader와 StringTokenizer를 병행하여 사용했다.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.StringTokenizer;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int money = Integer.parseInt(br.readLine());
            StringTokenizer st = new StringTokenizer(br.readLine());
    
            List<Integer> amountList = new ArrayList<>();
            Iterator<Object> iterator = st.asIterator();
            while (iterator.hasNext()) {
                amountList.add(Integer.parseInt((String) iterator.next()));
            }
    
            int BNP = BNP(money, amountList);
            int TIMING = TIMING(money, amountList);
    
            if (BNP > TIMING) {
                System.out.println("BNP");
            } else if (BNP < TIMING) {
                System.out.println("TIMING");
            } else {
                System.out.println("SAMESAME");
            }
        }
    
    
        public static int BNP(int money, List<Integer> amountList) {
            int totalCount = 0;
            for (Integer amount : amountList) {
                if (amount <= money) {
                    int count = (money / amount);
                    money -= (count * amount);
                    totalCount += count;
                }
            }
    
            return totalCount * amountList.get(amountList.size() - 1) + money;
        }
    
        public static int TIMING(int money, List<Integer> amountList) {
            int increaseCount = 0;
            int decreaseCount = 0;
            int yesterday = amountList.get(0);
            int buyCount = 0;
    
            for (int i = 1; i < amountList.size(); i++) {
                int today = amountList.get(i);
    
                if (today > yesterday) {
                    increaseCount++;
                    decreaseCount = 0;
                } else if (today < yesterday) {
                    decreaseCount++;
                    increaseCount = 0;
                } else {
                    increaseCount = 0;
                    decreaseCount = 0;
                }
    
                if (increaseCount >= 3) {
                    if (buyCount != 0) {
                        money += (today * buyCount);
                    }
                    increaseCount = 0;
                    buyCount = 0;
                } else if (decreaseCount >= 3) {
                    int nowCount = money / today;
                    buyCount += nowCount;
                    money -= (nowCount * today);
                }
                yesterday = today;
            }
    
            return money + (buyCount * amountList.get(amountList.size() - 1));
        }
    }

     

    풀고 느낀점

    1. 일단 메서드와 변수의 명명 규칙을 위반해서 코드가 마음에 안들긴 하지만 어쩔수 없었다.

    2. 그 날짜와 그 날짜에 해당하는 가격을 필드로 가지고 있는 객체를 만들어서 객체지향적으로 했으면 어떨까라는 생각을 해보았지만 여유가 된다면 해볼 생각이다.

    3. 카운트 변수들을 언제 초기화를 해주는게 관건인 것 같다.

Designed by Tistory.