CodingTour
ARTS #125

Algorithm

本周选择的算法题是:N-Queens II

规则

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return the number of distinct solutions to the n-queens puzzle.

Example 1:

img

Input: n = 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown.

Example 2:

Input: n = 1
Output: 1

Constraints:

  • 1 <= n <= 9

Solution

class Solution:
    def totalNQueens(self, n: int) -> int:
        if n == 0: return 0

        def dfs(matrix, diagonal_left, diagonal_right):
            i = len(matrix)
            if i == n: 
                self.ans += 1
                return

            for j in range(n):
                if j not in matrix and (j - i) not in diagonal_left and (j + i) not in diagonal_right:
                    dfs(matrix + [j], diagonal_left + [j-i], diagonal_right + [j+i])

        self.ans = 0
        for j in range(n):
            dfs([j], [j], [j])

        return self.ans

Review

This post will make you rethink which programming languages are worth learning

还是不能忽视行业的重要性。Java、C 就不说了,JavaScript、HTML/CSS 之所以很流行,是因为 web 开发者在劳动市场上很受欢迎;Python 和 SQL 也很流行,这和数据科学、数据分析、数据工程师有关。

但语言与工作满意度是无关的。

要选择语言,还是要思考自己喜欢做什么,构建 API,还是做有挑战性的交互页面,又或是支撑系统优化的数据分析,不同岗位角色决定了不同的适用语言。

Tip

用油猴脚本完全重写了 Chrome 插件,在内部小团队里内测中,可以解决 Chrome 插件(unpacked)不能自动更新的问题。

Share

从历史看组织