-
Notifications
You must be signed in to change notification settings - Fork 0
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
[위상 정렬] 6월 20일 #17
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "06\uC6D4-07\uC77C---\uC704\uC0C1-\uC815\uB82C"
[위상 정렬] 6월 20일 #17
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.
안녕하세요 예진님! 항상 열심히, 클린코드로 제출해주신 덕분에 예진님의 코드를 감상하고는 했습니다.. 😀 그저 빛..*
이번에도 너무 잘 작성해주신 덕분에 크게 피드백 드릴 부분이 없었습니다..!!
최종적으로 필수 2문제, 선택 3문제 확인했습니다!
머지 해주셔도 좋구, 수정하신 부분 있으시면 리뷰어로 불러주셔도 좋습니다!
마지막까지 열심히!!!!! 해주셔서 너무 감사드립니다!!! 수고 너무너무 많으셨고, 앞으로도 화이팅입니다!! 🔥
감사합니다. 🤗
vector<int> neg_nums; //음수와 0 저장 | ||
vector<int> pos_nums; //1보다 큰 양수 저장 |
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.
최고에요🔥🔥!
//음수 연산 | ||
for (int i = 0; i < neg_nums.size(); i++) { | ||
if (neg_nums[i] == 0) break; //이후 남은 수도 0, 반복 종료(0은 더해도 영향x 무시) | ||
|
||
if (i == neg_nums.size() - 1) { //짝이 없는 마지막 수는 그냥 더함 | ||
ans += neg_nums[i]; | ||
break; | ||
} | ||
|
||
//마지막 수가 아니면 다음 수와 곱하여 더함 | ||
ans += neg_nums[i] * neg_nums[i + 1]; | ||
i++; //다다음 수 탐색 | ||
} | ||
|
||
//양수 연산 | ||
for (int i = 0; i < pos_nums.size(); i++) { | ||
if (i == pos_nums.size() - 1) { //마지막 수인 경우 | ||
ans += pos_nums[i]; | ||
break; | ||
} | ||
|
||
//마지막 수가 아니면 다음 수와 곱하여 더함 | ||
ans += pos_nums[i] * pos_nums[i + 1]; | ||
i++; //다다음 수 탐색 | ||
} | ||
|
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.
p2. 중복되는 부분이 있는 것 같아요!
연산 부분은 함수로 빼주셔서 중복 코드를 줄이는 것도 좋을 것 같습니다 🙂🙂
using namespace std; | ||
typedef pair<int, int> ci; | ||
|
||
map<int, int> topologicalSort(int n, vector<int> &indegree, vector<vector<ci>> &graph) { |
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.
p3. map<int, int>
처럼 자주 쓰이는 건 편리하게 쓸 수 있도록 typedef pair<int, int> ci;
와 같이 typedef map<int, int> mi;
이런 식으로 정의하셔서 사용하시는 것을 추천 드립니다!
|
||
map<int, int> topologicalSort(int n, vector<int> &indegree, vector<vector<ci>> &graph) { | ||
queue<int> que; //진입차수 0인 노드 | ||
vector<map<int, int>> dp(n + 1); //기본부품 필요 개수 저장 |
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.
p2. map은 value에 바로 접근할 수 있어 충분히 좋은 STL이지만, vector보다 메모리를 많이 차지하기 때문에 key값이 사람의 이름처럼 int형이 아닐 때 등에 사용하는 것이 좋습니다!
따라서, 여기서는 부품의 번호 혹은 완제품의 번호가 int형이기 때문에 2차원 vector로 dp를 구현해주실 수 있을 것 같습니다 🤗🤗
while (m--) { | ||
cin >> x >> y >> k; //x 생산에 y가 k개 필요 | ||
graph[y].push_back({x, k}); | ||
indegree[x]++; | ||
} |
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.
p2.
x 생산에 y가 k개 필요하기에 위상정렬을 y->x 로 구현해주셨군요! 좋아요 🔥🔥
그런데, y(필요한 부품) -> x(만들어지는 부품) 의 관계를 역으로 만드는 건 어떨까요? 그러면, 루트노드가 완제품이 되고 리프노드가 기본 부품이 되겠네요!
이 문제는 루트노드가 완제품이 되고 리프노드가 기본 부품이 되어, 결국 BFS/DFS 탐색을 통해 필요한 리프노드의 개수를 구하는 문제가 됩니다! 따라서 리프노드의 개수만 구하면 되기 때문에 위상정렬 할 필요가 없게 됩니다. 샘플코드를 참고해주셔도 좋을 것 같습니다!
물론 위상정렬을 하게 된다면 큐에 중복 정점이 들어가지 않기 때문에 탐색 시간을 줄일 수 있다는 장점이 있습니다 🤗!
|
||
//카드 뒤집을 순서 순열 만들어 조작 횟수 연산 | ||
do { | ||
backtracking(board, image_list, 0, 0, {r, c}); //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.
p2. 0, 1번째 카드 중 어떤 카드를 먼저 뒤집을지 결정할 때 중복되는 코드가 많아 backtracking 함수로 빼주셨군요! 좋아요!
그런데, 비트연산자를 이용하여 더 간단하게 구현할 수도 있습니다! 샘플코드 참고해주시면 좋을 것 같아요 🙂🙂!!
내용 & 질문
<기존 제출>