Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warmup : game of life

Johnlin
February 07, 2017

Swift Warmup : game of life

Johnlin

February 07, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. 289. Game of Life • https://leetcode.com/problems/game-of-life/ • According to the

    Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.” • https://zh.wikipedia.org/wiki/%E5%BA %B7%E5%A8%81%E7%94%9F%E5%91%BD %E6%B8%B8%E6%88%8F • ߁Җੜ໋༡ፍ
  2. • Given a board with m by n cells, each

    cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article): • څ㟬Ұݸm x n త൫໘ɼ㑌ݸ֨ࢠ౎༗Ұݸى࢝㐫 ଶɼ׆(1) ҃ ࢮ(0)ɻ㑌ݸ֨ࢠ࿨ଞతീݸᬠډʢ্Լ ࠨӈɼؐ༗ࣼ޲ʣಁաԼ໘࢛ᑍنଇޓಈɻ
  3. • Any live cell with fewer than two live neighbors

    dies, as if caused by under-population. • ׆త֨ࢠɼ೗Ռ୞༗গԙၷݸ׆తᬠډɼ။ଠݽᘐࣕ ࢮɻ • Any live cell with two or three live neighbors lives on to the next generation. • ׆త֨ࢠɼ೗Ռ༗ၷ҃ࡾݸ׆తᬠډɼ။៺᠃׆Լ ڈɻ
  4. • Any live cell with more than three live neighbors

    dies, as if by over-population.. • ׆త֨ࢠɼ೗Ռ༗௒աࡾݸ׆తᬠډɼ။ଠ༴Ꮀࣕࢮɻ • Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. • ࢮత֨ࢠɼ೗Ռ༗߶޷ࡾݸ׆తᬠډɼब။෮׆ɻ
  5. • Write a function to compute the next state (after

    one update) of the board given its current state. • ሜҰݸവᏐɼኺݱࡏత㐫ଶိܭࢉԼҰճ߹త㐫ଶɻ
  6. • Follow up: • Ճ෼୊ • Could you solve it

    in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells. • ՄҎෆ࢖༻ֹ֎ۭؒ(in-place) ိ႔ཧ䆩ʁهॅɼॴ ༗త֨ࢠཁҰ࣍႔ཧ׬ɼෆՄҎઌ႔ཧҰࠣɼ࠶༻ଞ ၇৽తᆴိ႔ཧଖଞ֨ࢠɻ
  7. • In this question, we represent the board using a

    2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems? • ࡏṜ୊ཫ໘ɼզ၇༻ೋҡਞྻိදࣔ֨ࢠɻୠଖመ൫ ໘جຊ্ੋແݶେతɼ༻ਞྻత࿩ࡏᬑ㐊႔။༗໰୊ɼ Ṝݸ໰୊㟬။ዎኄղܾ䏆ʁ
  8. ൣྫ • [[0,0,0],
 [1,1,1],
 [0,0,0]] • తᬠډᏐੋ[[2,3,2],
 [1,2,1],
 [2,3,2]] •

    ॴҎ။Ꮣ੒
 [[0,1,0],
 [0,1,0],
 [0,1,0]] ׆ᬠډᏐ 2 3 ଖଞ ׆(1) ׆(1) ׆(1) ࢮ(0) ࢮ(0) ࢮ(0) ׆(1) ࢮ(0)
  9. ൣྫ • [[0,1.1,0],
 [1,0,0,1],
 [0,1.1,0]] • తᬠډᏐੋ[[2,2,2,2],
 [2,5,5,2],
 [2,2,2,2]] •

    ॴҎᔒᏓɼؐੋҰᒬੋ
 [[0,1.1,0],
 [1,0,0,1],
 [0,1.1,0]] ׆ᬠډᏐ 2 3 ଖଞ ׆(1) ׆(1) ׆(1) ࢮ(0) ࢮ(0) ࢮ(0) ׆(1) ࢮ(0)
  10. class Solution {
 func countLiveNeighbor(x:Int, y:Int, board: [[Int]]) -> Int

    { var liveNeighborCount = 0 for yy in max(0,y-1)...min(board.count-1,y+1) { for xx in max(0,x-1)...min(board[0].count-1, x+1) { liveNeighborCount += board[yy][xx] > 0 ? 1 : 0 } } liveNeighborCount -= board[y][x] > 0 ? 1 : 0 return liveNeighborCount } func gameOfLife(_ board: inout [[Int]]) { var numNeighbors:[[Int]] = board for y in 0..<board.count { for x in 0..<board[0].count { numNeighbors[y][x] = countLiveNeighbor(x: x, y: y, board: board) } } } }
  11. class Solution { func countLiveNeighbor(x:Int, y:Int, board: [[Int]]) -> Int

    {…}
 func gameOfLife(_ board: inout [[Int]]) { var numNeighbors:[[Int]] = board for y in 0..<board.count { for x in 0..<board[0].count { numNeighbors[y][x] = countLiveNeighbor(x: x, y: y, board: board) } } for y in 0..<board.count { for x in 0..<board[0].count { switch numNeighbors[y][x] { case 2: continue case 3: board[y][x] = 1 default: board[y][x] = 0 } } } } } ׆ᬠډᏐ 2 3 ଖଞ ׆(1) ׆(1) ׆(1) ࢮ(0) ࢮ(0) ࢮ(0) ׆(1) ࢮ(0) Time Complexity: O(m*n)
 Space Complexity: O(m*n)
 Run Time: 25ms
  12. Ճ෼୊ • ෆ༻ֹ֎తۭؒ( O(1) )ɼ௚઀ૢ࡞ଞڅతਞྻɻ • ଞతਞྻੋ [[Int]] ୠੋ୞༻ိଘ 0/1ɼଠ࿘අྃɻ

    • ೺׆ᬠډᏐ໵ଘਐڈɼઌငҎ2 ࠶Ճ্ݪိతᆴɼब ෆ။ׯ৙తࢮ׆త㐫ଶɻ
  13. • ׆֨ࢠɼࡾݸ׆ᬠډ => 1 + 3*2 = 7 = 111b

    • ࢮ֨ࢠɼࡾݸ׆ᬠډ => 0 + 3*2 = 6 = 110b • ׆֨ࢠɼ࢛ݸ׆ᬠډ => 1 + 4*2 = 9 = 1001b • ׆֨ࢠɼീݸ׆ᬠډ => 1 + 8*2 = 17 = 10001b • औআҎೋతᰨᏐबੋ 㐫ଶɼ আҎೋత঎बੋ ᬠډᏐ ᬠډᏐ 㐫ଶ 0 0 1 1 0/1
  14. class Solution { func countLiveNeighbor(x:Int, y:Int, board: [[Int]]) -> Int

    { var liveNeighborCount = 0 for yy in max(0,y-1)...min(board.count-1,y+1) { for xx in max(0,x-1)...min(board[0].count-1, x+1) { liveNeighborCount += board[yy][xx]%2 > 0 ? 1 : 0 } } liveNeighborCount -= board[y][x]%2 > 0 ? 1 : 0 return liveNeighborCount } func gameOfLife(_ board: inout [[Int]]) { for y in 0..<board.count { for x in 0..<board[0].count { board[y][x] += 2 * countLiveNeighbor(x: x, y: y, board: board) } } } }
  15. class Solution { func countLiveNeighbor(x:Int, y:Int, board: [[Int]]) -> Int

    {…}
 func gameOfLife(_ board: inout [[Int]]) { for y in 0..<board.count { for x in 0..<board[0].count { board[y][x] += 2 * countLiveNeighbor(x: x, y: y, board: board) } } for y in 0..<board.count { for x in 0..<board[0].count { switch board[y][x]/2 { case 2: board[y][x] = board[y][x]%2 case 3: board[y][x] = 1 default: board[y][x] = 0 } } } } } ׆ᬠډᏐ 2 3 ଖଞ ׆(1) ׆(1) ׆(1) ࢮ(0) ࢮ(0) ࢮ(0) ׆(1) ࢮ(0) Time Complexity: O(m*n)
 Space Complexity: O(1)
 Run Time: 42ms
  16. Q&A