Skip to content

Commit 7178b6a

Browse files
committed
Included the Leetcode code
1 parent cc2298f commit 7178b6a

10 files changed

+249
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
def count_splits(nums: List[int]) -> int:
4+
total_sum = sum(nums)
5+
first_part_sum = 0
6+
count = 0
7+
8+
# Iterate through the array up to the second-to-last element
9+
for i in range(len(nums) - 1):
10+
first_part_sum += nums[i]
11+
second_part_sum = total_sum - first_part_sum
12+
13+
if first_part_sum >= second_part_sum:
14+
count += 1
15+
16+
return count
17+
18+
# Example usage
19+
nums = [10, 4, -8, 7]
20+
print(count_splits(nums)) # Output: 2
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findMaxAverage(self, nums: list[int], k: int) -> float:
3+
4+
curr_sum = sum(nums[:k])
5+
max_sum = curr_sum
6+
for j in range(k, len(nums)):
7+
curr_sum += nums[j] - nums[j-k]
8+
max_sum = max(max_sum, curr_sum)
9+
return max_sum/k
10+
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# In this problem, we are given an array matches consisting of matches [winner, loser] where winner defeats loser.
2+
3+
# We need to collect these 2 kinds of players into two lists separately:
4+
5+
# players that have not lost any matches.
6+
# players that have lost exactly one match.
7+
# then return the two lists in increasing order.
8+
9+
from collections import Counter
10+
11+
def findWinners(matches: list[list[int]]) -> list[list[int]]:
12+
losses_count = Counter()
13+
for winner, loser in matches:
14+
losses_count[loser] += 1
15+
losses_count[winner] += 0
16+
17+
winners = []
18+
almost_winners = []
19+
for player, losses in losses_count.items():
20+
if losses == 0:
21+
winners.append(player)
22+
elif losses == 1:
23+
almost_winners.append(player)
24+
25+
return [sorted(winners), sorted(almost_winners)]
26+
27+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
def is_subsequence(s: str, t: str) -> bool:
3+
"""
4+
Check if a string is a subsequence of another string. (skip characters in between - allowed)
5+
:param s: string
6+
:param t: string
7+
:return: bool
8+
"""
9+
10+
i = j = 0
11+
12+
while i < len(s) and j < len(t):
13+
if s[i] == t[j]:
14+
# advance the pointer of the subsequence string
15+
i += 1
16+
# advance the pointer of the main string
17+
j += 1
18+
return i == len(s)
19+
20+
if __name__ == '__main__':
21+
print(is_subsequence("ace", "abcde")) # Output: True

