Skip to content
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
216 changes: 216 additions & 0 deletions 4주차/42586/42586_김지수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Solution 1
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {

int[] daysLeft = new int[speeds.length];

for (int i = 0; i < speeds.length; i++) {
// 자바에서 정수끼리 나눌 때 소수점 아래 버려지는 것 주의!
daysLeft[i] = (int) Math.ceil( (double) (100 - progresses[i]) / speeds[i] );
System.out.println(daysLeft[i]);
}

Stack<Integer> answerList = new Stack<>();
int max = 0;

for (int i = 0; i < daysLeft.length; i++) {
if (max < daysLeft[i]) {
max = daysLeft[i];
answerList.push(1);
continue;
} else if (max >= daysLeft[i]) {
if (!answerList.isEmpty()) {
int lastCnt = answerList.pop();
lastCnt++;
answerList.push(lastCnt);
}
}
}

int[] answer = new int[answerList.size()];
for (int i = 0; i < answerList.size(); i++) {
answer[i] = answerList.get(i);
}

return answer;
}
}


// Solution 2
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {

Queue<Integer> q = new LinkedList<>();
List<Integer> answerList = new ArrayList<>();

for (int i = 0; i < speeds.length; i++) {
double remain = (100 - progresses[i]) / (double) speeds[i];
int date = (int) Math.ceil(remain);

if (!q.isEmpty() && q.peek() < date) {
answerList.add(q.size());
q.clear();
}

q.offer(date);
}

answerList.add(q.size());

int[] answer = new int[answerList.size()];

for (int i = 0; i < answer.length; i++) {
answer[i] = answerList.get(i);
}

return answer;
}
}


// Solution 3
import java.util.*;
import java.util.stream.*;

class Solution {
public int[] solution(int[] progresses, int[] speeds) {
int[] arr = new int[progresses.length];
int length = progresses.length;

for(int i=0; i < length; i++) {
arr[i] = (int)Math.ceil((100 - progresses[i]) / (double)speeds[i]);
}

List<Integer> list = new ArrayList<>();
int pre = arr[0];
int idx = 0;

for(int i=1; i < length; i++) {
if(arr[i] <= pre) {
continue;
}

list.add(i - idx);
pre = arr[i];
idx = i;
}

list.add(length - idx);

return list.stream().mapToInt(i->i).toArray();
}
}


// Solution 4 모듈화 코드인데..어렵다..
import java.lang.System;
import java.lang.Math;
import java.util.ArrayList;
class Solution {

int progressesCount;
int[] needDays;

ArrayList<Integer> workCountStorage;

public int[] solution(int[] progresses, int[] speeds) {

//Init
progressesCount = progresses.length;
needDays = new int[progressesCount];
workCountStorage = new ArrayList<>();


//필요한 작업일 계산
this.calcNeedDays(progresses, speeds);

//this.viewAll(needDays, 0);


//동시에 진행된 프로세스 계산
for (int step = 0; step < progressesCount; ) {
int stepNeedDay = needDays[step];

//날짜 동시에 경과
for (int remainStep = step; remainStep < progressesCount; remainStep++) {
needDays[remainStep] -= stepNeedDay;
}

//this.viewAll(needDays, step);

//완료한 작업까지의 갯수
int workCount = 1;
for ( ; step + workCount < progressesCount; workCount++) {
if (needDays[step + workCount] > 0) break;
}

System.out.println("workCount:" + workCount);

//완료한 작업 갯수 저장
workCountStorage.add(workCount);

//작업 갯수만큼 step 증가
step += workCount;

}

//int[] answer = {};
int[] answer = Solution.convertIntegers(workCountStorage);
return answer;
}

private void calcNeedDays(int[] progresses, int[] speeds) {
for (int i = 0; i < progressesCount; i++) {
double remainProgress = 100 - progresses[i];
double fNeedDay = remainProgress / speeds[i];

needDays[i] = (int) Math.ceil(fNeedDay);
}
}

public static int[] convertIntegers(ArrayList<Integer> integers) {
int size = integers.size();
int[] ret = new int[size];
for (int i = 0; i < size; i++) {
ret[i] = integers.get(i).intValue();
}
return ret;
}

private void viewAll(int[] array, int startIdx) {
System.out.print("viewAll:");

int arrayCount = array.length;
for(int i = startIdx; i < arrayCount; i++) {
System.out.print(array[i]+",");
}

System.out.println();
}
}


// Solution 5
import java.util.*;
class Solution {
public int[] solution(int[] progresses, int[] speeds) {
Queue<Integer> works = new LinkedList<>();
List<Integer> result = new ArrayList<>();
for (int i=0; i<progresses.length; i++){
works.offer((int)Math.ceil((double)(100 - progresses[i]) / speeds[i]));
}
while(!works.isEmpty()){
int current = works.poll();
int cnt=1;
while(!works.isEmpty()&&works.peek()<=current){
cnt++;
works.poll();
}
result.add(cnt);
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
}
63 changes: 63 additions & 0 deletions 4주차/42885/42885_김지수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Solution 1
import java.util.*;
class Solution {
public int solution(int[] people, int limit) {
Arrays.sort(people);

int start = 0;
int end = people.length - 1;
int answer = 0;

while (start <= end) {
if (people[start] + people[end] <= limit) {
answer++;
start++;
end--;
} else {
answer++;
end--;
}
}

return answer;
}
}


// Solution 2
import java.util.Arrays;
class Solution {
public int solution(int[] people, int limit) {
Arrays.sort(people);
int i = 0, j = people.length - 1;

for ( ; i < j; --j) {
if (people[i] + people[j] <= limit)
++i;
}

return people.length - i;
// 사람 수 - 두 명 태우는 경우의 수 => 한 명 태우는 경우의 수 + 두 명 태우는 경우의 수
}
}


// Solution 3
import java.util.*;
class Solution {
public int solution(int[] people, int limit) {
int answer = 0;
Arrays.sort(people);
int j = 0;
for (int i = people.length - 1; i >= 0; i--) {
if (j > i) {
break;
}
if (people[i] + people[j] <= limit) {
j++;
}
answer++;
}
return answer;
}
}
62 changes: 62 additions & 0 deletions 4주차/43165/43165_김지수.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Solution 1
class Solution {
int answer = 0;

public int solution(int[] numbers, int target) {
dfs(numbers, 0, target, 0);
return answer;
}

public void dfs(int[] numbers, int depth, int target, int sum) {
if(depth == numbers.length) {
if (target == sum) answer++;
} else {
dfs(numbers, depth + 1, target, sum + numbers[depth]);
dfs(numbers, depth + 1, target, sum - numbers[depth]);
}
}
}


// Solution 2 이해 어려우니까 다시 보기
class Solution {
public int solution(int[] numbers, int target) {
int answer = 0;
answer = dfs(numbers, 0, 0, target);
return answer;
}

int dfs(int[] numbers, int n, int sum, int target) {
if(n == numbers.length) {
if(sum == target) {
return 1;
}
return 0;
}
return dfs(numbers, n + 1, sum + numbers[n], target) + dfs(numbers, n + 1, sum - numbers[n], target);
}
}


// Solution 3
class Solution {
static int goal;
static int answer=0;
static int[] arr;
public int solution(int[] numbers, int target) {
arr = numbers;
goal = target;
dfs(0, 0, numbers);
return answer;
}
private static void dfs(int depth, int sum, int[] numbers) {
if (depth == numbers.length) {
if (sum == goal) {
answer++;
}
return;
}
dfs(depth + 1, sum + arr[depth], numbers);
dfs(depth + 1, sum - arr[depth], numbers);
}
}
Loading