Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 419d30a

Browse files
Added new Solutions
1 parent 50a89a3 commit 419d30a

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

‎coding_solutions/Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ This repository contains my answers to all Leetcode/HackerRank algorithm questio
1010
| 3 | [Search In Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/SearchInRotatedSortedArray.py) |
1111
| 4 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/MergeSorted.py) |
1212
| 5 | [Search In Rotated Sorted Array](https://leetcode.com/problems/first-bad-version/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/FirstBadVersion.py) |
13-
| 6 | [Search In Rotated Sorted Array](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/BestTimeToBuySell.py) |
13+
| 6 | [Search In Rotated Sorted Array](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/[DP]ClimbingStairs.py) |
14+
| 7 | [Search In Rotated Sorted Array](https://leetcode.com/problems/maximum-subarray/)| [python3](https://github.com/sushant097/Data-Structure-Algorithms-Collections-Python/blob/master/coding_solutions/interview_related/[DP]BestTimeToBuySell.py) |
15+

‎coding_solutions/interview_related/BestTimeToBuySell.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
3+
4+
A subarray is a contiguous part of an array.
5+
6+
Example 1:
7+
8+
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
9+
Output: 6
10+
Explanation: [4,-1,2,1] has the largest sum = 6.
11+
Example 2:
12+
13+
Input: nums = [1]
14+
Output: 1
15+
16+
17+
'''
18+
19+
class Solution:
20+
def maxSubArray(self, nums: List[int]) -> int:
21+
curSum = 0
22+
maxSub = nums[0]
23+
24+
for n in nums:
25+
if curSum < 0:
26+
curSum = 0
27+
28+
curSum += n
29+
maxSub = max(maxSub, curSum)
30+
31+
return maxSub
32+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /