주식가격
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int length = prices.length;
int[] answer = new int[length];
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < length; i++) {
queue.add(prices[i]);
}
int red = 0;
while (queue.size() > 0) {
int price = queue.poll();
for (int i = (length - queue.size()); i < length; i++) {
if (price <= prices[i]) answer[red]++;
else {
answer[red]++;
break;
}
}
red++;
}
return answer;
}
}
올바른 괄호
import java.util.*;
class Solution {
boolean solution(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') stack.push('(');
else {
if(stack.isEmpty()) return false;
else stack.pop();
}
}
return stack.isEmpty() ? true : false;
}
}
프로세스
import java.util.*;
class Solution {
public int solution(int[] priorities, int location) {
int answer = 1;
Queue<Integer> processes = new LinkedList<>();
Queue<Integer> indexes = new LinkedList<>();
for (int i = 0; i < priorities.length; i++) {
processes.add(priorities[i]);
indexes.add(i);
}
int max = Collections.max(processes);
int process, index;
while (!processes.isEmpty()) {
process = processes.poll();
index = indexes.poll();
if (process == max && index == location) {
return answer;
}
else if (process == max) {
answer++;
max = Collections.max(processes);
}
else {
processes.add(process);
indexes.add(index);
}
}
return answer;
}
}
유익한 글이었습니다.