-
Notifications
You must be signed in to change notification settings - Fork 5
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
8-wonjunYou #168
8-wonjunYou #168
Conversation
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.
์ ๋ ๊ณผ์ ๋ฅผ ์์ ์๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ณ ์คํ์ ๋ฃ์ผ๋ฉด์
- ํ์ฌ ๊ณผ์ ๋ฅผ stack์ ๋ฃ๊ณ
- ๋ฐ๋ณต๋ฌธ์ ํตํด ํ๊ณ ์๋ ๊ณผ์ ๋ฅผ ๋น๊ตํด์ ๋จ์ ์๊ฐ๊ณผ ๊ณผ์ ํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ๋น๊ตํ ํ,
- ๋ค์ ๊ณผ์ ๊น์ง ๋จ์ ์๊ฐ์ด ๊ณผ์ ํ๋๋ฐ ๊ฑธ๋ฆฐ ์๊ฐ๋ณด๋ค ๋ง์ผ๋ฉด ๊ทธ๋งํผ ๋นผ๊ณ answer์๋ ๊ณผ์ ์ ์ด๋ฆ์ ์ถ๊ฐํ๊ณ
- ๊ณผ์ ๋ฆฌ์คํธ์ ๊ณผ์ ์ด๋ฆ๊ณผ ๋จ์ ๊ณผ์ ์๊ฐ์ ์ ์ฅํ๋
๋ก์ง์ ๋ฐํ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ์์ต๋๋ค.
์์ค๋์ ์๋์ฝ๋ ๋๋ถ์ ๋ก์ง์ ๋ ๊น์ํ๊ฒ ์ ๋ฆฌํ ์ ์์๋ค์๐
์ด๋ฒ PR๋ ์๊ณ ํ์ จ์ต๋๋ค!
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.
๋ณต๊ท ํ์
จ๊ตฐ์ ๐๐๐
์ ๋ ๋น์ทํ๊ฒ ํ์์ต๋๋ค! ์ด๋ฒ ์ฐจ์๋ ์๊ณ ํ์
จ์ด์
import java.util.*;
class Solution {
// ์๊ฐ์ "HH:MM" ํ์์์ ๋ถ ๋จ์๋ก ๋ณํํ๋ ํจ์
private int timeToMinutes(String time) {
String[] parts = time.split(":");
return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]);
}
public String[] solution(String[][] plans) {
// ๊ณํ์ ์์ ์๊ฐ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
Arrays.sort(plans, new Comparator<String[]>() {
@Override
public int compare(String[] a, String[] b) {
return timeToMinutes(a[1]) - timeToMinutes(b[1]);
}
});
Stack<String[]> stack = new Stack<>();
List<String> completedTasks = new ArrayList<>();
int currentTime = timeToMinutes(plans[0][1]);
int i = 0;
while (i < plans.length || !stack.isEmpty()) {
if (i < plans.length) {
String[] currentTask = plans[i];
int startTime = timeToMinutes(currentTask[1]);
// ์ด์ ๊ณผ์ ๋ฅผ ์๋ฃํ์ง ๋ชปํ์ผ๋ฉด ์คํ์ ์ ์ฅํ๊ณ ์๋ก์ด ๊ณผ์ ์์
while (!stack.isEmpty() && currentTime + Integer.parseInt(stack.peek()[2]) <= startTime) {
String[] pausedTask = stack.pop();
currentTime += Integer.parseInt(pausedTask[2]);
completedTasks.add(pausedTask[0]); // ๊ณผ์ ์๋ฃ ๊ธฐ๋ก
}
// ํ์ฌ ์๊ฐ์ ์๋ก์ด ๊ณผ์ ๋ฅผ ์์
if (currentTime <= startTime) {
currentTime = startTime;
stack.push(new String[]{currentTask[0], currentTask[1], currentTask[2]});
i++;
} else {
// ์๊ฐ์ด ๊ฒน์น๋ฉด ๋จ์ ๊ณผ์ ์๊ฐ์ ์
๋ฐ์ดํธํ์ฌ ์คํ์ ์ ์ฅ
String[] pausedTask = stack.pop();
int remainingTime = Integer.parseInt(pausedTask[2]) - (startTime - currentTime);
stack.push(new String[]{pausedTask[0], pausedTask[1], String.valueOf(remainingTime)});
currentTime = startTime;
stack.push(new String[]{currentTask[0], currentTask[1], currentTask[2]});
i++;
}
}
// ๋จ์ ๊ณผ์ ๋ฅผ ์ฒ๋ฆฌ
if (i >= plans.length) {
while (!stack.isEmpty()) {
String[] pausedTask = stack.pop();
currentTime += Integer.parseInt(pausedTask[2]);
completedTasks.add(pausedTask[0]);
}
}
}
return completedTasks.toArray(new String[0]);
}
}
๐ ๋ฌธ์ ๋งํฌ
๊ณผ์ ์งํํ๊ธฐ
๋ฌธ์ ์ค๋ช
๊ณผ์ ๋ฅผ ๋ฐ์ ๋ฃจ๋ ๋ค์๊ณผ ๊ฐ์ ์์๋๋ก ๊ณผ์ ๋ฅผ ์งํํฉ๋๋ค.
๊ณผ์ ๋ฅผ ๋๋ธ ์์๋๋ก ์ด๋ฆ์ ๋ฐฐ์ด์ ๋ด์ return ํ๋ solution์ ๊ตฌํ๋ผ
์ ๋ ฅ๊ฐ
3 โค plans์ ๊ธธ์ด โค 1,000
plans์ ์์๋ [name, start, playtime]์ ๊ตฌ์กฐ๋ก ์ด๋ฃจ์ด์ ธ ์์ต๋๋ค.
-> "hh:mm"์ ํํ๋ก "00:00" ~ "23:59" ์ฌ์ด์ ์๊ฐ๊ฐ๋ง ๋ค์ด๊ฐ ์์ต๋๋ค.
-> ๋ชจ๋ ๊ณผ์ ์ ์์ ์๊ฐ์ ๋ฌ๋ผ์ ๊ฒน์น ์ผ์ด ์์ต๋๋ค.
โ๏ธ ์์๋ ์๊ฐ
25๋ถ
โจ ์๋ ์ฝ๋
์์ ํ๋ก๊ทธ๋๋จธ์ค lv2 ~ 3 ์ค์ ์๋ฌด๊ฑฐ๋ ๋ถ์ก๊ณ ํ๊ธฐ ์์ํ์ต๋๋ค! ์๊ฐ๋ณด๋ค ์ฌ์ด ๋ฌธ์ ์์ง๋ง PR ์ง๋๋ฅผ ์ซ์๊ฐ๊ธฐ ์ํด ๊ทธ๋ฅ ํ๋ฒ ์ ์ด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค..!
์ผ๋จ ๊ณผ์ ์งํ ์์๋ฅผ ๋ณด๋ฉด
์๊ฐ์ ๋ง๊ฒ ์งํ์ ํ๋, ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ก์ด ๊ณผ์ -> ์๊ฐ์ด ๋จ๋ฉด ์ต๊ทผ์ ๋ฉ์ถ ๊ณผ์ ์์๋๋ก ์งํํ๋ ๊ฒ์ ์ ์ ์์ต๋๋ค.
ํด๋น ๋๋ชฉ์์ ์คํ์์ ๋ฐ๋ก ์บ์นํ ์ ์์์ต๋๋ค.
์ดํ ํฌ๊ฒ ๋๊ฐ์ง ์ผ์ด์ค๋ก ๋๋ ์ ์์ต๋๋ค.
-> ์๊ฐ์ด ๋ถ์กฑํ๋ฏ๋ก ํด๋น ๊ณผ์ ๋ ๋จ๋ ์๊ฐ๋งํผ stack์ ๋ฃ์ต๋๋ค.
๋ง์ฝ ๊ธฐ์กด ๊ณผ์ ํ๋๋ฅผ ์ํํ์ง ๋ชปํ ๊ฒฝ์ฐ ์์ฌ ์๊ฐ์ ๊ณ์ฐ ํ ์คํ์ ๋ค์ ๋ฃ์ด์ฃผ๋ ๊ณผ์ ๋ ์์ง ์์์ผ ํฉ๋๋ค!
์ต์ข ์ฝ๋