Skip to content
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

[01주차 이윤호] 준석이의 사탕 사기 #6

Merged
merged 2 commits into from
Oct 21, 2023

Conversation

leeyh1011
Copy link
Contributor

🔎 문제 소개

🖊️ 풀이

  • 시간 복잡도: $O(n)$
  • 입력의 크기:
    • $N \leq 1,000$

리스트에 들어가는 요소들의 합이 짝수와 홀수일 때로 경우의 수를 나누었다.
요소들의 합이 짝수일 때는 요소들의 합만 출력하면 됬지만, 홀수일 때는 요소 중 가장 작은 홀수를 골라서 빼준 다음, 요소들의 합을 짝수로 만들어야 했다.
요소들의 합이 홀수일 때, 홀수로만 리스트가 이루어져 있다면 0을 출력해야 했다.

리스트에서 push, pop 등으로 특정 위치의 요소들만 찾고 계산하는 것까지를 주로 해보았는데, 가장 작은 홀수를 찾는 것은 신선하고 재밌었다.

Comment on lines +4 to +6
candy_max_num = sum(candy_num)

if (candy_max_num % 2 != 0):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

처음에 모든 사탕의 수를 합해서 전체의 개수가 홀수인지 짝수인지를 찾고 시작했군요! 신선하네요

Copy link
Member

@hepheir hepheir Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확실히 처음에 모든 사탕 수를 세었어도 괜찮았겠네요!

저는 입력 받아가면서 그리디로 풀고 마지막에 예외처리 하는 방식인데, 이 풀이와 비교해보면 재밌는 것 같습니다.

풀이 보기...
# 사탕 묶음에 들어갈 수 있는 최대 수가 1000 이므로,
# 나올 수 없는 수인 1001을 infinity 를 의미하는 수로 사용.
INF = 1001


ans = 0 # 가져갈 사탕의 개수

# 홀수 개의 사탕을 가진 사탕 묶음...
min_odd = INF # ...중에서 가장 적은 수의 사탕을 가진 사탕 묶음 속 사탕의 개수
n_odds = 0 # ...의 개수

# 사탕 묶음의 개수는 필요 없으므로 버림
input()

# 각 사탕 묶음 속 사탕의 개수 a에 대해 살펴본다.
for a in map(int, input().split()):
    if a % 2 == 1:
        n_odds += 1
        # 홀수개의 사탕 중 가장 작은 것을 찾음
        if a < min_odd:
            min_odd = a
    ans += a # 일단은 모든 사탕을 가져간다고 전제.


# 홀수개의 사탕 묶음이 홀수 개이면, 짝수개로 짝수를 만들고 남은 나머지 하나만 버리면 됨.
if n_odds % 2 == 1 and min_odd != INF:
    ans -= min_odd


print(ans)

Copy link
Member

@Eunji1217 Eunji1217 Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 처음에는 단순히 짝수는 다 더하고 제일 큰 홀수개끼리 연산한다고 생각했는데 생각해보니 합이 홀수일 경우에만 가장작은 홀수 값을 빼면 되네요 깨달음!!!!

Comment on lines +4 to +6
candy_max_num = sum(candy_num)

if (candy_max_num % 2 != 0):
Copy link
Member

@Eunji1217 Eunji1217 Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 처음에는 단순히 짝수는 다 더하고 제일 큰 홀수개끼리 연산한다고 생각했는데 생각해보니 합이 홀수일 경우에만 가장작은 홀수 값을 빼면 되네요 깨달음!!!!

@hepheir hepheir merged commit 367c846 into dsa-master:main Oct 21, 2023
1 of 2 checks passed
hepheir pushed a commit that referenced this pull request Oct 21, 2023
* Create 이윤호

* 01주차 과제
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants