File tree Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Expand file tree Collapse file tree 3 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def tictactoe (self , moves ):
3
+ """
4
+ :type moves: List[List[int]]
5
+ :rtype: str
6
+ """
7
+ grid = [[- 1 for _ in range (3 )] for _ in range (3 )]
8
+ def check ():
9
+ for row in grid :
10
+ if row == [0 , 0 , 0 ]:
11
+ return 0
12
+ if row == [1 , 1 , 1 ]:
13
+ return 1
14
+
15
+ for j in range (3 ):
16
+ tmp = []
17
+ for i in range (3 ):
18
+ tmp .append (grid [i ][j ])
19
+ if tmp == [0 , 0 , 0 ]:
20
+ return 0
21
+ if tmp == [1 , 1 , 1 ]:
22
+ return 1
23
+
24
+ tmp = [grid [0 ][0 ], grid [1 ][1 ], grid [2 ][2 ]]
25
+ if tmp == [0 , 0 , 0 ]:
26
+ return 0
27
+ if tmp == [1 , 1 , 1 ]:
28
+ return 1
29
+
30
+ tmp = [grid [2 ][0 ], grid [1 ][1 ], grid [0 ][2 ]]
31
+ if tmp == [0 , 0 , 0 ]:
32
+ return 0
33
+ if tmp == [1 , 1 , 1 ]:
34
+ return 1
35
+ return - 1
36
+
37
+
38
+ player = 0
39
+ for move in moves :
40
+ grid [move [0 ]][move [1 ]] = player
41
+ player = 1 - player
42
+
43
+ tmp = check ()
44
+ if tmp != - 1 :
45
+ return "A" if tmp == 0 else "B"
46
+ return "Draw" if len (moves ) == 9 else "Pending"
47
+
48
+
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def numOfBurgers (self , tomatoSlices , cheeseSlices ):
3
+ """
4
+ :type tomatoSlices: int
5
+ :type cheeseSlices: int
6
+ :rtype: List[int]
7
+ """
8
+ doublex = tomatoSlices - cheeseSlices * 2
9
+ if doublex < 0 or doublex % 2 != 0 :
10
+ return []
11
+
12
+ x = doublex // 2
13
+ y = cheeseSlices - doublex // 2
14
+ if x >= 0 and y >= 0 :
15
+ return [x , y ]
16
+ return []
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def countSquares (self , matrix ):
3
+ """
4
+ :type matrix: List[List[int]]
5
+ :rtype: int
6
+ """
7
+ m , n = len (matrix ), len (matrix [0 ])
8
+ res = 0
9
+ dp = [[0 for _ in range (n )] for _ in range (m )]
10
+ for i in range (m ):
11
+ dp [i ][0 ] = matrix [i ][0 ]
12
+ res += dp [i ][0 ]
13
+
14
+ for j in range (1 , n ):
15
+ dp [0 ][j ] = matrix [0 ][j ]
16
+ res += dp [0 ][j ]
17
+
18
+ for i in range (1 , m ):
19
+ for j in range (1 , n ):
20
+ if matrix [i ][j ]:
21
+ dp [i ][j ] = min (dp [i - 1 ][j ], dp [i - 1 ][j - 1 ], dp [i ][j - 1 ]) + 1
22
+ res += dp [i ][j ]
23
+ print dp
24
+ return res
25
+
You can’t perform that action at this time.
0 commit comments