문제 21 - 2016년
후 나 날짜에 약한데 !
-> 월 먼저 계산하고 뒤에 요일 계산하기
class Solution {
public String solution(int a, int b) {
String [] days = new String [] {"SUN","MON","TUE","WED","THU","FRI","SAT"};
int [] month = new int[] {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int now = 0;
for (int i = 0; i < a; i++) {
now += month[i];
}
now += b;
return days[ (now + 4) % 7 ];
}
}
class Solution {
public String solution(String[] participant, String[] completion) {
Arrays.sort(participant);
Arrays.sort(completion);
for(int i = 0; i < completion.length; i++) {
if (!participant[i].equals(completion[i])) return participant[i];
}
return participant[participant.length - 1];
}
}
class Solution {
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
int nVideo_len = setSecondTime(video_len);
int nPos = setSecondTime(pos);
int nOp_start = setSecondTime(op_start);
int nOp_end = setSecondTime(op_end);
for (int i = 0; i < commands.length; i++) {
if (nPos >= nOp_start && nPos <= nOp_end) nPos = nOp_end;
switch (commands[i]) {
case "prev" :
if (nPos < 10) nPos = 0;
else nPos -= 10;
break;
case "next" :
if (nPos >= (nVideo_len - 10)) nPos = nVideo_len;
else nPos += 10;
break;
}
if (nPos >= nOp_start && nPos <= nOp_end) nPos = nOp_end;
}
return setTime(nPos);
}
// 시간 -> 초 단위
public int setSecondTime(String time) {
String [] pTime = time.split(":");
return Integer.parseInt(pTime[0]) * 60 + Integer.parseInt(pTime[1]);
}
// 초 단위 -> 시간
public String setTime(int time) {
int minute = time / 60;
int second = time % 60;
return String.format("%02d:%02d", minute, second);
}
}
🔅 String.format() !
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
// 1. Set으로 중복 제거
Set<String> reported = new HashSet<String>(Arrays.asList(report));
// 2. 신고당한 횟수 카운트 --> map 사용
Map<String, Integer> count = new HashMap<String, Integer>();
for(String r : reported) {
String reporter = r.split(" ")[1];
count.put(reporter, count.getOrDefault(reporter, 0) + 1);
}
// 3. 정지 먹은 회원 판단
Set<String> suspend = new HashSet();
for(Map.Entry<String, Integer> map : count.entrySet()) {
if(map.getValue() >= k) {
suspend.add(map.getKey());
}
}
// 4. 메일 보내기
Map<String , Integer> mail = new HashMap<String, Integer>();
for(String id : id_list) {
mail.put(id, 0);
}
for(String r : reported) {
String sender = r.split(" ")[0];
String reporter = r.split(" ")[1];
if(suspend.contains(reporter)) {
mail.put(sender, mail.get(sender) + 1);
}
}
// 5. set을 배열로 바꿔주기
int [] result = new int [id_list.length];
for(int i = 0; i < result.length; i++) {
result[i] = mail.get(id_list[i]);
}
return result;
}
}
어우 변수 너무 많아
import java.util.*;
class Solution {
public int solution(int[] nums) {
Set<Integer> unique = new HashSet<>();
for (int i : nums) {
unique.add(i);
}
int maxSize = nums.length / 2;
return Math.min(unique.size(), maxSize);
}
}
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int [] result = new int [commands.length];
for(int i = 0; i < commands.length; i++) {
int start = commands[i][0];
int end = commands[i][1];
int k = commands[i][2];
int [] sub = new int [end - start + 1 ];
int num = 0;
for(int j = start - 1; j < end; j++) {
sub[num] = array[j];
num++;
}
Arrays.sort(sub);
result[i] = sub[k - 1];
}
return result;
}
}
import java.util.*;
public class Solution {
public int[] solution(int []arr) {
Stack<Integer> stack = new Stack<>();
for(int i : arr) {
if(stack.isEmpty() || stack.peek() != i) stack.push(i);
}
int[] answer = new int[stack.size()];
for(int i = stack.size() - 1 ; i >= 0; i--) {
answer[i] = stack.pop();
}
return answer;
}
}
레벨 1 끝 ~.. ㅜ