HYEWON CHOI : PROGRAMMERS Lv3 다단계 칫솔 판매 #370
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Programmers - 다단계 칫솔 판매
-다단계 칫솔 판매 문제 바로가기
풀이
먼저 자식과 부모를 담는 hashmap과 자신과 순서를 담는 hashmap을 만들어주었습니다.
그리고 seller의 배열을 순차적으로 돌면서 추천인이 "-" 이면 break, 아닐경우 현재 추천인이 본인이 되어 다시 반복문을 돌며 칫솔의 판매값을 누적해서 합해주었습니다.
이 때 자신이 가져야할 돈은 추천인에게 10%를 떼고 남은 돈이므로 amount[자신의 인덱스값] - (amount[자신의 인덱스값]/10)를 더해주면 됩니다.
막힌점
처음에 11~13에서 시간초과가 나왔습니다. 다른 사람의 코드를 참고하며 보니 돈을 10%씩 추천인에게 주다보면 돈이 나누어 지지 않는 경우를 고려하지 않아서 생기는 문제였습니다. 따라서 while문 안에 money가 1보다 작은 경우를 break를 해주어 이를 해결했습니다.
소스코드
결과