-
Notifications
You must be signed in to change notification settings - Fork 4
[1주차] 이혜원 #3
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
[1주차] 이혜원 #3
Changes from 11 commits
685a141
266ecc3
253313c
7579654
e0f3ed9
b9f8fc0
19df404
d7cda1b
15917eb
26a2168
a54590f
0bd5ede
c2abd4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import java.io.*; | ||
import java.util.StringTokenizer; | ||
|
||
// 손님이 먹을 수 있는 초밥 가짓수의 최댓값을 구하는 프로그램 | ||
public class HW_2531 { | ||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int N = Integer.parseInt(st.nextToken()); // 초밥 벨트에 놓인 접시의 수 | ||
int d = Integer.parseInt(st.nextToken()); // 초밥의 가짓 수 | ||
int k = Integer.parseInt(st.nextToken()); // 연속해서 먹는 접시의 수 | ||
int c = Integer.parseInt(st.nextToken()); // 쿠폰 번호 | ||
|
||
int rail[] = new int[N]; // 레일배열 | ||
|
||
for(int i=0; i<N; i++) { | ||
rail[i] = Integer.parseInt(br.readLine()); | ||
} | ||
|
||
// 연속해서 먹는 접시의 크기가 변하지 않음 -> 슬라이딩 윈도우 | ||
// window : k, if(c) 쿠폰 번호 여부 확인하여 count++ | ||
int checkArr[] = new int[d+1]; // 초밥 종류 배열 | ||
int count = 0; // 경우의 수 카운트 | ||
int max = 0; | ||
// 윈도우 배열 초기화 | ||
for(int i=0; i<k; i++) { | ||
int a = rail[i]; | ||
if(checkArr[a] ==0) { | ||
count++; // 새로운 종류 초밥 | ||
} | ||
checkArr[a]++; // 초밥 개수 증가 | ||
} | ||
|
||
max = count; // 먹을 수 있는 초밥 최대 가짓 수 | ||
|
||
for(int i=0; i<N; i++) { // window : k, window내 중복X | ||
int start = i; | ||
int end = (i+k)%N; | ||
|
||
if(count >= max) { | ||
if(checkArr[c]==0) { | ||
max = count+1; | ||
} | ||
else { | ||
max = count; | ||
} | ||
} | ||
checkArr[rail[start]]--; | ||
if(checkArr[rail[start]]==0) { | ||
count--; | ||
} | ||
if(checkArr[rail[end]]==0) { | ||
count++; | ||
} | ||
checkArr[rail[end]]++; | ||
} | ||
System.out.println(max); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import java.util.Scanner; | ||
|
||
// 시간 복잡도 : 시간 제한 1초, 장애물의 크기 : O(NlogN) | ||
public class HW_3020 { | ||
public static void main(String[] args) { | ||
// 개똥벌레가 파괴해야 하는 장애물의 최솟값과 그러한 구간의 수 출력 | ||
Scanner sc = new Scanner(System.in); | ||
int N = sc.nextInt(); // 동굴 크기 (장애물의 개수) | ||
int H = sc.nextInt(); // 동굴 높이 | ||
|
||
int[] up = new int[H+1]; // 석순 장애물 | ||
int[] down = new int[H+1]; // 종유석 장애물 | ||
|
||
for (int n = 0; n < N; n++) { | ||
int height = sc.nextInt(); // 장애물의 높이를 입력 받음 | ||
if (n % 2 == 0) { | ||
up[height]++; | ||
} else { | ||
down[height]++; | ||
} | ||
} | ||
|
||
// 석순과 종유석 누적합 계산 | ||
for(int i=H-1; i>=1; i--) { | ||
up[i] += up[i + 1]; | ||
down[i] += down[i + 1]; | ||
} | ||
|
||
int min = N; | ||
int minCount = 0; | ||
|
||
for(int i=1; i<H+1; i++){ | ||
int total = up[i] + down[H - i + 1]; | ||
if(total < min){ | ||
min = total; // 최솟값 갱신 | ||
minCount = 1; | ||
} else if (total == min) { | ||
minCount++; | ||
} | ||
} | ||
System.out.println(min + " " + minCount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class HW_자율주행_자동차 { | ||
public static final int[] dx = {-1, 0, 1, 0}; // 북, 동, 남, 서 | ||
public static final int[] dy = {0, 1, 0, -1}; | ||
|
||
public static int n, m; // 세로 크기 n, 가로 크기 m | ||
public static int[][] grid; // 도로 상태 (0: 도로, 1: 인도) | ||
public static boolean[][] visited; // 방문 여부 체크 | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
n = Integer.parseInt(st.nextToken()); | ||
m = Integer.parseInt(st.nextToken()); | ||
|
||
st = new StringTokenizer(br.readLine()); | ||
int x = Integer.parseInt(st.nextToken()); // 시작 위치 x | ||
int y = Integer.parseInt(st.nextToken()); // 시작 위치 y | ||
int d = Integer.parseInt(st.nextToken()); // 시작 방향 d (0: 북, 1: 동, 2: 남, 3: 서) | ||
|
||
// 도로 상태 입력 받기 | ||
grid = new int[n][m]; | ||
visited = new boolean[n][m]; | ||
for (int i = 0; i < n; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
for (int j = 0; j < m; j++) { | ||
grid[i][j] = Integer.parseInt(st.nextToken()); | ||
} | ||
} | ||
int result = simulate(x, y, d); // 방문한 칸의 개수를 계산 | ||
System.out.println(result); | ||
} | ||
public static int simulate(int x, int y, int d) { | ||
visited[x][y] = true; // 현재 위치 방문 표시 | ||
int count = 1; // 방문한 칸의 수 (처음 위치 포함) | ||
|
||
while (true) { | ||
boolean moved = false; | ||
|
||
for (int i = 0; i < 4; i++) { // 4방향 탐색 (왼쪽부터 차례대로 탐색) | ||
d = (d + 3) % 4; // 왼쪽 방향으로 회전 | ||
int nx = x + dx[d]; | ||
int ny = y + dy[d]; | ||
|
||
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && grid[nx][ny] == 0) { // 왼쪽 방향으로 갈 수 있다면 -> 이동 | ||
visited[nx][ny] = true; | ||
x = nx; | ||
y = ny; | ||
count++; // 방문한 칸 증가 | ||
moved = true; | ||
break; // 왼쪽 방향으로 이동했으므로 탐색 종료 | ||
} | ||
} | ||
|
||
if (!moved) { // 4방향 모두 이동할 수 없는 경우, 현재 방향에서 후진 | ||
int backX = x - dx[d]; | ||
int backY = y - dy[d]; | ||
|
||
if (backX >= 0 && backX < n && backY >= 0 && backY < m && grid[backX][backY] == 0) { // 후진 가능 -> 후진 | ||
x = backX; | ||
y = backY; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
return count; // 방문한 칸의 총 개수 반환 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import java.util.Scanner; | ||
|
||
public class HW_불안한_무빙워크 { | ||
public static int MAX_N = 100; | ||
public static int n, k; | ||
public static int[] top = new int[MAX_N * 2]; // 레일 길이가 | ||
public static boolean[] check = new boolean[MAX_N]; // 위쪽 레일에서 사람이 있는지 여부 확인 | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
n = sc.nextInt(); // 레일의 길이 | ||
k = sc.nextInt(); // 안정성이 0인 칸이 k개 이상이면 과정 종료 | ||
|
||
for (int i = 0; i < 2 * n; i++) { // 위쪽과 아래쪽 레일을 한 배열로 입력 받음 | ||
top[i] = sc.nextInt(); | ||
} | ||
|
||
int tryCount = 0; | ||
while (!done()) { | ||
simulate(); | ||
tryCount++; | ||
} | ||
System.out.println(tryCount); | ||
} | ||
|
||
// 무빙워크를 시계 방향으로 한 칸 회전 | ||
public static void shift() { | ||
int tempStability = top[2 * n - 1]; | ||
|
||
// 위쪽 레일과 아래쪽 레일을 한 배열로 관리하기 때문에 전체적으로 한 칸씩 회전 | ||
for (int i = 2 * n - 1; i >= 1; i--) { | ||
top[i] = top[i - 1]; | ||
} | ||
top[0] = tempStability; | ||
|
||
// 사람의 위치도 함께 회전 | ||
for (int i = n - 1; i >= 1; i--) { | ||
check[i] = check[i - 1]; | ||
} | ||
check[0] = false; // 첫 번째 칸에 사람은 없음 | ||
check[n - 1] = false; // 마지막 칸에 사람이 있으면 내림 | ||
} | ||
|
||
// 사람이 현재 위치에서 다음 위치로 이동 가능한지 확인 | ||
public static boolean canGo(int idx) { | ||
return top[idx] > 0 && !check[idx]; // 안정성이 0보다 크고 사람이 없으면 이동 가능 | ||
} | ||
|
||
// 뒤에서부터 사람이 이동할 수 있는지 확인하며 이동 | ||
public static void movePerson() { | ||
for (int i = n - 2; i >= 0; i--) { // 마지막 칸은 사람이 무조건 내려가기 때문에 n-2부터 시작 | ||
if (check[i] && canGo(i + 1)) { | ||
check[i] = false; // 현재 위치에서 사람이 이동 | ||
check[i + 1] = true; // 다음 위치로 이동 | ||
top[i + 1]--; // 이동한 칸의 안정성 감소 | ||
} | ||
} | ||
check[n - 1] = false; // 마지막 칸에 도달한 사람은 내려감 | ||
} | ||
|
||
public static void add() { | ||
if (top[0] > 0 && !check[0]) { | ||
check[0] = true; // 첫 번째 칸에 사람 추가 | ||
top[0]--; // 안정성 1 감소 | ||
} | ||
} | ||
|
||
public static void simulate() { | ||
shift(); // 1. 무빙워크 한 칸 회전 | ||
movePerson(); // 2. 사람 이동 | ||
add(); // 3. 새로운 사람 추가 | ||
} | ||
|
||
// 안정성이 0인 칸이 k개 이상인지를 확인 | ||
public static boolean done() { | ||
int unstableCount = 0; | ||
for (int i = 0; i < 2 * n; i++) { | ||
if (top[i] == 0) { | ||
unstableCount++; | ||
} | ||
} | ||
return unstableCount >= k; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import java.util.*; | ||
|
||
// 시간 복잡도 : topping길이 : 백만 O(NM) X , O(N), O(N logN) ... | ||
// 일반 정렬을 사용하면 시간초과 날 것 | ||
// 롤케이크를 공평하게 자르는 방법의 수 출력 | ||
|
||
// 항상 궁금했던거.. 자바에서는 다른 원소를 어떻게 확인할까? - 중복 제거 : HashSet! | ||
|
||
class Solution { | ||
public int solution(int[] topping) { | ||
int answer = 0; | ||
int N = topping.length; | ||
int left[] = new int[N]; | ||
int right[] = new int[N]; | ||
|
||
HashSet<Integer> leftSet = new HashSet<>(); | ||
HashSet<Integer> rightSet = new HashSet<>(); | ||
for(int i=0; i<N; i++){ | ||
leftSet.add(topping[i]); | ||
left[i] = leftSet.size(); | ||
} | ||
|
||
for(int i=N-1; i>0; i--){ | ||
rightSet.add(topping[i]); | ||
right[i] = rightSet.size(); | ||
} | ||
|
||
|
||
for(int i=0; i<N-1; i++){ | ||
if(left[i] == right[i+1]){ // 철수 0~i / 동생 : i+1 ~ N | ||
answer++; | ||
} | ||
} | ||
|
||
return answer; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.*; | ||
class Solution { | ||
// 뒷큰수 : 신보다 뒤에 있는 숫자 중에서 자신보다 크면서 가장 가까이 있는 수 | ||
// O(N^2) 불가 | ||
public int[] solution(int[] numbers) { | ||
int[] answer = new int[numbers.length]; | ||
Stack<Integer> stack = new Stack<>(); | ||
|
||
int size = numbers.length; | ||
stack.push(0); | ||
|
||
for(int i=1; i<size; i++){ | ||
while(!stack.isEmpty() && numbers[stack.peek()] < numbers[i]){ | ||
answer[stack.pop()] = numbers[i]; | ||
} | ||
stack.push(i); // numbers[stack.peek()] > numbers[i] | ||
} | ||
|
||
while(!stack.empty()){ | ||
answer[stack.pop()] = -1; | ||
} | ||
|
||
return answer; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 .idea 폴더 올라온거 삭제해주실 수 있나요!
머지하면 충돌날 것 같습니당!
https://gmlwjd9405.github.io/2018/05/17/git-delete-incorrect-files.html
위에 블로그 참고했는데, 아래 명령어 입력하면 되는 것 같습니당!
git rm --cached -r .idea/
git commit -m "delete: idea 폴더 삭제"
git push origin main