【LeetCode】526. Beautiful Arrangement【M】【35】【回溯】

175 篇文章 0 订阅
157 篇文章 0 订阅


Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:

  1. The number at the ith position is divisible by i.
  2. i is divisible by the number at the ith position.

Now given N, how many beautiful arrangements can you construct?

Example 1:

Input: 2
Output: 2
Explanation: 

The first beautiful arrangement is [1, 2]:
Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).
The second beautiful arrangement is [2, 1]:
Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

Note:

  1. N is a positive integer and will not exceed 15.

Subscribe to see which companies asked this question.


对每个位置,判断是不是能够整除

现在问题是,被注释掉的只是一个普通的广搜,并是不回溯,n=10的时候,超时了

后面是回溯的算法。

现在的一个需要注意的是,对于回溯问题,有一种方式就是,其中是否访问过,可以用visited标记,而不是用数字一个个塞进去



class Solution(object):
    def countArrangement(self, N):
            
        '''        
        def check(num):
            #print 'check',num
            for i in range(len(num)):
                if num[i] % (i+1) != 0 and (i+1) % num[i] != 0:
                    return 0
            return 1

        def bt(pos,res,n,num):
            if pos == n:
                res += check(num)
                return res
            for i in xrange(0,pos+1):
                temp = num[:i]+[pos+1]+num[i:]
                res = bt(pos+1,res,n,temp)
            return res
        return bt(1,0,N,[1])
        '''
        
        
        self.res = 0
        def bt(N,pos,visited):
            if pos > N:
                self.res += 1
                #print self.res
                return 
            for i in xrange(1,N+1):
                #print i,N+1
                if visited[i] == 0 and (i % pos == 0 or pos % i == 0):
                    #print i,N,pos,visited,self.res
                    visited[i] = 1
                    bt(N,pos+1,visited)
                    visited[i] = 0
            
            
            
        bt(N,1,[0]*(N+1))
        return self.res
        
        
        
        
        """
        :type N: int
        :rtype: int
        """


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个只包含正整数的非空数组,是否可以将这个数组分成两个子集,使得两个子集的元素和相等。 示例: 输入:[1, 5, 11, 5] 输出:true 解释:数组可以分割成 [1, 5, 5] 和 [11]。 解题思路: 这是一道经典的 0-1 背包问题,可以使用动态规划或者回溯算法解决。 使用回溯算法,需要定义一个 backtrack 函数,该函数有三个参数: - 数组 nums; - 当前处理到的数组下标 index; - 当前已经选择的元素和 leftSum。 回溯过程中,如果 leftSum 等于数组元素和的一半,那么就可以直接返回 true。如果 leftSum 大于数组元素和的一半,那么就可以直接返回 false。如果 index 到达数组末尾,那么就可以直接返回 false。 否则,就对于当前元素,有选择和不选择两种情况。如果选择当前元素,那么 leftSum 就加上当前元素的值,index 就加 1。如果不选择当前元素,那么 leftSum 不变,index 也加 1。最终返回所有可能性的结果中是否有 true。 Java 代码实现: class Solution { public boolean canPartition(int[] nums) { int sum = 0; for (int num : nums) { sum += num; } if (sum % 2 != 0) { return false; } Arrays.sort(nums); return backtrack(nums, nums.length - 1, sum / 2); } private boolean backtrack(int[] nums, int index, int leftSum) { if (leftSum == 0) { return true; } if (leftSum < 0 || index < 0 || leftSum < nums[index]) { return false; } return backtrack(nums, index - 1, leftSum - nums[index]) || backtrack(nums, index - 1, leftSum); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值