I am wondering if I calculate the time complexity correctly with the function below.
mat is a list of lists.
k is an integer.
def kWeakestRows(mat, k):
hashmap = {}
for i in range(len(mat)):
hashmap[i] = Counter(mat[i]).get(1)
if hashmap[i] == None:
hashmap[i] = 0
result = []
while len(result) < k:
key = min(hashmap, key=hashmap.get)
result.append(key)
hashmap.pop(key)
return result
My thought is since it iterates through one for loop (for size of a list) and one while loop (for the value of k), it is O(N). But in the for loop, I use Counter to count the 1s in the inner list, it would be O(N*M).
In addition, my guess on space complexity is also O(N) as it fills the hashmap with the elements in the given list (mat), and it fills a list (result) by the value of k.
Please let me know if I am wrong.