Skip to content

Commit 6f9f478

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 724, 1732 and 1991
1 parent 4eb3ff0 commit 6f9f478

7 files changed

+146
-2
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
- [61 Rotate List](https://leetcode.com/problems/rotate-list/description/)
107107
- [63 Unique Paths II](https://leetcode.com/problems/unique-paths-ii/description/)
108108
- [64 Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/description/)
109-
- [66 Plus One](https://leetcode.com/problems/plus-one/description)
109+
- [66 Plus One](https://leetcode.com/problems/plus-one/description/)
110110
- [67 Add Binary](https://leetcode.com/problems/add-binary/description/)
111111
- [68 Text Justification](https://leetcode.com/problems/text-justification/description/)
112112
- [69 Sqrt(x)](https://leetcode.com/problems/sqrtx/description/)
@@ -229,6 +229,7 @@
229229
- [673 Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/)
230230
- [712 Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/)
231231
- [714 Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
232+
- [724 Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)
232233
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
233234
- [746 Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
234235
- [790 Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/description/)
@@ -242,7 +243,9 @@
242243
- [1143 Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/description/)
243244
- [1218 Longest Arithmetic Subsequence of Given Difference](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/description/)
244245
- [1312 Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/description/)
245-
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description)
246+
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description/)
247+
- [1732 Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/description/)
248+
- [1991 Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array/description/)
246249
- [1922 Count Good Numbers](https://leetcode.com/problems/count-good-numbers/description/)
247250
- [1964 Find the Longest Valid Obstacle Course at Each Position](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/description/)
248251
- [2140 Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/description/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def largestAltitude(self, gain: List[int]) -> int:
8+
"""
9+
There is a biker going on a road trip. The road trip consists of n + 1 points
10+
at different altitudes. The biker starts his trip on point 0 with altitude
11+
equal 0.
12+
13+
You are given an integer array gain of length n where gain[i] is the net gain
14+
in altitude between points i and i + 1 for all (0 <= i < n). Return the highest
15+
altitude of a point.
16+
"""
17+
# Time complexity: O(n)
18+
# Space complexity: O(1)
19+
res = 0
20+
altitude = 0
21+
for g in gain:
22+
altitude += g
23+
res = max(res, altitude)
24+
return res
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def findMiddleIndex(self, nums: List[int]) -> int:
8+
"""
9+
Given a 0-indexed integer array nums, find the leftmost middleIndex (i.e., the
10+
smallest amongst all the possible ones).
11+
12+
A middleIndex is an index where nums[0] + nums[1] + ... +
13+
nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ... +
14+
nums[nums.length-1].
15+
16+
If middleIndex == 0, the left side sum is considered to be 0. Similarly, if
17+
middleIndex == nums.length - 1, the right side sum is considered to be 0.
18+
19+
Return the leftmost middleIndex that satisfies the condition, or -1 if there is
20+
no such index.
21+
"""
22+
# Time complexity: O(n)
23+
# Space complexity: O(1)
24+
left = 0
25+
right = sum(nums)
26+
for i in range(len(nums)):
27+
right -= nums[i]
28+
if left == right:
29+
return i
30+
left += nums[i]
31+
return -1
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def pivotIndex(self, nums: List[int]) -> int:
8+
"""
9+
Given an array of integers nums, calculate the pivot index of this array.
10+
11+
The pivot index is the index where the sum of all the numbers strictly to the
12+
left of the index is equal to the sum of all the numbers strictly to the
13+
index's right.
14+
15+
If the index is on the left edge of the array, then the left sum is 0 because
16+
there are no elements to the left. This also applies to the right edge of the
17+
array.
18+
19+
Return the leftmost pivot index. If no such index exists, return -1.
20+
"""
21+
# Time complexity: O(n)
22+
# Space complexity: O(1)
23+
left = 0
24+
right = sum(nums)
25+
for i in range(len(nums)):
26+
right -= nums[i]
27+
if left == right:
28+
return i
29+
left += nums[i]
30+
return -1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1732_find_the_highest_altitude import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["gain", "expected"],
10+
argvalues=[
11+
([-5, 1, 5, 0, -7], 1),
12+
([-4, -3, -2, -1, 4, 3, 2], 0),
13+
],
14+
)
15+
def test_func(gain: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
largest_altitude = Solution().largestAltitude(gain)
18+
assert largest_altitude == expected
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1991_find_the_middle_index_in_array import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([2, 3, -1, 8, 4], 3),
12+
([1, -1, 4], 2),
13+
([2, 5], -1),
14+
],
15+
)
16+
def test_func(nums: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
middle_idx = Solution().findMiddleIndex(nums)
19+
assert middle_idx == expected

tests/test_724_find_pivot_index.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._724_find_pivot_index import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 7, 3, 6, 5, 6], 3),
12+
([1, 2, 3], -1),
13+
([2, 1, -1], 0),
14+
],
15+
)
16+
def test_func(nums: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
pivot_idx = Solution().pivotIndex(nums)
19+
assert pivot_idx == expected

0 commit comments

Comments
 (0)