This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-10-06] sumin #301 #331
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| """ | ||
| 풀이시간: 20분 | ||
|
|
||
| <input> | ||
| topping: 롤케이크에 올려진 토핑들의 번호를 저장한 정수배열(1 ≤ topping의 길이 ≤ 1,000,000) | ||
| - 1 ≤ topping의 원소 ≤ 10,000 | ||
|
|
||
| <solution> | ||
| 전체 토핑의 개수와 토핑의 가짓수에서 철수가 먹게되는 토핑의 개수와 가짓수를 계속해서 갱신하며 답을 찾아나간다. | ||
| 이 때, set 자료형을 써서 철수가 먹게되는 토핑의 가짓수를 O(1)에 탐색한다. | ||
|
|
||
| <시간복잡도> | ||
| O(N) | ||
| """ | ||
| from typing import List | ||
| from collections import Counter | ||
|
|
||
|
|
||
| def solution(topping: List) -> int: | ||
| """ | ||
| topping: 롤케이크에 올려진 토핑들의 번호를 저장한 정수 배열 | ||
| """ | ||
| answer = 0 # 롤케이크를 공평하게 자르는 방법의 수 | ||
| tot = Counter(topping) # 각 토핑의 개수 | ||
| tot_cnt = len(tot) # 전체 토핑의 가짓수 | ||
|
|
||
| # 철수가 먹게되는 토핑의 종류 | ||
| eat = set() | ||
| eat_cnt = 0 | ||
| for x in topping: | ||
| # 기존에 없는 토핑인 경우 -> 토핑을 set에 추가하고, 먹게되는 토핑의 가짓수도 증가 | ||
| if x not in eat: | ||
| eat.add(x) | ||
| eat_cnt += 1 | ||
| # 전체 토핑에서 철수가 먹게되는 토핑의 개수 1 감소 | ||
| tot[x] -= 1 | ||
| if tot[x] == 0: # 만약 해당 토핑을 모두 철수가 먹게된다면 동생이 먹는 토핑의 가짓수 1 감소 | ||
| tot_cnt -= 1 | ||
| if eat_cnt == tot_cnt: # 철수가 먹는 토핑의 가짓수와 동생이 먹는 토핑의 가짓수가 같아지면 경우의 수 1 증가 | ||
| answer += 1 | ||
| elif eat_cnt > tot_cnt: # 철수가 먹는 가짓수가 더 커진다면 더 이상 확인할 필요 없으니 반복문 종료 | ||
| break | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
| def main() -> None: | ||
| case1 = [1, 2, 1, 3, 1, 4, 1, 2] | ||
| case2 = [1, 2, 3, 1, 4] | ||
| print(solution(case1)) # 2 | ||
| print(solution(case2)) # 0 | ||
|
|
||
| main() | ||
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.
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.
이 경우를 별도의 카운트 변수를 두지 않고, 카운터의 key를 remove하는 방식으로 해도 좋을 것 같아요! 그러면 len으로 비교가 가능할 수 있습니다.