CodingTour
ARTS #91

Algorithm

本周选择的算法题是:Word Search

规则

Given an m x n board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cells, where “adjacent” cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example 1:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true

Example 2:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true

Example 3:

Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false

Constraints:

  • m == board.length
  • n = board[i].length
  • 1 <= m, n <= 200
  • 1 <= word.length <= 103
  • board and word consists only of lowercase and uppercase English letters.

Solution

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        def find(i, j, word_index):
            if board[i][j] == word[word_index]:
                if word_index == len(word) - 1: return True
                board[i][j] = "#"
                for (new_i, new_j) in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
                    if not 0 <= new_i < len(board): continue
                    if not 0 <= new_j < len(board[0]): continue
                    if find(new_i, new_j, word_index + 1): return True
                board[i][j] = word[word_index]
            else:
                return False

        for i in range(len(board)):
            for j in range(len(board[0])):
                if board[i][j] == word[0] and find(i, j, 0): return True
        return False

尽量减少不必要的计算(击败了 97% 的用户):

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        l = len(word)
        m = len(board)
        n = len(board[0])

        def find(i, j, index):
            if board[i][j] == word[index]:
                if index == l - 1: return True
                board[i][j] = "#"
                if (i != 0 and find(i-1, j, index+1)) or (i != m-1 and find(i+1, j, index+1)) or (j != 0 and find(i, j-1, index+1)) or (j != n-1 and find(i, j+1, index+1)):
                    return True
                board[i][j] = word[index]
            return False

        for i in range(m):
            for j in range(n):
                if board[i][j] == word[0] and find(i, j, 0): return True
        return False

Review

How we misunderstood microservices

作者的论点很简单,很多情况下在微服务架构的设计过程中,只专注于后端服务的解耦、数据库部署和服务间的调用,而忽略了前端(包含客户端)的设计,最终产生了下图这样的架构:

这应该是很多公司的现状,作者呼吁:

A microservice should include all the parts necessary to enable a team to deliver software whenever they feel ready to deliver value to their users. Those teams should also own their interfaces, not just back-end services and databases.

我很认同,不过这事儿的难度很高,需要软件架构与组织架构高度一致。

Tip

找到了 Maven 访问加速的方法,能较为完美的解决异地团队的诉求。

Share

《发现心流》讲了什么