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

1/27 studywebservice 복습 + 이중우선순위큐 #46

Open
skarltjr opened this issue Jan 27, 2021 · 0 comments
Open

1/27 studywebservice 복습 + 이중우선순위큐 #46

skarltjr opened this issue Jan 27, 2021 · 0 comments
Labels

Comments

@skarltjr
Copy link
Owner

package algo;


import java.util.Collections;
import java.util.PriorityQueue;

public class Solution {
    public int[] solution(String[] operations) {
        int[] answer = new int[2];
        PriorityQueue<Integer> maxQ = new PriorityQueue<>(Collections.reverseOrder()); // 내림차순 맨 앞 max
        PriorityQueue<Integer> minQ = new PriorityQueue<>();    //오름차순 맨 앞 min

        
        /** {"I 4", "I 3", "I 2", "I 1", "D 1", "D 1", "D -1", "D -1", "I 5", "I 6"}; 같은 경우를 생각해야한다*/
        for (int i = 0; i < operations.length; i++)
        {
            if (!maxQ.isEmpty() && !minQ.isEmpty() && maxQ.peek() < minQ.peek())
            {
                maxQ = new PriorityQueue<>(Collections.reverseOrder());
                minQ = new PriorityQueue<>();
            }


            if (operations[i].charAt(0) == 'I')
            {
                if (operations[i].charAt(2) == '-')
                {
                    // 16같은경우 1만 들어감
                    String substring = operations[i].substring(3, operations[i].length());
                    minQ.add(-Integer.parseInt(substring));
                    maxQ.add(-Integer.parseInt(substring));
                } else {
                    String substring = operations[i].substring(2, operations[i].length());
                    minQ.add(Integer.parseInt(substring));
                    maxQ.add(Integer.parseInt(substring));
                }
            }
            if (operations[i].charAt(0) == 'D')
            {
                if (operations[i].charAt(2) == '-' && !minQ.isEmpty())
                {
                    minQ.remove();
                }
                else if (operations[i].charAt(2) != '-' && !maxQ.isEmpty())
                {
                    maxQ.remove();
                }
            }
        }

        // 최대값이 비었는데 최소값이 존재할 수 없다.
        if (maxQ.isEmpty())
        {
            answer[0] = 0;
            answer[1] = 0;
        }else{
            answer[0] = maxQ.peek();
            answer[1] = minQ.peek();
        }



        return answer;
    }
}


@skarltjr skarltjr added the Daily label Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant