forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmetic Slices.py
More file actions
56 lines (48 loc) · 1.47 KB
/
Arithmetic Slices.py
File metadata and controls
56 lines (48 loc) · 1.47 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#Arithmetic Slices
#https://leetcode.com/problems/arithmetic-slices/
class DP1(object):
def __init__(self):
self.matrix = None
def displayMatrix(self):
for row in range(len(self.matrix)):
print self.matrix[row]
def fillMatrix(self, A):
row = 0
col = 0
startC = 0
length = 1
numSlices = 0
while(startC < len(A)):
if(length < 3):
self.matrix[row][col] = A[row] - A[col]
else:
if(self.matrix[row][col-1] == self.matrix[row+1][col] and self.matrix[row][col-1] != -999):
self.matrix[row][col] = True
numSlices = numSlices+1
else:
self.matrix[row][col] = -999
row = row+1
col = col+1
if(col == len(A)):
row = 0
startC = startC+1
col = startC
length = length+1
return numSlices
def numberOfArithmeticSlices(self, A):
self.matrix = [[0 for col in range(len(A))] for row in range(len(A))]
print self.fillMatrix(A)
self.displayMatrix()
"""
:type A: List[int]
:rtype: int
"""
#Main
obj1 = DP1()
obj1.numberOfArithmeticSlices([1,2,3,4,5])
obj2 = DP1()
obj2.numberOfArithmeticSlices([1,2,3,9,4,5])
obj3 = DP1()
obj3.numberOfArithmeticSlices([1,1,1,1])
obj4 = DP1()
obj4.numberOfArithmeticSlices([18, -36, 18])