Skip to content

Commit 709e05b

Browse files
Merge pull request #10 from algorithm-cote-study/yeji/week4
인프런 4주차
2 parents 5ca4645 + 5094688 commit 709e05b

File tree

7 files changed

+304
-0
lines changed

7 files changed

+304
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
public class Solution41 {
9+
// 공주구하기
10+
public static int solution(int length, int target){
11+
int answer = 0;
12+
List<Integer> queue = new ArrayList<>();
13+
List<Integer> result = new ArrayList<>();
14+
int front = 0;
15+
int rear = length-1;
16+
for(int i=1; i<=length; i++){
17+
queue.add( i );
18+
}
19+
20+
int i = 1;
21+
while ( !queue.isEmpty()){
22+
if(i == target){
23+
result.add( queue.get( front ) );
24+
queue.remove( front );
25+
i = 1;
26+
}else{
27+
queue.add(queue.get( front ));
28+
queue.remove( front );
29+
rear++;
30+
i++;
31+
}
32+
}
33+
34+
35+
return result.get( result.size()-1 );
36+
}
37+
38+
public static void main(String[] args){
39+
Scanner sc = new Scanner( System.in);
40+
int[] input = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
41+
System.out.println(solution(input[0],input[1]));
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution42 {
6+
// 교육과정설계
7+
public static String solution(String target, String subjcet){
8+
int index=0;
9+
int[] result = new int[target.length()];
10+
int compare = 0;
11+
while(index<target.length()){
12+
if(compare>=subjcet.length()){
13+
compare = 0;
14+
}
15+
if(!subjcet.contains(String.valueOf( target.charAt( index ) ))){
16+
return "NO";
17+
}
18+
if(target.charAt(index)==subjcet.charAt(compare)){
19+
result[index] = compare;
20+
index++;
21+
}
22+
compare++;
23+
}
24+
int min = result[0];
25+
for(int i : result){
26+
if(min>i) return "NO";
27+
}
28+
return "YES";
29+
}
30+
31+
public static void main(String[] args){
32+
Scanner sc = new Scanner( System.in);
33+
String target = sc.nextLine();
34+
String input = sc.nextLine();
35+
System.out.println(solution(target,input));
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
public class Solution43 {
9+
// 응급실
10+
public static int solution(int n,int target, int[] hwanja){
11+
int answer = 0;
12+
List<Integer> compare = new ArrayList<>();
13+
List<Integer> result = new ArrayList<>();
14+
for(int i =0; i<hwanja.length; i++){
15+
compare.add(i);
16+
}
17+
int front = 0;
18+
int rear = n-1;
19+
while(!compare.isEmpty()){
20+
if(rear == front){
21+
result.add(compare.get(front));
22+
compare.remove(0);
23+
rear = compare.size()-1;
24+
}
25+
if(rear<0) break;
26+
if(hwanja[compare.get(front)]<hwanja[compare.get(rear)]){
27+
int c = compare.get(front);
28+
compare.remove(0);
29+
compare.add(c);
30+
rear = compare.size()-1;
31+
}
32+
rear--;
33+
}
34+
for(int i = 0; i<result.size(); i++){
35+
if(target==result.get( i )){
36+
answer=i+1;
37+
}
38+
}
39+
return answer;
40+
}
41+
42+
public static void main(String[] args){
43+
Scanner sc = new Scanner( System.in);
44+
int[] input1 = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
45+
int[] input2 = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
46+
System.out.println(solution(input1[0],input1[1],input2));
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class Solution44 {
7+
// 선택 정렬
8+
public static void swap(int[] input,int findMinIndex, int index){
9+
int tmp = input[index];
10+
input[index] = input[findMinIndex];
11+
input[findMinIndex] = tmp;
12+
}
13+
14+
public static int[] solution(int n, int[] input){
15+
int index = 0;
16+
while(index < n){
17+
int min = Integer.MAX_VALUE;
18+
int findMinIndex = -1;
19+
for(int i = index; i<n; i++){
20+
if(input[i]<min){
21+
min = input[i];
22+
findMinIndex = i;
23+
}
24+
}
25+
swap(input,findMinIndex,index);
26+
index++;
27+
}
28+
return input;
29+
}
30+
31+
public static void main(String[] args){
32+
Scanner sc = new Scanner( System.in);
33+
int n = sc.nextInt();
34+
sc.nextLine();
35+
int[] input = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
36+
for(int i : solution( n,input )){
37+
System.out.print(i+" ");
38+
}
39+
}
40+
41+
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class Solution45 {
7+
// 버블정렬
8+
public static void swap(int[] input,int findMinIndex, int index){
9+
int tmp = input[index];
10+
input[index] = input[findMinIndex];
11+
input[findMinIndex] = tmp;
12+
}
13+
14+
public static int[] solution(int n, int[] input){
15+
int t = 0;
16+
while(t<n-1){
17+
int last = 0;
18+
for(int j=n-1; j>t; j--){
19+
if(input[j-1]>input[j]){
20+
swap(input,j-1,j);
21+
}
22+
last = j;
23+
}
24+
t = last;
25+
}
26+
return input;
27+
}
28+
29+
public static void main(String[] args){
30+
Scanner sc = new Scanner( System.in);
31+
int n = sc.nextInt();
32+
sc.nextLine();
33+
int[] input = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
34+
for(int i : solution(n,input)) {
35+
System.out.print(i + " ");
36+
}
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class Solution46 {
7+
// 삽입정렬
8+
public static void swap(int[] input,int minIndex, int index){
9+
int tmp = input[minIndex];
10+
input[minIndex] = input[index];
11+
input[index] = tmp;
12+
}
13+
14+
public static int[] solution(int n, int[] input){
15+
int k = 1;
16+
while(k<n){
17+
for(int i=k; i>0; i--){
18+
if(input[i-1]>input[i]){
19+
swap(input,i-1,i);
20+
}
21+
}
22+
k++;
23+
}
24+
return input;
25+
}
26+
27+
public static void main(String[] args){
28+
Scanner sc = new Scanner( System.in);
29+
int n = sc.nextInt();
30+
sc.nextLine();
31+
int[] input = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
32+
for(int i : solution(n,input)) {
33+
System.out.print(i + " ");
34+
}
35+
}
36+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sgyj.inflearn.yeji.week5;
2+
3+
import java.util.Arrays;
4+
import java.util.Scanner;
5+
6+
public class Solution47 {
7+
// Least Recently Used
8+
public static void moveCache(int[] input, int cache){
9+
int first = input[0];
10+
for(int i = 1; i<input.length; i++){
11+
int next = input[i];
12+
input[i] = first;
13+
first = next;
14+
}
15+
input[0] = cache;
16+
}
17+
18+
public static void restoreCache(int[] input, int targetIndex, int cache){
19+
int first = input[0];
20+
for(int i=1; i<=targetIndex; i++){
21+
int next = input[i];
22+
input[i] = first;
23+
first = next;
24+
}
25+
input[0] = cache;
26+
}
27+
28+
public static int[] solution(int n, int[] input){
29+
int[] result = new int[n];
30+
int target=0;
31+
int i = 0;
32+
while ( i< input.length ){
33+
// 해야할 작업이 캐시에 없음
34+
if(target == n-1){
35+
target=0;
36+
moveCache(result,input[i]);
37+
i++;
38+
continue;
39+
}
40+
if(result[target]==input[i]){
41+
restoreCache(result,target,result[target]);
42+
i++;
43+
target=0;
44+
}
45+
if(result[target]!=input[i]){
46+
target++;
47+
}
48+
}
49+
return result;
50+
}
51+
52+
public static void main(String[] args){
53+
Scanner sc = new Scanner( System.in);
54+
int[] input1 = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
55+
int[] input2 = Arrays.stream( sc.nextLine().split( " ")).mapToInt( Integer::parseInt).toArray();
56+
for(int i : solution(input1[0],input2)){
57+
System.out.print(i+" ");
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)