add some array playing

This commit is contained in:
Marina S 2023-07-29 11:08:44 -07:00 committed by GitHub
parent 5732cfcc61
commit 5c55b81641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,41 @@
'''
Given an integer array nums, return the third distinct maximum
number in this array. If the third maximum does not exist,
return the maximum number.
Do it O(n).
'''
import math
def thirdMax(nums: list[int]) -> int:
first_max = -math.inf
second_max = -math.inf
third_max = -math.inf
for n in nums:
if n == first_max or n == second_max or n == third_max:
continue
elif n > first_max:
third_max = second_max
second_max = first_max
first_max = n
elif n > second_max:
third_max = second_max
second_max = n
elif n > third_max:
third_max = n
if third_max == -math.inf:
third_max = max(second_max, first_max)
return third_max
if __name__ == "__main__":
nums = [3,2,1]
assert(thirdMax(nums) == 1)

View File

@ -0,0 +1,27 @@
"""
Given an array arr of integers, check if there exist two indices i and j such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
"""
def check_if_exist(arr: list[int]) -> bool:
aux_dict = {}
for i, num in enumerate(arr):
aux_dict[2*num] = i
for j, num in enumerate(arr):
if num in aux_dict.keys() and j != aux_dict[num]:
return (j, aux_dict[num])
return False
if __name__ == "__main__":
arr = [-2, 0, 10, -19, 4, 6, -8]
print(check_if_exist(arr))

View File

@ -0,0 +1,39 @@
'''
Given an array of integers arr, return true if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
'''
def valid_mountain_array(arr: list[int]) -> bool:
last_number, mountain_up = arr[0], True
for i, n in enumerate(arr[1:]):
if n > last_number:
if mountain_up == False:
return False
elif n < last_number:
if i == 0:
return False
mountain_up = False
else:
return False
last_number = n
return not mountain_up
if __name__ == "__main__":
arr = [0,3,2,1]
print(valid_mountain_array(arr))

View File

@ -0,0 +1,33 @@
# Given a fixed-length integer array arr, duplicate each occurrence of zero,
# shifting the remaining elements to the right.
def duplicate_zeros(arr: list[int]) -> list[int]:
"""
Do not return anything, modify arr in-place instead.
"""
i = 0
while i < len(arr):
if arr[i] == 0 and i != len(arr) - 1:
range_here = len(arr) - (i + 2)
while range_here > 0:
arr[i + range_here + 1] = arr[i + range_here]
range_here -= 1
arr[i+1] = 0
i += 2
else:
i += 1
return arr
if __name__ == "__main__":
arr = [1, 0, 2, 3, 0, 4, 5, 0]
print(duplicate_zeros(arr))

View File

@ -0,0 +1,53 @@
"""
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two
integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function,
but instead be stored inside the array nums1.
To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements
that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
"""
def merge(big_list: list[int], m: int, small_list: list[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
"""
index_big, index_small, index_backward = m - 1, n - 1, m + n - 1
while index_big >= 0 and index_small >= 0:
if small_list[index_small] > big_list[index_big]:
big_list[index_backward] = small_list[index_small]
index_small -= 1
else:
big_list[index_backward] = big_list[index_big]
index_big -= 1
index_backward -= 1
while index_backward >= 0 and index_big >= 0:
big_list[index_backward] = big_list[index_big]
index_backward -= 1
index_big -= 1
while index_backward >= 0 and index_small >= 0:
big_list[index_backward] = small_list[index_small]
index_backward -= 1
index_small -= 1
if __name__ == "__main__":
nums1 = [1,2,3,0,0,0]
m = 3
nums2 = [2,5,6]
n = 3
merge(nums1, m, nums2, n)
print(nums1)
# [1,2,2,3,5,6]

View File

@ -0,0 +1,42 @@
'''
Given an integer array nums, move all 0's to the end of it while
maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:
Input: nums = [0]
Output: [0]
'''
def move_zeroes(nums: list[int]) -> list[int]:
"""
Do not return anything, modify nums in-place instead.
"""
i = 0
while i < len(nums) - 1:
if nums[i] == 0:
j = i + 1
while nums[j] == 0 and j < len(nums) - 1:
j += 1
nums[i], nums[j] = nums[j], nums[i]
i += 1
return nums
if __name__ == "__main__":
nums = [0,1,0,3,12]
assert(move_zeroes(nums) == [1,3,12,0,0])

View File

@ -0,0 +1,28 @@
### Remove Duplicates from Sorted Array in-place
def remove_duplicates(nums: list[int]) -> int:
arr_i, dup_i = 0, 1
while arr_i < len(nums) and dup_i < len(nums):
if nums[arr_i] == nums[dup_i]:
dup_i += 1
else:
arr_i += 1
nums[arr_i] = nums[dup_i]
for i in range(arr_i + 1, dup_i):
nums[i] = '_'
return dup_i - arr_i - 1, nums
if __name__ == "__main__":
nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
assert(remove_duplicates(nums) == (5, [0, 1, 2, 3, 4, '_', '_', '_', '_', '_']))