Martyn Ye

A geek & a dreamer

  • 主页
  • 随笔
所有文章 友链 关于我

Martyn Ye

A geek & a dreamer

  • 主页
  • 随笔

[LeetCode] Surrender Regions

2016-02-18

Analysis:

we must use breadth first traversal, otherwise the recursion stack depth will be exceeded.
use touchWall flag to determine whether this area can be marked

Time Complexity:

  • O(n)

Space Complexity:

  • O(n)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution(object):
def bfs(self, board, row, col, visited):
toFlip = []
touchWall = False

height, width = len(board), len(board[0])
bfsStore = collections.deque([(row, col)])
def getNextNode(i, j):
if i > 0 and board[i-1][j] == 'O' and visited[i-1][j] == 0: bfsStore.append((i-1, j))
if i < height-1 and board[i+1][j] == 'O' and visited[i+1][j] == 0: bfsStore.append((i+1, j))
if j > 0 and board[i][j-1] == 'O' and visited[i][j-1] == 0: bfsStore.append((i, j-1))
if j < width-1 and board[i][j+1] == 'O' and visited[i][j+1] == 0: bfsStore.append((i, j+1))

while len(bfsStore) > 0:
tmprow, tmpcol = bfsStore.popleft()
if tmprow == 0 or tmprow == height-1 or tmpcol == 0 or tmpcol == width-1:
touchWall = True

if visited[tmprow][tmpcol] == 0:
visited[tmprow][tmpcol] = 1
toFlip.append((tmprow, tmpcol))
getNextNode(tmprow, tmpcol)

if touchWall == False:
for x, y in toFlip:
board[x][y] = 'X'
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if len(board) == 0 or len(board[0]) == 0:
return

height, width = len(board), len(board[0])

visited = [[0 for i in xrange(width)] for j in xrange(height)]
for i in xrange(height):
for j in xrange(width):
if visited[i][j] == 0 and board[i][j] == 'O':
self.bfs(board, i, j, visited)

赏

谢谢你请我吃糖果

  • LeetCode
  • Breadth First Search

扫一扫,分享到微信

微信分享二维码
[LeetCode] Serialize and Deserialize Binary Tree
[LeetCode] Verify Preorder Serialization of a Binary Tree
© 2024 Martyn Ye
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • Misc
  • LeetCode
  • Dynamic Programming
  • Binary Tree
  • Greedy
  • Breadth First Search
  • Binary Search
  • Math
  • System Design
  • Binary Search Tree
  • Backtracking
  • Heap
  • Design
  • Depth First Search
  • Build Binary Tree
  • array
  • Tree
  • Stack
  • Reservoir Sampling
  • Bit Manipulation
  • Deep-First-Search
  • Trie
  • Hash Table
  • Two Pointers
  • String
  • Android
  • Github
  • Data Structure Design
  • C++
  • Sort
  • Linked List
  • Bit-Manipulation
  • LintCode
  • Merge Sort
  • Mafia fight
  • Array
  • Heap Sort
  • Open Source
  • Summary

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

Martyn's Playground