data_structures/arrays/longestOnes.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def longestOnes(self, nums: list[int], k: int) -> int:
3+
left = 0
4+
current_count = 0
5+
max_count = 0
6+
for right in range(len(nums)):
7+
if nums[right] == 0:
8+
current_count += 1
9+
10+
while current_count > k:
11+
if nums[left] == 0:
12+
current_count -= 1
13+
left += 1
14+
15+
max_count = max(max_count, right - left + 1)
16+
return max_count
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
class Solution:
4+
def maxSubArray(self, nums: List[int]) -> int:
5+
# Initialize with the first element
6+
max_current = max_global = nums[0]
7+
8+
# Iterate through the array starting from the second element
9+
for num in nums[1:]:
10+
max_current = max(num, max_current + num)
11+
max_global = max(max_global, max_current)
12+
13+
return max_global
14+
15+
# Example usage
16+
solution = Solution()
17+
nums = [-2,1,-3,4,-1,2,1,-5,4]
18+
print(solution.maxSubArray(nums)) # Output: 6
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
class Solution:
4+
def minSubArray(self, nums: List[float]) -> float:
5+
# Initialize with the first element
6+
min_current = min_global = nums[0]
7+
8+
# Iterate through the array starting from the second element
9+
for num in nums[1:]:
10+
min_current = min(num, min_current + num)
11+
min_global = min(min_global, min_current)
12+
13+
return min_global
14+
15+
# Example usage
16+
solution = Solution()
17+
nums = [1.1, -2.3, 3.4, -4.5, 5.6, -6.7]
18+
print(solution.minSubArray(nums)) # Output: -6.7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def num_subarrays_with_product_less_than_k(arr, k):
2+
if k <= 1:
3+
return 0
4+
5+
left = 0
6+
product = 1
7+
count = 0
8+
9+
for right in range(len(arr)):
10+
product *= arr[right]
11+
12+
while product >= k and left <= right:
13+
product /= arr[left]
14+
left += 1
15+
16+
count += right - left + 1
17+
18+
return count
19+
20+
# Example usage
21+
arr = [10, 5, 2, 6]
22+
k = 100
23+
print(num_subarrays_with_product_less_than_k(arr, k)) # Output: 8
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
class Solution:
3+
def sortedSquares(self, nums: List[int]) -> List[int]:
4+
n = len(nums)
5+
l = 0
6+
r = n - 1
7+
result = [0]*len(nums)
8+
9+
for i in range(n - 1, -1, -1):
10+
11+
if abs(nums[l]) < abs(nums[r]):
12+
res = nums[r] ** 2
13+
r -= 1
14+
else:
15+
res = nums[l] ** 2
16+
l += 1
17+
result[i] = res
18+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from typing import Any, List
2+
3+
4+
# Base Case:
5+
6+
# If k equals n, it means we have considered all elements, and we process the current subset.
7+
# Recursive Case:
8+
9+
# The function search(k + 1) is called without including the current element k in the subset.
10+
# The element k is then added to the subset, and search(k + 1) is called again to include the current element in the subset.
11+
# After processing the subset including the current element, it is removed (subset.pop_back()) to backtrack.
12+
13+
# void search(int k) {
14+
# if (k == n) {
15+
# // process subset
16+
# } else {
17+
# search(k + 1);
18+
# subset.push_back(k);
19+
# search(k + 1);
20+
# subset.pop_back();
21+
# }
22+
# }
23+
24+
def search(k: int, n: int, subset: list[Any], all_subsets: list[list[Any]]):
25+
"""
26+
:param k: current index
27+
:param n: length of nums
28+
:param subset: current subset
29+
:param all_subsets: all subsets
30+
31+
When the function search is called with parameter k, it decides whether to
32+
include the element k in the subset or not, and in both cases, then calls itself
33+
with parameter k + 1 However, if k = n, the function notices that all elements
34+
have been processed and a subset has been generated.
35+
The following tree illustrates the function calls when n = 3. We can always
36+
choose either the left branch (k is not included in the subset) or the right branch
37+
(k is included in the subset).
38+
"""
39+
if k == n:
40+
# process subset
41+
all_subsets.append(subset[:])
42+
else:
43+
# The function search(k + 1) is called without including the current element k in the subset.
44+
search(k + 1, n, subset, all_subsets)
45+
# Include nums[k] in the subset
46+
# The element k is then added to the subset, and search(k + 1) is called again to include the current element in the subset.
47+
subset.append(k)
48+
search(k + 1, n, subset, all_subsets)
49+
50+
subset.pop()
51+
52+
def subsets_recursive(nums: List[int]) -> List[List[int]]:
53+
all_subsets = []
54+
search(0, len(nums), [], all_subsets)
55+
return all_subsets
56+
57+
58+
print(subsets_recursive([1, 2, 3])) # Output: [[], [2], [1], [1, 2], [3], [2, 3], [1, 3], [1, 2, 3]]
59+
60+
61+
62+
# def subsets(nums: List[int]) -> List[List[int]]:
63+
# result: List[List[int]] = [[]]
64+
# for num in nums:
65+
# result += [curr + [num] for curr in result]
66+
# return result
67+
68+
69+
# def subsets_bitmask(nums: List[int]) -> List[List[int]]:
70+
# n = len(nums)
71+
# result = []
72+
# for i in range(1 << n):
73+
# subset = []
74+
# for j in range(n):
75+
# if i & (1 << j):
76+
# subset.append(nums[j])
77+
# result.append(subset)
78+
# return result

0 commit comments

Comments
 (0)