문제를 정답률 높은 순으로 풀어서 지금 거의 뒷쪽이라 정답률이 낮은 문제들을 풀고 있는데 LV1이지만 쉽지 않은 문제들이 많았다. LV1 마무리 하면 이모지로 표시해둔 문제만 다시 풀어봐야겠다.
class Solution {
public String solution(String s, String skip, int index) {
StringBuilder sb = new StringBuilder();
for(char c : s.toCharArray()) {
int plus = 0;
while(plus < index) {
c++;
if(c > 'z') {
c = 'a';
}
if(!skip.contains(String.valueOf(c))) {
plus++;
}
}
sb.append(c);
}
return sb.toString();
}
}class Solution {
public int solution(String[][] board, int h, int w) {
int answer = 0;
String color = board[h][w];
int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
for(int i = 0; i < 4; i++){
int nx = w + dx[i];
int ny = h + dy[i];
if(nx < 0 || nx >= board[0].length || ny < 0 || ny >= board.length) continue;
if(board[ny][nx].equals(color)) answer++;
}
return answer;
}
}class Solution {
public int solution(String s) {
int answer = 0;
int same = 0, another = 0;
char cur = ' ';
for(char c : s.toCharArray()){
if(same == 0){
cur = c;
same++;
}
else if(c == cur){
same++;
}
else if(c != cur){
another++;
}
if(same == another){
answer++;
same = 0;
another = 0;
cur = ' ';
}
}
if(cur != ' ') answer++;
return answer;
}
}import java.util.*;
class Solution {
public int[] solution(String[] keymap, String[] targets) {
int[] answer = new int[targets.length];
Map<Character, Integer> m = new HashMap<>();
for(String key : keymap){
for(int i = 0; i < key.length(); i++){
char c = key.charAt(i);
m.put(c, Math.min(m.getOrDefault(c, 101), i + 1));
}
}
for(int i = 0; i < targets.length; i++){
for(int j = 0; j < targets[i].length(); j++){
char c = targets[i].charAt(j);
if(!m.containsKey(c)){
answer[i] = -1;
break;
}
answer[i] += m.get(c);
}
}
return answer;
}
}class Solution {
public int solution(String dartResult) {
int answer = 0;
int cur = 0, prev = -101, idx = 0;
char b = ' ', op = ' ';
char[] ch = dartResult.toCharArray();
while(idx < ch.length){
char c = ch[idx];
if(Character.isDigit(c)){
int i = c - '0';
if(i == 1 && idx + 1 < ch.length && ch[idx + 1] == '0'){
cur = 10;
idx += 2;
}
else{
cur = i;
idx++;
}
}
else{
if(c == 'S' || c == 'D' || c == 'T'){
b = c;
int plus = Score(cur, b);
if(idx + 1 < ch.length && (ch[idx + 1] == '*' || ch[idx + 1] == '#')){
op = ch[idx + 1];
idx += 2;
if(op == '*'){
plus *= 2;
if(prev != -101){
answer += prev;
}
}
else plus *= -1;
}
else{
idx++;
}
prev = plus;
answer += plus;
}
}
}
return answer;
}
private int Score(int s, char b){
int r = 0;
switch(b){
case 'S':
r += s;
break;
case 'D':
r += Math.pow(s, 2);
break;
case 'T':
r += Math.pow(s, 3);
break;
}
return r;
}
}
// 개선
class Solution {
public int solution(String dartResult) {
int[] score = new int[3];
int round = -1;
char[] ch = dartResult.toCharArray();
for(int i = 0; i < ch.length; i++){
char c = ch[i];
if(Character.isDigit(c)){
round++;
if(c == '1' && ch[i + 1] == '0'){
score[round] = 10;
i++;
} else {
score[round] = c - '0';
}
}
else if(c == 'S' || c == 'D' || c == 'T'){
if(c == 'D') score[round] = (int)Math.pow(score[round], 2);
else if(c == 'T') score[round] = (int)Math.pow(score[round], 3);
}
else if(c == '*'){
score[round] *= 2;
if(round > 0) {
score[round - 1] *= 2;
}
}
else if(c == '#'){
score[round] *= -1;
}
}
return score[0] + score[1] + score[2];
}
}class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int[] rank = {6, 6, 5, 4, 3, 2, 1};
int zero = 0, cnt = 0;
for(int num : lottos){
if(num == 0){
zero++;
continue;
}
for(int win : win_nums){
if(num == win){
cnt++;
break;
}
}
}
return new int[]{rank[cnt + zero], rank[cnt]};
}
}class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length;
for(int lo : lost){
for(int i = 0; i < reserve.length; i++){
if(reserve[i] == lo || reserve[i] == lo - 1 || reserve[i] == lo + 1){
reserve[i] = -1;
answer++;
break;
}
}
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length;
Arrays.sort(lost);
Arrays.sort(reserve);
for (int i = 0; i < lost.length; i++) {
for (int j = 0; j < reserve.length; j++) {
if (lost[i] == reserve[j]) {
answer++;
lost[i] = -1;
reserve[j] = -1;
break;
}
}
}
for (int i = 0; i < lost.length; i++) {
if(lost[i] == -1) continue;
for (int j = 0; j < reserve.length; j++) {
if (lost[i] - 1 == reserve[j] || lost[i] + 1 == reserve[j]) {
answer++;
reserve[j] = -1;
break;
}
}
}
return answer;
}
}import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
String answer = "";
Map<String, Integer> m = new HashMap<>();
for(String com : completion){
if(m.containsKey(com)){
m.put(com, 1 + m.get(com));
}
else{
m.put(com, 1);
}
}
for(String par : participant){
if(!m.containsKey(par)){
answer = par;
break;
}
else{
int cnt = m.get(par);
if(cnt > 1) m.put(par, cnt - 1);
else m.remove(par);
}
}
return answer;
}
}import java.util.*;
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
List<Integer> list = new ArrayList<>();
for(int i : ingredient){
list.add(i);
while(list.size() >= 4){
int s = list.size();
if(list.get(s - 1) != 1 ||
list.get(s - 2) != 3 ||
list.get(s - 3) != 2 ||
list.get(s - 4) != 1) break;
for(int j = 0; j < 4; j++){
list.remove(list.size() - 1);
}
answer++;
}
}
return answer;
}
}
// 개선
class Solution {
public int solution(int[] ingredient) {
int answer = 0;
int[] stack = new int[ingredient.length];
int sp = 0;
for (int i : ingredient) {
stack[sp] = i;
sp++;
if (sp >= 4 &&
stack[sp - 1] == 1 &&
stack[sp - 2] == 3 &&
stack[sp - 3] == 2 &&
stack[sp - 4] == 1) {
answer++;
sp -= 4;
}
}
return answer;
}
}import java.util.*;
class Solution {
public String solution(String X, String Y) {
List<String> li = new ArrayList<>();
for(char c : X.toCharArray()){
String s = Character.toString(c);
if(Y.contains(s)){
Y = Y.replaceFirst(s, " ");
li.add(s);
}
}
if(li.size() == 0) return "-1";
Collections.sort(li, Collections.reverseOrder());
StringBuilder sb = new StringBuilder();
for(String s : li){
if(sb.length() == 0 && s.equals("0")) continue;
sb.append(s);
}
if(sb.length() == 0) return "0";
return sb.toString();
}
}
// 개선
class Solution {
public String solution(String X, String Y) {
int[] countX = new int[10];
int[] countY = new int[10];
for(char c : X.toCharArray()) countX[c - '0']++;
for(char c : Y.toCharArray()) countY[c - '0']++;
StringBuilder sb = new StringBuilder();
for(int i = 9; i >= 0; i--){
int common = Math.min(countX[i], countY[i]);
for(int j = 0; j < common; j++){
sb.append(i);
}
}
if(sb.length() == 0) return "-1";
if(sb.charAt(0) == '0') return "0";
return sb.toString();
}
}import java.util.*;
class Solution {
public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
String[] condition = {"code", "date", "maximum", "remain"};
int idx1 = -1, idx2 = -1;
for(int i = 0; i < 4; i++){
if(condition[i].equals(ext)) idx1 = i;
if(condition[i].equals(sort_by)) idx2 = i;
}
List<int[]> li = new ArrayList<>();
for(int[] d : data){
if(d[idx1] < val_ext)
li.add(d);
}
final int sortIdx = idx2;
li.sort((a, b) -> a[sortIdx] - b[sortIdx]);
int[][] answer = new int[li.size()][4];
for(int i = 0; i < li.size(); i++){
answer[i] = li.get(i);
}
return answer;
}
}import java.util.*;
class Solution {
public String solution(String[] survey, int[] choices) {
Map<Character, Integer> m = new HashMap<>();
int[] score = {3, 2, 1, 0, 1, 2, 3};
for(int i = 0; i < survey.length; i++){
char[] ch = survey[i].toCharArray();
if(choices[i] == 4) continue;
else if(choices[i] < 4){
m.put(ch[0], m.getOrDefault(ch[0], 0) + score[choices[i] - 1]);
}
else{
m.put(ch[1], m.getOrDefault(ch[1], 0) + score[choices[i] - 1]);
}
}
StringBuilder sb = new StringBuilder();
char[] ch = {'R', 'T', 'C', 'F', 'J', 'M', 'A', 'N'};
for(int i = 0; i < 8; i += 2){
if(m.getOrDefault(ch[i], 0) > m.getOrDefault(ch[i + 1], 0)){
sb.append(ch[i]);
}
else if(m.getOrDefault(ch[i], 0) < m.getOrDefault(ch[i + 1], 0)){
sb.append(ch[i + 1]);
}
else{
char c = ch[i] < ch[i + 1]? ch[i] : ch[i + 1];
sb.append(c);
}
}
return sb.toString();
}
}import java.util.*;
class Solution {
public int solution(int[][] board, int[] moves) {
int answer = 0;
List<Integer>[] li = new ArrayList[board.length];
for(int i = board[0].length - 1; i >= 0; i--){
li[i] = new ArrayList<>();
for(int j = board.length - 1; j >= 0; j--){
if(board[j][i] == 0) continue;
li[i].add(board[j][i]);
}
}
List<Integer> bucket = new ArrayList<>();
for(int m : moves){
if(li[m - 1].size() <= 0) continue;
int num = li[m - 1].get(li[m - 1].size() - 1);
li[m - 1].remove(li[m - 1].size() - 1);
if(bucket.size() > 0 && bucket.get(bucket.size() - 1) == num){
answer += 2;
bucket.remove(bucket.size() - 1);
}
else{
bucket.add(num);
}
}
return answer;
}
}
// 개선
import java.util.*;
class Solution {
public int solution(int[][] board, int[] moves) {
int answer = 0;
Stack<Integer> bucket = new Stack<>();
for (int m : moves) {
int col = m - 1;
for (int row = 0; row < board.length; row++) {
if (board[row][col] != 0) {
int doll = board[row][col];
board[row][col] = 0;
if (!bucket.isEmpty() && bucket.peek() == doll) {
bucket.pop();
answer += 2;
} else {
bucket.push(doll);
}
break;
}
}
}
return answer;
}
}import java.util.*;
class Solution {
public String solution(int[] numbers, String hand) {
int[] xy1 = {1, 4}, xy2 = {3, 4};
Map<Integer, int[]> m = new HashMap<>();
int num = 1;
for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 3; j++){
m.put(num++, new int[]{j, i});
}
}
m.put(0, new int[]{2, 4});
StringBuilder sb = new StringBuilder();
for(int n : numbers){
int[] p = m.get(n);
if(n == 1 || n == 4 || n == 7){
sb.append("L");
xy1 = p;
}
else if(n == 3 || n == 6 || n == 9){
sb.append("R");
xy2 = p;
}
else{
int d1 = Math.abs(p[0] - xy1[0]) + Math.abs(p[1] - xy1[1]);
int d2 = Math.abs(p[0] - xy2[0]) + Math.abs(p[1] - xy2[1]);
if(d1 == d2){
if(hand.equals("left")){
sb.append("L");
xy1 = p;
}
else{
sb.append("R");
xy2 = p;
}
}
else if(d1 < d2){
sb.append("L");
xy1 = p;
}
else{
sb.append("R");
xy2 = p;
}
}
}
return sb.toString();
}
}class Solution {
public String solution(String new_id) {
String answer = new_id.toLowerCase();
answer = answer.replaceAll("[^a-z0-9-_.]", "");
answer = answer.replaceAll("\\.{2,}", ".");
answer = answer.replaceAll("^\\.|\\.$", "");
if(answer.length() == 0) answer = "a";
if(answer.length() >= 16) {
answer = answer.substring(0, 15);
answer = answer.replaceAll("\\.$", "");
}
if(answer.length() <= 2){
while(answer.length() < 3){
answer += answer.charAt(answer.length() - 1);
}
}
return answer;
}
}class Solution {
public int[] solution(String[] wallpaper) {
int lux = 51, luy = 51, rdx = -1, rdy = -1;
for(int i = 0; i < wallpaper.length; i++){
char[] ch = wallpaper[i].toCharArray();
for(int j = 0; j < ch.length; j++){
if(ch[j] == '#'){
lux = Math.min(i, lux);
luy = Math.min(j, luy);
rdx = Math.max(i + 1, rdx);
rdy = Math.max(j + 1, rdy);
}
}
}
int[] answer = {lux, luy, rdx, rdy};
return answer;
}
}import java.util.*;
class Solution {
public int[] solution(String today, String[] terms, String[] privacies) {
Map<String, Integer> map = new HashMap<>();
for(String t : terms){
String[] split = t.split(" ");
map.put(split[0], Integer.parseInt(split[1]));
}
int todayDays = toDays(today);
List<Integer> result = new ArrayList<>();
for(int i = 0; i < privacies.length; i++){
String[] split = privacies[i].split(" ");
int start = toDays(split[0]);
int expire = start + map.get(split[1]) * 28;
if(expire <= todayDays){
result.add(i + 1);
}
}
return result.stream().mapToInt(i -> i).toArray();
}
private int toDays(String date){
int y = Integer.parseInt(date.substring(0, 4));
int m = Integer.parseInt(date.substring(5, 7));
int d = Integer.parseInt(date.substring(8, 10));
return y * 12 * 28 + m * 28 + d;
}
}class Solution {
public int solution(int[] schedules, int[][] timelogs, int startday) {
int answer = 0;
for(int i = 0; i < timelogs.length; i++){
int start = startday;
int h = schedules[i] / 100;
int m = schedules[i] % 100;
m += 10;
if(m >= 60){
h++;
m -= 60;
}
int time = h * 100 + m;
boolean get = true;
for(int j = 0; j < 7; j++){
if(start != 6 && start != 0){
if(timelogs[i][j] > time){
get = false;
break;
}
}
start = (start + 1) % 7;
}
if(get) answer++;
}
return answer;
}
}
// 개선
class Solution {
public int solution(int[] schedules, int[][] timelogs, int startday) {
int answer = 0;
for(int i = 0; i < timelogs.length; i++){
int start = startday;
int h = schedules[i] / 100;
int m = schedules[i] % 100;
m += 10;
if(m >= 60){
h++;
m -= 60;
}
int time = h * 100 + m;
boolean get = true;
for(int j = 0; j < 7; j++){
if(start != 6 && start != 7){
if(timelogs[i][j] > time){
get = false;
break;
}
}
start = (start % 7) + 1;
}
if(get) answer++;
}
return answer;
}
}import java.util.*;
class Solution {
public String[] solution(String[] players, String[] callings) {
String[] answer = players;
Map<String, Integer> m = new HashMap<>();
for(int i = 0; i < players.length; i++){
m.put(players[i], i);
}
for(String c : callings){
int idx = m.get(c);
answer[idx] = answer[idx - 1];
answer[idx - 1] = c;
m.put(answer[idx], idx);
m.put(answer[idx - 1], idx - 1);
}
return answer;
}
}class Solution {
public int[] solution(String[] park, String[] routes) {
int x= -1, y = -1;
int[][] grid = new int[park.length][park[0].length()];
for(int i = 0; i < park.length; i++){
for(int j = 0; j < park[i].length(); j++){
if(park[i].charAt(j) == 'S'){
grid[i][j] = 0;
x = i;
y = j;
}
else if(park[i].charAt(j) == 'O')
grid[i][j] = 0;
else grid[i][j] = 1;
}
}
for(String r : routes){
String[] s = r.split(" ");
int cnt = Integer.parseInt(s[1]);
if(check(s[0], grid, x, y, cnt)){
switch(s[0]){
case "S":
x += cnt;
break;
case "N":
x -= cnt;
break;
case "E":
y += cnt;
break;
case "W":
y -= cnt;
break;
}
}
}
return new int[]{x, y};
}
private boolean check(String s, int[][] grid, int x, int y, int cnt){
if(s.equals("S")){
if(x + cnt >= grid.length){
return false;
}
for(int i = 1; i <= cnt; i++){
if(grid[x + i][y] == 1) return false;
}
}
else if(s.equals("N")){
if(x - cnt < 0){
return false;
}
for(int i = 1; i <= cnt; i++){
if(grid[x - i][y] == 1) return false;
}
}
else if(s.equals("E")){
if(y + cnt >= grid[x].length){
return false;
}
for(int i = 1; i <= cnt; i++){
if(grid[x][y + i] == 1) return false;
}
}
else if(s.equals("W")){
if(y - cnt < 0){
return false;
}
for(int i = 1; i <= cnt; i++){
if(grid[x][y - i] == 1) return false;
}
}
return true;
}
}
// 개선
import java.util.*;
class Solution {
public int[] solution(String[] park, String[] routes) {
int x = -1, y = -1;
for (int i = 0; i < park.length; i++) {
if (park[i].contains("S")) {
x = i;
y = park[i].indexOf("S");
break;
}
}
Map<String, int[]> dir = new HashMap<>();
dir.put("N", new int[]{-1, 0});
dir.put("S", new int[]{1, 0});
dir.put("W", new int[]{0, -1});
dir.put("E", new int[]{0, 1});
for (String r : routes) {
String[] s = r.split(" ");
String op = s[0];
int cnt = Integer.parseInt(s[1]);
int nx = x;
int ny = y;
boolean isOk = true;
for (int i = 0; i < cnt; i++) {
nx += dir.get(op)[0];
ny += dir.get(op)[1];
if (nx < 0 || nx >= park.length || ny < 0 || ny >= park[0].length() || park[nx].charAt(ny) == 'X') {
isOk = false;
break;
}
}
if (isOk) {
x = nx;
y = ny;
}
}
return new int[]{x, y};
}
}import java.util.*;
class Solution {
public int solution(int[] mats, String[][] park) {
int answer = 0;
Arrays.sort(mats);
for(int idx = mats.length - 1; idx >= 0; idx--){
int m = mats[idx];
for(int i = 0; i <= park.length - m; i++){
for(int j = 0; j <= park[i].length - m; j++){
if(!park[i][j].equals("-1")) continue;
boolean check = true;
for(int k = i; k < i + m; k++){
for(int l = j; l < j + m; l++){
if(!park[k][l].equals("-1")){
check = false;
break;
}
}
if(!check) break;
}
if(check) return m;
}
}
}
return -1;
}
}
// 개선
import java.util.*;
class Solution {
public int solution(int[] mats, String[][] park) {
int answer = 0;
Arrays.sort(mats);
int n = park.length, m = park[0].length;
int[][] dp = new int[n][m];
int max = 0;
for(int i = 0; i < n; i++){
if(park[i][0].equals("-1")){
dp[i][0] = 1;
max = 1;
}
}
for(int i = 0; i < m; i++){
if(park[0][i].equals("-1")){
dp[0][i] = 1;
max = 1;
}
}
for(int i = 1; i < n; i++){
for(int j = 1; j < m; j++){
if(!park[i][j].equals("-1")) continue;
int now = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
max = Math.max(max, now);
dp[i][j] = now;
}
}
for(int i = mats.length - 1; i >= 0; i--){
if(max >= mats[i]) return mats[i];
}
return -1;
}
}