Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

이태호 / 10월 4주차 / 목 #304

Merged
merged 2 commits into from
Oct 30, 2023
Merged

Conversation

leetaggg
Copy link
Member

@leetaggg leetaggg commented Oct 27, 2023


🎈boj 2467 - 용액


🗨 해결방법 :


투포인터를 사용하여 구현하였습니다.

📝메모 :


코드를 너무 지저분하게 쓴 것 같다. 리팩토링을 하고 좀 더 최적화를 해야겠다.

✔코드 :

import java.io.*;
import java.util.*;

public class boj2467 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
        }

        int left = 0;
        int right = arr.length - 1;
        int sum = 0;

        int resultLeft = 0;
        int resultRight = 0;
        int minValue = Integer.MAX_VALUE;

        while(left < right){
            sum = arr[left] + arr[right];
            if(Math.abs(minValue) > Math.abs(sum)){
                minValue = sum;
                resultLeft = left;
                resultRight = right;
            }
            if(sum == 0){
                break;
            }else if(sum < 0){
                left++;
            }else{
                right--;
            }
        }
        System.out.println(arr[resultLeft] + " " + arr[resultRight]);
    }
}


🎈boj 16120 - PPAP


🗨 해결방법 :


문자열을 하나씩 스택에 넣으며 PPAP를 탐색하면 해당 문자열을 스택에서 pop하고 p를 push하여 ppap 문자열을 만들었습니다.

📝메모 :


스택을 사용하지 않고 구현해보자.

✔코드 :

import java.io.*;
import java.util.*;

public class boj16120 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] ppap = br.readLine().toCharArray();

        if(isPPAP(ppap)){
            System.out.println("PPAP");
        }else{
            System.out.println("NP");
        }
    }

    static boolean isPPAP(char[] ppap){

        if(ppap.length < 4){
            if(ppap.length == 1 && ppap[0] == 'P'){
                return true;
            }
            return false;
        }

        Stack<Character> s = new Stack<>();

        for (int i = 0; i < 3; i++) {
            s.push(ppap[i]);
        }

        for (int i = 3; i < ppap.length; i++) {
            if(s.size() >= 3 && ppap[i] == 'P' && s.elementAt(s.size() - 1) == 'A' && s.elementAt(s.size() - 2) == 'P'&& s.elementAt(s.size() - 3) == 'P'){
                for (int j = 0; j < 3; j++) {
                    s.pop();
                }
            }
            s.push(ppap[i]);
        }

        if(s.size() == 1 && s.peek() == 'P'){
            return true;
        }else{
            return false;
        }
    }
}

@leetaggg leetaggg changed the title 11 이태호 / 10월 4주차 / 목 Oct 27, 2023
@leetaggg leetaggg added I'm TaeHO 나는 이태호입니다 A준 나는 잘생겼어요 labels Oct 27, 2023
@leetaggg leetaggg merged commit 42814c7 into SSAFY-10th-Seoul17:main Oct 30, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A준 나는 잘생겼어요 I'm TaeHO 나는 이태호입니다
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants