-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpLib.py
More file actions
31 lines (29 loc) · 1.47 KB
/
HelpLib.py
File metadata and controls
31 lines (29 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
from collections import defaultdict
class Helper():
def CalculatePrefixSum(self, m_matrix, m_rowNumber, m_columnnumber):
for i in range(m_rowNumber):
for j in range(m_columnnumber):
m_matrix[i, j] += m_matrix[i-1, j]
for i in range(m_columnnumber):
for j in range(m_rowNumber):
m_matrix[j, i] += m_matrix[j, i-1]
def GetCountInRangeFromPrefixSum(self, m_matrix, m_minRowIndex, m_minColumnIndex, m_maxRowIndex, m_maxColumnIndex):
if (m_minRowIndex > m_maxRowIndex) or (m_minColumnIndex > m_maxColumnIndex):
return 0
return (m_matrix[m_maxRowIndex + 1, m_maxColumnIndex + 1] - m_matrix[m_maxRowIndex + 1, m_minColumnIndex]- m_matrix[m_minRowIndex, m_maxColumnIndex + 1] + m_matrix[m_minRowIndex, m_minColumnIndex])
class TwoDimensionalDictionary():
def __init__(self):
self.dict = defaultdict(dict)
def ContainsKey(self, m_rowKey, m_columnKey):
if not(m_rowKey in self.dict):
return False
if not(m_columnKey in self.dict[m_rowKey]):
return False
return True
def Add(self, m_rowKey, m_columnKey, m_addValue):
if not(m_rowKey in self.dict):
self.dict[m_rowKey] = {}
self.dict[m_rowKey][m_columnKey] = []
elif not(m_columnKey in self.dict[m_rowKey]):
self.dict[m_rowKey][m_columnKey] = []
self.dict[m_rowKey][m_columnKey].append(m_addValue)