[LeetCode][Python3] 526. Beautiful Arrangement
2019. 8. 21. 01:56 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/beautiful-arrangement/
My Solution :
class Solution:
def countArrangement(self, N):
def dfs():
if not nums:
self.ans += 1
return
i = 1 + N - len(nums)
for n in nums:
if i % n == 0 or n % i == 0:
nums.remove(n)
dfs()
nums.add(n)
self.ans = 0
nums = set(range(1, N+1))
dfs()
return self.ans
My Solution 2 :
class Solution:
def countArrangement(self, N):
def dfs(i):
if i == 0:
self.ans += 1
return
for j in range(1, N+1):
if not visited[j] and (j % i == 0 or i % j == 0):
visited[j] = 1
dfs(i-1)
visited[j] = 0
self.ans = 0
visited = [0]*(N+1)
dfs(N)
return self.ans
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 77. Combinations (0) | 2019.08.25 |
---|---|
[LeetCode][Python3] 39. Combination Sum (0) | 2019.08.23 |
[LeetCode][Python3] 216. Combination Sum III (0) | 2019.08.23 |
[LeetCode][Python3] 52. N-Queens II (0) | 2019.08.23 |
[LeetCode][Python3] 784. Letter Case Permutation (0) | 2019.08.20 |
[LeetCode][Python3] 980. Unique Paths III (0) | 2019.08.20 |
[LeetCode][Python3] 47. Permutations II (0) | 2019.08.18 |
[LeetCode][Python3] 1079. Letter Tile Possibilities (0) | 2019.08.16 |
최근에 달린 댓글 최근에 달린 댓글