Week1 Arrays

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

1493.

Longest Subarray of 1's After Deleting One Element

class Solution:
def longestSubarray(self, nums: List[int]) -> int:
res = 0
l, left, count = len(nums), 0, 0

for i in range(0, l):


if nums[i] == 0:
res = max(res, left+count)
left = count
count = 0
else:
count += 1

if count == l:
return count-1
else:
return max(res, left+count)

===============================================

1476. Subrectangle Queries

class SubrectangleQueries:

def __init__(self, rectangle: List[List[int]]):


self.matrix = rectangle

def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int,
newValue: int) -> None:
for i in range(row1, row2+1):
for j in range(col1, col2+1):
self.matrix[i][j] = newValue

def getValue(self, row: int, col: int) -> int:


return self.matrix[row][col]

# Your SubrectangleQueries object will be instantiated and called as such:


# obj = SubrectangleQueries(rectangle)
# obj.updateSubrectangle(row1,col1,row2,col2,newValue)
# param_2 = obj.getValue(row,col)

===============================================

1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K

class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
fibo = []
a, b = 0, 1
ans = 0
while a <= k:
fibo.append(a)
b, a = a+b, b
for i in range(len(fibo)-1, 0, -1):
if k >= fibo[i]:
k -= fibo[i]
ans +=1
return ans

===============================================

1395. Count Number of Teams

class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
up = [0] * n
down = [0] * n
teams = 0

for i in range(n-1, -1, -1):


for j in range(i+1, n):
if rating[i] < rating[j]:
up[i] += 1
teams += up[j]
else:
down[i] += 1
teams += down[j]

return teams

===============================================

1338. Reduce Array Size to The Half

class Solution:
def minSetSize(self, arr: List[int]) -> int:
coll = collections.Counter(arr)
values = sorted(coll.values(), reverse=True)
z = 0

for i in range(len(values)):
z += values[i]
if z >= len(arr)//2:
return i + 1

===============================================

1277. Count Square Submatrices with All Ones

class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
rows = len(matrix)
cols = len(matrix[0])
temp = [[0]*cols for _ in range(rows)]
count = 0

for row in range(rows):


for col in range(cols):
if matrix[row][col] == 1:
temp[row][col] = 1 + min(temp[row][col - 1], temp[row - 1]
[col], temp[row - 1][col - 1])
count += temp[row][col]

return count

===============================================

621. Task Scheduler

class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:

freq = collections.Counter(tasks)
max_freq = max(freq.values())
freq = list(freq.values())
max_freq_ele_count = 0
i = 0
while(i < len(freq)):
if(freq[i] == max_freq):
max_freq_ele_count += 1
i += 1

ans = (max_freq - 1) * (n+1) + max_freq_ele_count

return max(ans, len(tasks))

===============================================

1366. Rank Teams by Votes

===============================================

You might also like