Skip to content

Commit 7960529

Browse files
committed
Added committed
1 parent 3f61aee commit 7960529

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
"""
4+
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
5+
Example 1:
6+
7+
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
8+
Output: 6
9+
Explanation: [1,1,1,0,0,1,1,1,1,1,1]
10+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
11+
Example 2:
12+
13+
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
14+
Output: 10
15+
Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
16+
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
17+
18+
19+
Constraints:
20+
21+
1 <= nums.length <= 105
22+
nums[i] is either 0 or 1.
23+
0 <= k <= nums.length
24+
"""
25+
26+
class Solution:
27+
def longestOnes(self, nums: List[int], k: int) -> int:
28+
left = 0
29+
current_count = 0
30+
max_count = 0
31+
for right in range(len(nums)):
32+
if nums[right] == 0:
33+
current_count += 1
34+
35+
while current_count > k:
36+
if nums[left] == 0:
37+
current_count -= 1
38+
left += 1
39+
40+
max_count = max(max_count, right - left + 1)
41+
return max_count
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Sliding Window Technique
2+
The sliding window technique involves maintaining a window that can expand and contract as needed to meet the conditions of the problem. Here's the step-by-step approach:
3+
4+
Initialize Variables:
5+
6+
left pointer starting at the beginning of the array.
7+
current_sum to keep track of the sum of the current window.
8+
min_length to store the minimum length of the valid subarray found.
9+
Expand and Contract the Window:
10+
11+
Expand the window by moving the right pointer and adding the element at the right pointer to current_sum.
12+
Once current_sum is greater than or equal to target, contract the window from the left side to see if we can get a smaller valid window.
13+
Update min_length whenever a valid window is found.
14+
Return the Result:
15+
16+
If min_length is not updated, return 0 indicating no valid subarray was found.
17+
Otherwise, return min_length."""
18+
19+
def min_sub_array_len(target: int, nums: list[int]):
20+
n = len(nums)
21+
left = 0
22+
current_sum = 0
23+
min_length = float('inf')
24+
25+
for right in range(n):
26+
current_sum += nums[right]
27+
28+
while current_sum >= target:
29+
min_length = min(min_length, right - left + 1)
30+
current_sum -= nums[left]
31+
left += 1
32+
33+
return 0 if min_length == float('inf') else min_length
34+
35+
# Example usage
36+
target = 7
37+
nums = [2, 3, 1, 2, 4, 3]
38+
print(min_sub_array_len(target, nums)) # Output: 2
39+
40+
target = 4
41+
nums = [1, 4, 4]
42+
print(min_sub_array_len(target, nums)) # Output: 1
43+
44+
target = 11
45+
nums = [1, 1, 1, 1, 1, 1, 1, 1]
46+
print(min_sub_array_len(target, nums)) # Output: 0
47+
48+
"""
49+
Explanation of the Code
50+
Initialization:
51+
52+
left is initialized to 0.
53+
current_sum starts at 0.
54+
min_length is set to infinity to ensure any valid subarray length found will be smaller.
55+
Main Loop:
56+
57+
The right pointer iterates over the array.
58+
current_sum is updated to include nums[right].
59+
Inner While Loop:
60+
61+
Checks if current_sum is greater than or equal to target.
62+
If true, it updates min_length and contracts the window from the left by incrementing left and subtracting nums[left] from current_sum.
63+
Return Result:
64+
65+
If min_length remains infinity, it means no valid subarray was found, so return 0.
66+
Otherwise, return min_length.
67+
Time Complexity:
68+
The time complexity is O(n) since both the left and right pointers traverse the array at most once."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from typing import Any
2+
3+
class Node:
4+
def __init__(self, data: Any):
5+
self.data = data
6+
self.next : Node | None = None
7+
8+
9+
class SinglyLinkedList:
10+
def __init__(self):
11+
self.head: Node | None = None
12+
self.tail: Node | None = None
13+
14+
def append(self, data: Any):
15+
"""Append a node with the given data to the end of the list."""
16+
new_node = Node(data)
17+
if not self.head:
18+
self.head = new_node
19+
self.tail = new_node
20+
else:
21+
self.tail.next = new_node
22+
self.tail = new_node
23+
24+
def prepend(self, data):
25+
"""Prepend a node with the given data to the start of the list."""
26+
new_node = Node(data)
27+
if not self.head:
28+
self.head = new_node
29+
self.tail = new_node
30+
else:
31+
new_node.next = self.head
32+
self.head = new_node
33+
34+
def delete_node(self, key):
35+
"""Delete the first node with the given data."""
36+
current = self.head
37+
if current and current.data == key:
38+
self.head = current.next
39+
if self.head is None: # List becomes empty
40+
self.tail = None
41+
current = None
42+
return
43+
44+
prev = None
45+
while current and current.data != key:
46+
prev = current
47+
current = current.next
48+
49+
if current is None:
50+
return
51+
52+
prev.next = current.next
53+
if current.next is None: # Deleted node was the tail
54+
self.tail = prev
55+
current = None
56+
57+
def search(self, key):
58+
"""Search for a node with the given data."""
59+
current = self.head
60+
while current and current.data != key:
61+
current = current.next
62+
return current
63+
64+
def print_list(self):
65+
"""Print the entire list."""
66+
current = self.head
67+
while current:
68+
print(current.data, end=" -> ")
69+
current = current.next
70+
print("None")
71+
72+
def insert_after_node(self, prev_node, data):
73+
"""Insert a node with the given data after a specified node."""
74+
if not prev_node:
75+
print("Previous node does not exist.")
76+
return
77+
new_node = Node(data)
78+
new_node.next = prev_node.next
79+
prev_node.next = new_node
80+
if new_node.next is None: # New node becomes the tail
81+
self.tail = new_node
82+
83+
def length(self):
84+
"""Return the length of the linked list."""
85+
count = 0
86+
current = self.head
87+
while current:
88+
count += 1
89+
current = current.next
90+
return count
91+
92+
def reverse(self):
93+
"""Reverse the linked list."""
94+
prev = None
95+
current = self.head
96+
self.tail = self.head # Update the tail to be the old head
97+
while current:
98+
next_node = current.next
99+
current.next = prev
100+
prev = current
101+
current = next_node
102+
self.head = prev
103+
104+
# Example usage
105+
if __name__ == "__main__":
106+
linked_list = SinglyLinkedList()
107+
linked_list.append(1)
108+
linked_list.append(2)
109+
linked_list.append(3)
110+
linked_list.prepend(0)
111+
linked_list.print_list() # Output: 0 -> 1 -> 2 -> 3 -> None
112+
113+
linked_list.delete_node(2)
114+
linked_list.print_list() # Output: 0 -> 1 -> 3 -> None
115+
116+
print("Length:", linked_list.length()) # Output: Length: 3
117+
118+
linked_list.reverse()
119+
linked_list.print_list() # Output: 3 -> 1 -> 0 -> None
120+
121+
found_node = linked_list.search(1)
122+
print("Found:", found_node.data if found_node else "Not Found") # Output: Found: 1
123+
124+
linked_list.insert_after_node(found_node, 2)
125+
linked_list.print_list() # Output: 3 -> 1 -> 2 -> 0 -> None

0 commit comments

Comments
 (0)