Skip to content

Commit

Permalink
39_pushedrumex (#157)
Browse files Browse the repository at this point in the history
* 크기가 작은 부분 문자열

* 미로 탈출 명령어

* 압축

* 거스름돈

* 우박 수열 정적분
  • Loading branch information
pushedrumex authored Jul 22, 2024
1 parent 27dc670 commit 227877d
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
16 changes: 16 additions & 0 deletions week-39/pushedrumex/거스름돈.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.*;

class Solution {
public int solution(int n, int[] money) {
int[] count = new int[n+1];
count[0] = 1;
for (int m: money) {
for (int value=0;value<n;value++) {
if (value+m > n) break;
count[value+m] += count[value];
}
}

return count[n] % 1_000_000_007;
}
}
89 changes: 89 additions & 0 deletions week-39/pushedrumex/미로 탈출 명령어.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import java.util.*;

class Solution {

int n, m, r, c, k;
String answer = "impossible";
ArrayDeque<String> path = new ArrayDeque<>();
int count = 0;;

Direction[] directions = {
new Direction("d", 1, 0),
new Direction("l", 0, -1),
new Direction("r", 0, 1),
new Direction("u", -1, 0)
};

public String solution(int n, int m, int x, int y, int r, int c, int k) {
x--;
y--;
r--;
c--;
this.n = n;
this.m = m;
this.r = r;
this.c = c;
this.k = k;

dfs(new Node(x, y));

return answer;
}

void dfs(Node now) {

if (!answer.equals("impossible")) {
return;
}

if (Math.abs(now.x-r) + Math.abs(now.y-c) > k-count) {
return;
}

if ((Math.abs(now.x-r) + Math.abs(now.y-c) + k-count) % 2 != 0) {
return;
}

if (count == k) {
if (now.x == r && now.y == c) {
answer = String.join("", path);
}
return;
}

for (Direction direction: directions) {
int nextX = now.x + direction.dx;
int nextY = now.y + direction.dy;

if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= m) {
continue;
}

count++;
path.addLast(direction.name);
dfs(new Node(nextX, nextY));
count--;
path.removeLast();
}
}

class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}

class Direction {
String name;
int dx;
int dy;
Direction(String name, int dx, int dy) {
this.name = name;
this.dx = dx;
this.dy = dy;
}
}
}
36 changes: 36 additions & 0 deletions week-39/pushedrumex/압축.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;

class Solution {
public int[] solution(String msg) {
HashMap<String, Integer> map = new HashMap<>();
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int idx = 1;
for (int i=0;i<26;i++) {
map.put(alpha.substring(i, i+1), idx++);
}

ArrayList<Integer> answer = new ArrayList<>();
int i = 0;
int N = msg.length();
while (i < N) {
String word = msg.substring(i, i+1);
i++;

// 사전에 없는 단어가 나올 때까지
while (i < N && map.containsKey(word)) {
word += msg.substring(i, i+1);
i++;
}

// 사전에 존재하지 않는다면
if (!map.containsKey(word)) {
answer.add(map.get(word.substring(0, word.length()-1)));
map.put(word, idx++);
i--;
} else {
answer.add(map.get(word));
}
}
return answer.stream().mapToInt(x -> x).toArray();
}
}
41 changes: 41 additions & 0 deletions week-39/pushedrumex/우박 수열 정적분.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.util.*;

class Solution {
public double[] solution(int k, int[][] ranges) {
ArrayList<Integer> y = new ArrayList<>();
y.add(k);
while (k != 1) {
if (k % 2 == 0) {
k /= 2;
} else {
k = k * 3 + 1;
}
y.add(k);
}

double[] sums = new double[y.size()];
for (int i=0;i<y.size()-1;i++) {
sums[i+1] = (y.get(i)+y.get(i+1)) / (double)2;
}
for (int i=1;i<sums.length;i++) {
sums[i] += sums[i-1];
}
System.out.println(Arrays.toString(sums));
double[] answer = new double[ranges.length];
for (int i=0;i<ranges.length;i++) {
int[] range = ranges[i];
int x1 = range[0];
int x2 = sums.length+range[1]-1;

if (x1 > x2) {
answer[i] = -1;
} else if (x1 == x2) {
answer[i] = 0;
} else {
answer[i] = sums[x2] - sums[x1];
}
}

return answer;
}
}
15 changes: 15 additions & 0 deletions week-39/pushedrumex/크기가 작은 부분 문자열.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int solution(String t, String p) {
int N = t.length();
int M = p.length();

long pLong = Long.parseLong(p);
int answer = 0;
for (int i=0;i<=N-M;i++) {
if (Long.parseLong(t.substring(i, i+M)) <= pLong) {
answer++;
}
}
return answer;
}
}

0 comments on commit 227877d

Please sign in to comment.