526. Beautiful Arrangement Difficulty: Medium

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:
N is a positive integer and will not exceed 15.

算法解析:利用回溯法遍历所有可能的结果,类似于深度优先搜索。Python版如果套用C语言的套路,会在计算n=15的时候超时,所以用了别人写了一个动态规划的算法。

C语言版

void solve(int* visited, int t, int n)
{
    int i;
    if(t > n)
        visited[0]++;
    for(i = 1; i <= n; i++)
    {
        if(!visited[i] && (t % i == 0 || i % t == 0))
        {
            visited[i] = 1;
            solve(visited, t + 1, n);
            visited[i] = 0;
        }
    }
}

int countArrangement(int N) {
    int t = 1;
    int visited[16] = {0};
    solve(visited, t, N);
    return visited[0];
}

Python版

cache = {}
class Solution(object):
    def countArrangement(self, N):
        def helper(i, X):
            if i == 1:
                return 1
            key = (i, X)
            if key in cache:
                return cache[key]
            total = 0
            for j in xrange(len(X)):
                if X[j] % i == 0 or i % X[j] == 0:
                    total += helper(i - 1, X[:j] + X[j + 1:])
            cache[key] = total
            return total
        return helper(N, tuple(range(1, N + 1)))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
An LCD (Liquid Crystal Display) is a type of display technology that uses liquid crystals to display information. LCDs are commonly used in electronic devices such as digital watches, calculators, and mobile phones, and can also be used in more complex systems such as embedded systems and microcontrollers. A matrix keypad, on the other hand, is a type of input device that allows users to input data through a combination of buttons arranged in a matrix pattern. A typical matrix keypad has rows and columns of buttons that can be pressed to input data. The arrangement of the buttons in a matrix pattern allows for a large number of buttons to be accommodated in a small space. To interface an LCD and a matrix keypad with a microcontroller, several specifications need to be considered. These include: 1. Pinout: The pinout of the LCD and matrix keypad needs to be understood in order to connect them to the microcontroller. The pinout typically includes pins for power, ground, data, and control signals. 2. Voltage levels: The voltage levels of the LCD and matrix keypad need to be compatible with the microcontroller. Most microcontrollers operate at 5V or 3.3V, so the LCD and matrix keypad should also operate at these voltage levels. 3. Data communication protocol: The data communication protocol between the microcontroller, LCD, and matrix keypad needs to be understood in order to transfer data between them. The most common data communication protocols used are SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit). 4. LCD commands: The LCD has a set of commands that can be used to control its behavior, such as clearing the screen, setting the cursor position, and displaying text. These commands need to be understood in order to control the LCD using the microcontroller. 5. Matrix keypad scanning: The matrix keypad needs to be scanned in order to detect which buttons are being pressed. This is typically done using a technique called matrix scanning, where the rows and columns of the keypad are scanned sequentially to detect button presses. Overall, interfacing an LCD and matrix keypad with a microcontroller requires a thorough understanding of their specifications and how they can be connected and controlled.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值