Test Commit
This commit is contained in:
commit
32fdc2fb2e
0
Top150Interview/121BestTimetoBuyandSellStock.py
Normal file
0
Top150Interview/121BestTimetoBuyandSellStock.py
Normal file
37
Top150Interview/169MajorityElement.py
Normal file
37
Top150Interview/169MajorityElement.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# def majorityElement(nums):
|
||||||
|
# """
|
||||||
|
# :type nums: List[int]
|
||||||
|
# :rtype: int
|
||||||
|
# """
|
||||||
|
# majority = {}
|
||||||
|
# # Start initial position
|
||||||
|
# for i in range(len(nums)):
|
||||||
|
# k = 1
|
||||||
|
# if nums[i] in majority:
|
||||||
|
# # print(f'found {nums[i]} already')
|
||||||
|
# continue
|
||||||
|
# # Count the duplicates
|
||||||
|
# for j in range(i+1, len(nums)):
|
||||||
|
# if nums[i] == nums[j]:
|
||||||
|
# k += 1
|
||||||
|
# majority[nums[i]] = k
|
||||||
|
# print(majority)
|
||||||
|
# # print(max(majority.values()))
|
||||||
|
# # print(max(majority.items(), key=lambda x: x[1])[0])
|
||||||
|
# return max(majority.items(), key=lambda x: x[1])[0]
|
||||||
|
# majorityElement([2,2,1,1,1,2,2])
|
||||||
|
|
||||||
|
# Faster runtime and less memory
|
||||||
|
def majorityElement(nums):
|
||||||
|
majority = {} # Create an empty dictionary to store the counts of each number
|
||||||
|
for num in nums: # Iterate over each element in the input list
|
||||||
|
if num in majority: # Check if the number is already in the dictionary
|
||||||
|
majority[num] += 1 # If it is, increment its count by 1
|
||||||
|
else:
|
||||||
|
majority[num] = 1 # If it is not, add it to the dictionary with a count of 1
|
||||||
|
if majority[num] > len(nums) / 2: # Check if the count of the number exceeds half the length of the list
|
||||||
|
return num # If it does, return the number as the majority element
|
||||||
|
return None # If no majority element is found, return None
|
||||||
|
|
||||||
|
result = majorityElement([2, 2, 1, 1, 1, 2, 2]) # Call the function with the input list [2, 2, 1, 1, 1, 2, 2]
|
||||||
|
print(result) # Print the result
|
||||||
26
Top150Interview/189RotateArray.py
Normal file
26
Top150Interview/189RotateArray.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
def rotate(nums, k):
|
||||||
|
"""
|
||||||
|
Rotate the elements of the list nums to the right by k positions.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nums (List[int]): List of integers to be rotated.
|
||||||
|
k (int): Number of positions to rotate the elements.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None. Modifies nums in-place.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> rotate([1,2,3,4,5,6,7], 3)
|
||||||
|
[5, 6, 7, 1, 2, 3, 4]
|
||||||
|
"""
|
||||||
|
k %= len(nums) # Adjust k if it's larger than the length of the list
|
||||||
|
|
||||||
|
# Use slicing to shift the elements to the right
|
||||||
|
nums = nums[-k:] + nums[:-k]
|
||||||
|
|
||||||
|
print(nums) # Print the rotated list (optional)
|
||||||
|
|
||||||
|
# Example usages
|
||||||
|
rotate([1,2,3,4,5,6,7], 3)
|
||||||
|
rotate([-1,-100,3,99], 2)
|
||||||
|
rotate([1,2], 3)
|
||||||
17
Top150Interview/26RemoveElement.py
Normal file
17
Top150Interview/26RemoveElement.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
def removeElement(nums, val):
|
||||||
|
"""
|
||||||
|
:type nums: List[int]
|
||||||
|
:type val: int
|
||||||
|
:rtype: int
|
||||||
|
"""
|
||||||
|
k = 0
|
||||||
|
for i in range(len(nums)):
|
||||||
|
if val != nums[i]:
|
||||||
|
k += 1
|
||||||
|
else:
|
||||||
|
nums[i] = None
|
||||||
|
print(k)
|
||||||
|
return k
|
||||||
|
|
||||||
|
# removeElement([0,1,2,2,3,0,4,2], 2)
|
||||||
|
removeElement([3,2,2,3], 3)
|
||||||
31
Top150Interview/80RemoveDuplicates.py
Normal file
31
Top150Interview/80RemoveDuplicates.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
def removeDuplicates(nums):
|
||||||
|
"""
|
||||||
|
Remove duplicates from a list of integers and return the length of the resulting list.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
nums (List[int]): A list of integers
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
int: The length of the resulting list after removing duplicates
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> removeDuplicates([1,1,1,2,2,3])
|
||||||
|
3
|
||||||
|
"""
|
||||||
|
k = len(nums) # Initialize k with the length of the input list nums
|
||||||
|
|
||||||
|
for i in range(len(nums)):
|
||||||
|
tempNum = 0
|
||||||
|
# Iterate over the elements of nums starting from index i
|
||||||
|
for j in range(i+1, len(nums)):
|
||||||
|
temp = nums[i]
|
||||||
|
if temp == nums[j]: # Check if a duplicate element is found
|
||||||
|
tempNum += 1
|
||||||
|
if tempNum > 1 and nums[j] != float('inf'):
|
||||||
|
# If it's the second occurrence and the element is not already marked as 'inf',
|
||||||
|
# mark it as 'inf' to indicate it as a duplicate and decrement k by 1
|
||||||
|
nums[j] = float('inf')
|
||||||
|
k -= 1
|
||||||
|
|
||||||
|
nums.sort() # Sort the list in ascending order
|
||||||
|
return k # Return the length of the resulting list without duplicates
|
||||||
23
Top150Interview/88MergeSortedArray.py
Normal file
23
Top150Interview/88MergeSortedArray.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
def merge(self, nums1, m, nums2, n):
|
||||||
|
"""
|
||||||
|
:type nums1: List[int]
|
||||||
|
:type m: int
|
||||||
|
:type nums2: List[int]
|
||||||
|
:type n: int
|
||||||
|
:rtype: None Do not return anything, modify nums1 in-place instead.
|
||||||
|
"""
|
||||||
|
count = 0
|
||||||
|
# merge unsorted arrays
|
||||||
|
for j in range(len(nums1)):
|
||||||
|
if nums1[j] == 0:
|
||||||
|
nums1[j] = nums2[count]
|
||||||
|
count += 1
|
||||||
|
# # array is filled, now sort, used bubble sort
|
||||||
|
for i in range(len(nums1)):
|
||||||
|
for j in range(len(nums1) - i - 1):
|
||||||
|
if nums1[j] > nums1[j + 1]:
|
||||||
|
nums1[j], nums1[j+1] = nums1[j+1], nums1[j]
|
||||||
|
print(nums1)
|
||||||
|
# merge(0, [1,2,3,0,0,0], 3, [2,5,6], 3)
|
||||||
|
merge(0, [4,5,6,0,0,0], 3, [1,2,3], 3)
|
||||||
17
Top150Interview/LessonsLearned.md
Normal file
17
Top150Interview/LessonsLearned.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Lessons Learned from LeetCode
|
||||||
|
## Slicing
|
||||||
|
- How to use slicing for shifting elements
|
||||||
|
|
||||||
|
```
|
||||||
|
my_list = [1, 2, 3, 4, 5]
|
||||||
|
|
||||||
|
# Shift list to the right using positive slicing
|
||||||
|
shifted_positive = my_list[-2:] + my_list[:-2]
|
||||||
|
print(shifted_positive) # Output: [4, 5, 1, 2, 3]
|
||||||
|
|
||||||
|
# Shift list to the left using negative slicing
|
||||||
|
shifted_negative = my_list[2:] + my_list[:2]
|
||||||
|
print(shifted_negative) # Output: [3, 4, 5, 1, 2]
|
||||||
|
|
||||||
|
```
|
||||||
|
-
|
||||||
Loading…
Reference in New Issue
Block a user