Skip to content

Commit

Permalink
백준 1071번 : 소트 풀이 (그리디)
Browse files Browse the repository at this point in the history
  • Loading branch information
hepheir committed Oct 9, 2023
1 parent 99f2143 commit 2af5478
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 01주차/김동주/1071.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MAX_NUM = 1000


def solve(N: int, A: list[int]):
counter = [0] * (MAX_NUM+1)
for a in A:
counter[a] += 1
build(N, counter)


def build(N: int, counter: list[int], stack: list[int] = []):
if len(stack) == N:
print(' '.join(map(str, stack)))
exit()
for n in generate_possible_numbers(counter, stack):
stack.append(n)
counter[n] -= 1
build(N, counter, stack)
counter[n] += 1
stack.pop()


def generate_possible_numbers(counter: list[int], stack: list[int]):
for i in range(MAX_NUM+1):
if counter[i] > 0 and (not stack or i != stack[-1]+1):
yield i


if __name__ == "__main__":
N = int(input())
A = list(map(int, input().split()))
solve(N, A)

0 comments on commit 2af5478

Please sign in to comment.