1. N Empress (Hard)
n research queens problem is how to n queens placed n × n on the board, and the queen can not attack each other to each other.
A Method for Solving the picture shows the 8 queens problem.
Given an integer n , returns all the different n solutions to the problem of the Queen.
Each solution contains an explicit n queens problem pieces placed embodiment, the program 'Q'
and '.'
represent the space and queen.
Example:
1 |
Input: 4 |
answer:
Ideas: Backtracking
This question is difficult, freshman felt so hard. . .
Prior to the establishment of the algorithm, let us consider two useful details.
Line may only have a queen and one may only have a queen.
This means no need to consider all of the squares on the board. Simply press the column is circulated.
There row number = column number + constant for all the main diagonal, the row numbers for all have a diagonal views - column number = constant.
This allows us to have a diagonal mark in the attack range and check a box (row number, column number) whether in attack position.
Now you can write back function backtrack (row = 0).
The first row = 0 from the start.
Cycle and attempts to place the column in each column of the queen.
- If the grid (row, column) is not within the scope of the attack
- At (row, column) is placed on the grid queen.
- Negative position corresponding rows, columns and the two diagonals.
- If all rows are considered, row == N
- Means that we have found a solution
- Else
- Queen continue to consider the next place backtrack (row + 1).
- Back: will be removed at (row, column) squares queen.
- If the grid (row, column) is not within the scope of the attack
1 |
class (object): |
Thinking two: DFS
Eight Queens problem can be extended to a more general place n queens problem: At this time the size of the board becomes n × n, and the Queen has become the number n. If and only if n = 1 or n ≥ 4 when the problem solvable.
对于任意(x,y),如果要让新的点和它不能处于同一条横行、纵行或斜线上,则新点(p,q)必须要满足p+q != x+y 和p-q!= x-y, 前者针对左下右上斜线,后者针对左上右下斜线,两者同时都保证了不在同一条横行和纵行上。
代码中变量的含义:
- cols_lst: 每一行皇后的column位置组成的列表
- cur_row:目前正在判断的row的index
- xy_diff:所有x-y组成的列表
- xy_sum:所有x+y组成的列表
1 |
class (object): |
2. N皇后2(Hard)
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
上图为 8 皇后问题的一种解法。
给定一个整数 n,返回 n 皇后不同的解决方案的数量。
示例:
1 |
输入: 4 |
解答:
思路:
和上题完全一模一样,只是最后输出的时候,输出的是结果的长度。
1 |
class (object): |
3. 最大子序和(Easy)
给定一个整数数组 nums
,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
1 |
输入: [-2,1,-3,4,-1,2,1,-5,4], |
进阶:
如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
解答:
思路:
这道题很简单,动态规划,逐步更新局部最优解和全局最优解。
1 |
class (object): |
4. 螺旋矩阵(Medium)
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
1 |
输入: |
示例 2:
1 |
输入: |
解答:
思路:
绘制螺旋轨迹路径,我们发现当路径超出界限或者进入之前访问过的单元格时,会顺时针旋转方向。
算法
假设数组有R 行 C 列,seen[r][c]表示第 r 行第 c 列的单元格之前已经被访问过了。当前所在位置为 (r, c),前进方向是 di。我们希望访问所有 R x C 个单元格。
当我们遍历整个矩阵,下一步候选移动位置是(next_r, next_c)。如果这个候选位置在矩阵范围内并且没有被访问过,那么它将会变成下一步移动的位置;否则,我们将前进方向顺时针旋转之后再计算下一步的移动位置。
1 |
class (object): |
5. 跳跃游戏(Medium)
给定一个非负整数数组,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
判断你是否能够到达最后一个位置。
示例 1:
1 |
输入: [2,3,1,1,4] |
示例 2:
1 |
输入: [3,2,1,0,4] |
解答:
思路:
这个题很好想。
就是从后往前倒推,如果倒数第二个能到底倒数第一个位置,那么可以就去求是否可以达到倒数第二个位置。
就是反复往回推的过程。
1 |
class Solution(object): |