526. Beautiful Arrangement
526. Beautiful Arrangement
Description
Solution
Naively do backtracking
DP + State Compression
use
mask
to record firstnum
‘s integer’s combinations, for example0101
means 1 and 3 are in the first 2 slots of the array.1
dp[mask] = sum of dp[mask^i], which i is suitable for popcount(mask) slot
Code
1 | class Solution { |
Solution2
1 | class Solution { |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.