Skip to content

Commit f8af46c

Browse files
committed
Solution of 1184, 1185, 1186
1 parent e0d010c commit f8af46c

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

1100-1200q/1184.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.
3+
4+
The bus goes along both directions i.e. clockwise and counterclockwise.
5+
6+
Return the shortest distance between the given start and destination stops.
7+
8+
9+
10+
Example 1:
11+
Input: distance = [1,2,3,4], start = 0, destination = 1
12+
Output: 1
13+
Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.
14+
15+
16+
Example 2:
17+
Input: distance = [1,2,3,4], start = 0, destination = 2
18+
Output: 3
19+
Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.
20+
21+
22+
Example 3:
23+
Input: distance = [1,2,3,4], start = 0, destination = 3
24+
Output: 4
25+
Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
26+
27+
28+
Constraints:
29+
30+
1 <= n <= 10^4
31+
distance.length == n
32+
0 <= start, destination < n
33+
0 <= distance[i] <= 10^4
34+
'''
35+
36+
class Solution(object):
37+
def distanceBetweenBusStops(self, distance, start, destination):
38+
"""
39+
:type distance: List[int]
40+
:type start: int
41+
:type destination: int
42+
:rtype: int
43+
"""
44+
start, destination = min(start, destination), max(start, destination)
45+
clock_dist = sum(distance[start:destination])
46+
anti_clock_dist = sum(distance[:start]) + sum(distance[destination:])
47+
return min(clock_dist, anti_clock_dist)

1100-1200q/1185.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given a date, return the corresponding day of the week for that date.
3+
4+
The input is given as three integers representing the day, month and year respectively.
5+
6+
Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.
7+
8+
9+
10+
Example 1:
11+
12+
Input: day = 31, month = 8, year = 2019
13+
Output: "Saturday"
14+
Example 2:
15+
16+
Input: day = 18, month = 7, year = 1999
17+
Output: "Sunday"
18+
Example 3:
19+
20+
Input: day = 15, month = 8, year = 1993
21+
Output: "Sunday"
22+
23+
24+
Constraints:
25+
26+
The given dates are valid dates between the years 1971 and 2100.
27+
'''
28+
class Solution(object):
29+
def dayOfTheWeek(self, day, month, year):
30+
"""
31+
:type day: int
32+
:type month: int
33+
:type year: int
34+
:rtype: str
35+
"""
36+
day_of_week_map = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
37+
t = [ 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 ]
38+
year -= month < 3
39+
return day_of_week_map[((year + int(year / 4) - int(year / 100) + int(year / 400) + t[month - 1] + day) % 7)]

1100-1200q/1186.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.
3+
4+
Note that the subarray needs to be non-empty after deleting one element.
5+
6+
7+
8+
Example 1:
9+
10+
Input: arr = [1,-2,0,3]
11+
Output: 4
12+
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.
13+
Example 2:
14+
15+
Input: arr = [1,-2,-2,3]
16+
Output: 3
17+
Explanation: We just choose [3] and it's the maximum sum.
18+
Example 3:
19+
20+
Input: arr = [-1,-1,-1,-1]
21+
Output: -1
22+
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.
23+
24+
25+
Constraints:
26+
27+
1 <= arr.length <= 10^5
28+
-10^4 <= arr[i] <= 10^4
29+
'''
30+
31+
class Solution(object):
32+
def maximumSum(self, arr):
33+
"""
34+
:type arr: List[int]
35+
:rtype: int
36+
"""
37+
forward = [0] * len(arr)
38+
backward = [0] * len(arr)
39+
40+
curr_max, max_so_far = arr[0], arr[0]
41+
forward[0] = arr[0]
42+
for index in range(1, len(arr)):
43+
curr_max = max(arr[index], curr_max + arr[index])
44+
max_so_far = max(max_so_far, curr_max)
45+
46+
forward[index] = curr_max
47+
48+
curr_max = arr[len(arr) - 1]
49+
max_so_far = arr[len(arr) - 1]
50+
backward[len(arr) - 1] = arr[len(arr) - 1]
51+
52+
index = len(arr) - 2
53+
while index >= 0:
54+
curr_max = max(arr[index], curr_max + arr[index])
55+
max_so_far = max(max_so_far, curr_max)
56+
57+
backward[index] = curr_max
58+
index -= 1
59+
60+
result = max_so_far
61+
for index in range(1, len(arr)-1):
62+
result = max(result, forward[index-1] + backward[index + 1])
63+
return result

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1313

1414

1515
##### [Problems 1100-1200](./1100-1200q/)
16-
16+
| # | Title | Solution | Difficulty |
17+
|---| ----- | -------- | ---------- |
18+
|1186|[Maximum Subarray Sum with One Deletion](https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/)|[Python](./1100-1200q/1186.py)|Medium|
19+
|1185|[Day of the Week](https://leetcode.com/problems/day-of-the-week/)|[Python](./1100-1200q/1185.py)|Easy|
20+
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/)|[Python](./1100-1200q/1184.py)|Easy|
1721

1822
##### [Problems 1000-1100](./1000-1100q/)
1923
| # | Title | Solution | Difficulty |

0 commit comments

Comments
 (0)