-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Diald patch 7 #9356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Diald patch 7 #9356
Changes from 16 commits
3491a98
a052365
f79680b
0a192e8
2fe6444
3f95987
e2a0a4f
1997470
4620267
4608a23
1e6b33b
7e934f1
41d6d00
917c546
3e8922e
c6f17e5
0842377
cb1d9ce
dbd7e88
0199e39
8e0ab53
c640e6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Given an integer array nums, find the subarray with the largest sum, and return its sum. | ||
Example 1: | ||
Input: nums = [-2,1,-3,4,-1,2,1,-5,4] | ||
Output: 6 | ||
Explanation: The subarray [4,-1,2,1] has the largest sum 6. | ||
Example 2: | ||
Input: nums = [1] | ||
Output: 1 | ||
Explanation: The subarray [1] has the largest sum 1. | ||
Example 3: | ||
Input: nums = [5,4,-1,7,8] | ||
Output: 23 | ||
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. | ||
""" | ||
|
||
|
||
def max_subArray(self, nums: List[int]) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file |
||
# kadane's algorithm# | ||
curr = 0 | ||
maxtillnow = -inf | ||
for c in nums: | ||
curr = max(c, curr + c) | ||
maxtillnow = max(curr, maxtillnow) | ||
return maxtillnow | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""<----------------------------------------2 POINTER SOLUTION ---------------------------------------> | ||
|
||
Given a 1-indexed array of integers numbers that is | ||
already sorted | ||
in non-decreasing order, find two numbers such that they | ||
add up to a specific target number. | ||
Let these two numbers be numbers[index1] and numbers[index2] | ||
where 1 <= index1 < index2 < numbers.length. | ||
Return the indices of the two numbers, index1 and index2, | ||
added by one as an integer array [index1, index2] of length 2. | ||
The tests are generated such that there is exactly one solution. | ||
You may not use the same element twice. | ||
Your solution must use only constant extra space. | ||
Example 1: | ||
|
||
Input: numbers = [2,7,11,15], target = 9 | ||
Output: [1,2] | ||
Explanation: The sum of 2 and 7 is 9. | ||
Therefore, index1 = 1, index2 = 2. We return [1, 2]. | ||
Example 2: | ||
|
||
Input: numbers = [2,3,4], target = 6 | ||
Output: [1,3] | ||
Explanation: The sum of 2 and 4 is 6. | ||
Therefore index1 = 1, index2 = 3. We return [1, 3]. | ||
Example 3: | ||
|
||
Input: numbers = [-1,0], target = -1 | ||
Output: [1,2] | ||
Explanation: The sum of -1 and 0 is -1. | ||
Therefore index1 = 1, index2 = 2. We return [1, 2]. | ||
""" | ||
|
||
|
||
def two_sum(self, numbers: list[int], target: int) -> list[int]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
n = len(numbers) | ||
l = 0 | ||
r = n - 1 | ||
res = [] | ||
while l < r: | ||
if numbers[l] + numbers[r] == target: | ||
res.append(l + 1) | ||
res.append(r + 1) | ||
if numbers[l] + numbers[r] > target: | ||
r -= 1 | ||
else: | ||
l += 1 | ||
res.sort() | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ pandas | |
pillow | ||
projectq | ||
qiskit | ||
qiskit-aer | ||
requests | ||
rich | ||
scikit-fuzzy | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable and function names should follow the
snake_case
naming convention. Please update the following name accordingly:max_subArray
As there is no test file in this pull request nor any test function or class in the file
data_structures/arrays/kadanes_algorithm.py
, please provide doctest for the functionmax_subArray