Untitled6.ipynb - Colab
Untitled6.ipynb - Colab
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.prev = None # This will hold the previous node in post-order traversal
class Solution:
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
a=[t-n for t,n in zip(target,nums)] #differences array
ans=0
def solve(a):
a=[0]+a # add 0 at left.
ans=0
for x,y in pairwise(a):
if x<y:
ans+=y-x
return ans
for g in groupby(a,lambda x: x<0):
ans+=solve(list([abs(x) for x in g[1]]))
return ans
class Solution:
def waysToReachStair(self, target: int) -> int:
current_sum = 1
ways = 0
if target == 0 or target == 1:
ways += 1
for i in range(30):
current_sum += 1 << i
if current_sum >= target and current_sum - target <= i + 2:
n = i + 2
r = current_sum - target
ways += pas[n][r]
return ways
if (currval > 0) :
res = max(res,currsum - memo[currval] + currval);
else :
res = max(res,currsum - memo[currval] + 2 * currval);
else :
memo[currval] = currsum;
if (currval < 0) :
currsum = currsum - currval;
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[1])
ans, t = 0, intervals[0][1]
for s, e in intervals[1:]:
if s >= t:
t = e
else:
ans += 1
return ans
for i in range(k):
res += max(0, arr[n - 1 - i] - i)
return res
def maximize_value(n, k, arr):
# Sort the array
arr.sort()
res = 0
for i in range(k):
res += max(0, arr[n - k - i] - i)
return res
class Solution:
def find_prime_pairs(self, n: int) -> List[List[int]]:
# Edge case: if n is less than 4, no prime pairs can sum to it
if n < 4:
return []
class Solution:
def queryString(self, s: str, n: int) -> bool:
for i in range(1,n+1):
if bin(i)[2:] not in s:return False
return True
strikers = 0
last_striker_position = float('-inf') # Initialize to negative infinity
for x_start, x_end in targets:
# If the current striker cannot cover this target, place a new striker
if x_start > last_striker_position:
strikers += 1
last_striker_position = x_end # Place the striker at the end of the current target
return strikers
# Append the maximum stock level to the result (0 if all stocks are non-positive)
inventory_status.append(max_stock if max_stock > 0 else 0)
return inventory_status
def longest_zigzag_sequence(numbers):
# Initialize the longest sequence length to 1
longest_sequence_length = 1
# List with size equal to the input list, initialized with 1
sequence_lengths_larger = [1] * len(numbers)
sequence_lengths_smaller = [1] * len(numbers)
# Iterate through the numbers
for i in range(1, len(numbers)):
if(numbers[i] > numbers[i - 1]):
sequence_lengths_larger[i] = sequence_lengths_smaller[i - 1] + 1
sequence_lengths_smaller[i] = 1
elif(numbers[i] < numbers[i - 1]):
sequence_lengths_smaller[i] = sequence_lengths_larger[i - 1] + 1
sequence_lengths_larger[i] = 1
else:
sequence_lengths_larger[i] = 1
sequence_lengths_smaller[i] = 1
#Find the longest sequence
for i in range(len(numbers)):
longest_sequence_length = max(longest_sequence_length, max(sequence_lengths_larger[i], sequence_lengths_smaller[i]))
return longest_sequence_length
# If more than 'k' numbers divisible by 'p' are found, stop this sub-array
if divisible_count > k:
break
# Convert subarray to a tuple (since lists are not hashable) and add to set
unique_subarrays.add(tuple(subarray))
# Example usage:
nums = [1,2,3,4] # The array from the example
k = 4 # Maximum numbers divisible by p allowed in a sub-array
p = 1 # The magical number
def non_mirror_string(word):
from collections import Counter
n = len(word)
if n % 2 != 0:
return "-1" # Ensure the word has even length.
return ''.join(result)
# Example Usage
print(non_mirror_string("zadz")) # Expected Output: "adzz"
print(non_mirror_string("zyyz")) # Expected Output: "yyzz"
print(non_mirror_string("qqqt")) # Expected Output: "-1"