Skip to content

Commit

Permalink
Merge pull request #1 from hepheir/main
Browse files Browse the repository at this point in the history
[01주차 김동주] 소트
  • Loading branch information
hepheir authored Oct 21, 2023
2 parents 2cfbcf8 + 3ccc08b commit ad12c17
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 ad12c17

Please sign in to comment.