{"type": "session", "harness": "opencode", "id": "cp-10033", "name": "LeetCode 0526: Beautiful Arrangement"} {"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}} {"type": "message", "message": {"role": "user", "content": "Problem: Beautiful Arrangement (Difficulty: Medium)\nTags: Array, Dynamic Programming, Backtracking, Bit Manipulation, Bitmask\n\nSuppose you have n integers labeled 1 through n. A permutation of those n integers perm (1-indexed) is considered a beautiful arrangement if for every i (1 <= i <= n), either of the following is true:\nGiven an integer n, return the number of the beautiful arrangements that you can construct.\n\nExample 1:\nInput: n = 2\nOutput: 2\nExplanation: \nThe first beautiful arrangement is [1,2]:\n - perm[1] = 1 is divisible by i = 1\n - perm[2] = 2 is divisible by i = 2\nThe second beautiful arrangement is [2,1]:\n - perm[1] = 2 is divisible by i = 1\n - i = 2 is divisible by perm[2] = 1\n\nExample 2:\nInput: n = 1\nOutput: 1\n\nConstraints:\n- 1 <= n <= 15"}} {"type": "message", "message": {"role": "assistant", "content": "# Time: O(n!)\n# Space: O(n)\n\n\nclass Solution(object):\n def countArrangement(self, N):\n \"\"\"\n :type N: int\n :rtype: int\n \"\"\"\n def countArrangementHelper(n, arr):\n if n <= 0:\n return 1\n count = 0\n for i in range(n):\n if arr[i] % n == 0 or n % arr[i] == 0:\n arr[i], arr[n-1] = arr[n-1], arr[i]\n count += countArrangementHelper(n - 1, arr)\n arr[i], arr[n-1] = arr[n-1], arr[i]\n return count\n\n return countArrangementHelper(N, range(1, N+1))\n\n"}}