Skip to content

solve(방금그곡, 징검다리 건너기): soyun_230912 #194

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions soyun/week27/압축.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.*;

class Solution {

public List<Integer> solution(String msg) {
ArrayList<Integer> answer = new ArrayList<>();
HashMap<String, Integer> dict = new HashMap<>();

int dictIdx = 1;
for(int i='A'; i<='Z'; i++){
dict.put( String.valueOf((char)i), dictIdx++) ;
}

int idx = 0;
while(idx < msg.length()){
StringBuilder w = new StringBuilder();
while(idx < msg.length()){
if(!dict.containsKey(w.toString() + msg.charAt(idx))){
break;
}
w.append(msg.charAt(idx++));
}

answer.add(dict.get(w.toString()));
if(idx < msg.length()){
dict.put(w.append(msg.charAt(idx)).toString(), dictIdx++);
}
}

return answer;
}
}
33 changes: 14 additions & 19 deletions soyun/week27/프렌즈4블록.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import java.util.*;

class Solution {

private char[][] board;
private int popped;

public int solution(int m, int n, String[] input) {
board = new char[m][n];
popped = 0;
Expand All @@ -16,11 +16,14 @@ public int solution(int m, int n, String[] input) {
}
return popped;
}

public boolean pop(int m, int n) {
boolean isPopped = false;
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (board[i][j] == ' '){
continue;
}
if (isSame(i, j)) {
board[i][j] = Character.toLowerCase(board[i][j]);
board[i + 1][j] = Character.toLowerCase(board[i + 1][j]);
Expand All @@ -32,18 +35,15 @@ public boolean pop(int m, int n) {
}
return isPopped;
}

public boolean isSame(int x, int y) {
if (board[x][y] == ' ') {
return false;
}
char a = Character.toUpperCase(board[x][y]);
char b = Character.toUpperCase(board[x + 1][y]);
char c = Character.toUpperCase(board[x][y + 1]);
char d = Character.toUpperCase(board[x + 1][y + 1]);
return (a == b) && (a == c) && (a == d);
}

public void drag(int m, int n) {
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
Expand All @@ -52,18 +52,13 @@ public void drag(int m, int n) {
popped++;
continue;
}
if (i + 1 >= m) {
continue;
}
if (Character.isUpperCase(board[i + 1][j])) {
continue;
}
for (int next = i + 2; next < m; next++) {
if (Character.isUpperCase(board[next][j])) {
board[next - 1][j] = board[i][j];
board[i][j] = ' ';
break;
int row = i + 1;
while(row < m) {
if (board[row][j] == ' ') {
board[row][j] = board[row - 1][j];
board[row - 1][j] = ' ';
}
row++;
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions soyun/week28/방금그곡.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.*;

class Solution {

static class Music implements Comparable<Music>{
private int seq;
private String start;
private String end;
private String name;
private int playtime;
private String melody;

public Music(int seq, String start, String end, String name, String melody) {
this.seq = seq;
this.start = start;
this.end = end;
this.name = name;
this.playtime = getTime(end) - getTime(start);
this.melody = getMelody(playtime, melody);
}

public int getTime(String time) {
String[] s = time.split(":");
return Integer.parseInt(s[0]) * 60 + Integer.parseInt(s[1]);
}

public String getMelody(int playtime, String melody) {
int musicLength = melody.length();
if (playtime < musicLength) {
return melody.substring(0, playtime);
}
int quotient = playtime / musicLength;
int remain = playtime % musicLength;

return melody.repeat(quotient) + melody.substring(0, remain);
}

public int compareTo(Music other) {
if (this.playtime == other.playtime) {
return Integer.compare(this.seq, other.seq);
}
return -1 * Integer.compare(this.playtime, other.playtime);
}
}
public String solution(String m, String[] musicinfos) {
PriorityQueue<Music> pq = new PriorityQueue<>();
for (int i = 0; i < musicinfos.length; i++) {
String[] splitted = musicinfos[i].split(",");
String start = splitted[0];
String end = splitted[1];
String name = splitted[2];
String melody = replace(splitted[3]);
pq.offer(new Music(i, start, end, name, melody));
}
m = replace(m);
while (!pq.isEmpty()) {
Music music = pq.poll();
if (music.melody.contains(m)) {
return music.name;
}
}
return "(None)";
}

public String replace(String melody) {
return melody.replace("C#", "c").replace("D#", "d").replace("F#", "f").replace("G#", "g").replace("A#", "a");
}
}
26 changes: 26 additions & 0 deletions soyun/week28/징검다리_건너기.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.*;

class Solution {

public int solution(int[] stones, int k) {

PriorityQueue<int[]> q = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[0], o2[0]));

for (int i = 0; i < k - 1; i++) {
q.offer(new int[]{-1 * stones[i], i});
}
int min = Integer.MAX_VALUE;
for (int i = k - 1; i < stones.length; i++) {
while (!q.isEmpty()) {
int[] cur = q.peek();
if (cur[1] > i - k) {
break;
}
q.poll();
}
q.offer(new int[]{-1 * stones[i], i});
min = Math.min(min, -1 * q.peek()[0]);
}
return min;
}
}