0% found this document useful (0 votes)
4 views

coding notes

Uploaded by

sadimula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

coding notes

Uploaded by

sadimula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT-3

Aim: Maximum Sum circular subarray

Program:

from typing import List class

Solution:

def maxSubarraySumCircular(self, nums: List[int]) -> int:

def kadane(array): max_ending_here =

max_so_far = array[0] for num in array[1:]:

max_ending_here = max(num, max_ending_here + num)

max_so_far = max(max_so_far, max_ending_here) return

max_so_far total_sum = sum(nums) max_subarray_sum =

kadane(nums) min_subarray_sum = kadane([-num for num in

nums]) if total_sum == -min_subarray_sum:

return max_subarray_sum wrap_around_sum =

total_sum + min_subarray_sum return

max(max_subarray_sum, wrap_around_sum)

Output:
Aim: Stamping the sequence

Program:

from typing import List class

Solution:

def movesToStamp(self, stamp: str, target: str) -> List[int]:

m, n = len(stamp), len(target)

target_list = list(target)

stamp_list = list(stamp)

result = [] visited =

[False] * n

total_replaced = 0

def

can_stamp(start):

for i in range(m):

if target_list[start + i] != '?' and target_list[start + i] != stamp_list[i]:

return False
return True def

do_stamp(start): nonlocal

total_replaced for i in

range(m):

if target_list[start + i] != '?': target_list[start

+ i] = '?'

total_replaced += 1

while total_replaced < n:

stamped = False for i in

range(n - m + 1):

if not visited[i] and can_stamp(i):

do_stamp(i)

visited[i] = True

result.append(i)

stamped = True if

not stamped:

return [] return

result[::-1]

Output:

You might also like