Skip to content

[김민진-15주차 알고리즘 스터디] #94

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 4 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
50 changes: 50 additions & 0 deletions 김민진/15주차/[Baekjoon-10986] 나머지 합.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package week15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Q4RemaningSum {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;

private static int N;
private static int M;

private static long[] mod;

public static void main(String[] args) throws IOException {
init();
sol();
}

private static void init() throws IOException {
st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

mod = new long[M];

long psum = 0;

st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
psum = (psum + Integer.parseInt(st.nextToken())) % M;
mod[(int) psum] += 1;
}
}

private static void sol() {
// 나머지가 0인 구간
long cnt = mod[0];

for (int i = 0; i < M; i++) {
cnt += (mod[i] * (mod[i] - 1)) / 2;
}
System.out.println(cnt);
}

}
44 changes: 44 additions & 0 deletions 김민진/15주차/[Baekjoon-13164] 행복 유치원.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package week15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;

public class Q3HappyKindergarten {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer st;

public static void main(String[] args) throws IOException {
sol();
}

private static void sol() throws IOException {
st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

int[] kids = new int[N];

st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
kids[i] = Integer.parseInt(st.nextToken());
}

Queue<Integer> pq = new PriorityQueue<>();
for (int i = 1; i < N; i++) {
pq.offer(kids[i] - kids[i - 1]);
}

int cnt = 0;
while (N-- - M > 0) {
cnt += pq.poll();
}
System.out.println(cnt);
}

}
32 changes: 32 additions & 0 deletions 김민진/15주차/[Baekjoon-1699] 제곱수의 합.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package week15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Q5SumOfSquareNumber {

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

private static int N;
private static int[] dp;

public static void main(String[] args) throws IOException {
sol();
}

private static void sol() throws IOException {
N = Integer.parseInt(br.readLine());

dp = new int[N + 1];

for (int i = 1; i <= N; i++) {
dp[i] = i;
for (int j = 1; j * j <= i; j++) {
dp[i] = Math.min(dp[i], dp[i - j * j] + 1);
}
}
System.out.println(dp[N]);
}

}
80 changes: 80 additions & 0 deletions 김민진/15주차/[Baekjoon-1918] 후위 표기식.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package week15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Q2Postfix {

private static final Map<Character, Integer> OP = new HashMap<>() {
{
put('+', 0);
put('-', 0);
put('*', 1);
put('/', 1);
put('(', -1);
put(')', -1);
}
};

private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static final StringBuilder sb = new StringBuilder();

private static String s;
private static Stack<Character> stack;

public static void main(String[] args) throws IOException {
sol();
}

private static void sol() throws IOException {
s = br.readLine().trim();
stack = new Stack<>();

int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);

// nums
if (!OP.containsKey(c)) {
sb.append(c);
continue;
}

// ops
switch (c) {
case '(' ->
stack.push(c);
case ')' -> {
while (!stack.isEmpty() && stack.peek() != '(') {
sb.append(stack.pop());
}
// rm '('
stack.pop();
}
default -> {
int pri = OP.get(c);

while (!stack.isEmpty() && OP.get(stack.peek()) >= pri) {
sb.append(stack.pop());
}
stack.push(c);
}
}
}

// print remaining ops
while (!stack.isEmpty()) {
char pop = stack.pop();

if (pop != '(' && pop != ')') {
sb.append(pop);
}
}

System.out.println(sb);
}
}