Skip to content

Commit 8ab8123

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 118, 509, 740, 746, 931 and 1137
1 parent 4273240 commit 8ab8123

13 files changed

+309
-0
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@
2222

2323
This repository contains awesome LeetCode problems and solutions written in Python to prepare for coding interviews.
2424

25+
## Leetcode Study Plans 📚
26+
27+
### Introduction To
28+
29+
- [Introduction to Pandas](https://leetcode.com/studyplan/introduction-to-pandas/)
30+
31+
### 30 Days Challenge
32+
33+
- [30 Days of Pandas](https://leetcode.com/studyplan/30-days-of-pandas/)
34+
- [30 Days of JavaScript](https://leetcode.com/studyplan/30-days-of-javascript/)
35+
36+
### Cracking Coding Interview
37+
38+
- [Top Interview 150](https://leetcode.com/studyplan/top-interview-150/)
39+
- [LeetCode 75](https://leetcode.com/studyplan/leetcode-75/)
40+
- [Top 100 Liked](https://leetcode.com/studyplan/top-100-liked/)
41+
- [SQL 50](https://leetcode.com/studyplan/top-sql-50/)
42+
- [Premium Algo 100](https://leetcode.com/studyplan/premium-algo-100/)
43+
- [Advanced SQL 50](https://leetcode.com/studyplan/premium-sql-50/)
44+
45+
### Sprint Interview
46+
47+
- [Amazon Spring '23 High Frequency](https://leetcode.com/studyplan/amazon-spring-23-high-frequency/)
48+
- [Google Spring '23 High Frequency](https://leetcode.com/studyplan/google-spring-23-high-frequency/)
49+
- [TikTok Spring '23 High Frequency](https://leetcode.com/studyplan/tiktok-spring-23-high-frequency/)
50+
- [Apple Spring '23 High Frequency](https://leetcode.com/studyplan/apple-spring-23-high-frequency/)
51+
52+
### In-Depth Topics
53+
54+
- [Programming Skills](https://leetcode.com/studyplan/programming-skills/)
55+
- [Dynamic Programming](https://leetcode.com/studyplan/dynamic-programming/)
56+
- [Dynamic Programming Grandmaster](https://leetcode.com/studyplan/dynamic-programming-grandmaster/)
57+
- [Graph Theory](https://leetcode.com/studyplan/graph-theory/)
58+
- [Binary Search](https://leetcode.com/studyplan/binary-search/)
59+
2560
## Leetcode Problems & Solutions 💻
2661

2762
- [1 Two Sum](https://leetcode.com/problems/two-sum/description/)
@@ -99,6 +134,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
99134
- [112 Path Sum](https://leetcode.com/problems/path-sum/description/)
100135
- [114 Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/)
101136
- [117 Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/)
137+
- [118 Pascals Triangle](https://leetcode.com/problems/pascals-triangle/description/)
102138
- [120 Triangle](https://leetcode.com/problems/triangle/description/)
103139
- [121 Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
104140
- [122 Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
@@ -172,10 +208,15 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
172208
- [433 Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/description/)
173209
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
174210
- [502 IPO](https://leetcode.com/problems/ipo/description/)
211+
- [509 Fibonacci Number](https://leetcode.com/problems/fibonacci-number/description/)
175212
- [530 Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
176213
- [637 Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
214+
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
215+
- [746 Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
177216
- [909 Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders/description/)
178217
- [918 Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/description/)
218+
- [931 Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/description/)
219+
- [1137 N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/description/)
179220

180221
## Development 🔧
181222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def tribonacci(self, n: int) -> int:
5+
"""
6+
The Tribonacci sequence Tn is defined as follows:
7+
8+
T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
9+
10+
Given n, return the value of Tn.
11+
"""
12+
if n == 0:
13+
return 0
14+
elif n <= 2:
15+
return 1
16+
17+
dp = [0 for _ in range(n + 1)]
18+
dp[1] = 1
19+
dp[2] = 1
20+
21+
for i in range(3, n + 1):
22+
dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3]
23+
24+
return dp[-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def generate(self, numRows: int) -> List[List[int]]:
8+
"""
9+
Given an integer numRows, return the first numRows of Pascal's triangle.
10+
11+
In Pascal's triangle, each number is the sum of the two numbers directly above
12+
it as shown:
13+
numRows = 5
14+
Output = [
15+
[1],
16+
[1, 1],
17+
[1, 2, 1],
18+
[1, 3, 3, 1],
19+
[1, 4, 6, 4, 1],
20+
[1, 5, 10, 10, 5, 1],
21+
]
22+
"""
23+
dp = [[1 for _ in range(i)] for i in range(1, numRows + 1)]
24+
for i in range(2, numRows):
25+
for j in range(1, len(dp[i]) - 1):
26+
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
27+
return dp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def fib(self, n: int) -> int:
5+
"""
6+
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the
7+
Fibonacci sequence, such that each number is the sum of the two preceding ones,
8+
starting from 0 and 1. That is,
9+
10+
F(0) = 0, F(1) = 1
11+
F(n) = F(n - 1) + F(n - 2), for n > 1.
12+
13+
Given n, calculate F(n).
14+
"""
15+
if n == 0:
16+
return 0
17+
elif n <= 1:
18+
return 1
19+
20+
dp = [0 for _ in range(n + 1)]
21+
dp[1] = 1
22+
23+
for i in range(2, n + 1):
24+
dp[i] = dp[i - 1] + dp[i - 2]
25+
26+
return dp[-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import collections
2+
from typing import List
3+
4+
5+
class Solution:
6+
"""Base class for all LeetCode Problems."""
7+
8+
def deleteAndEarn(self, nums: List[int]) -> int:
9+
"""
10+
You are given an integer array nums. You want to maximize the number of points
11+
you get by performing the following operation any number of times:
12+
- Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must
13+
delete every element equal to nums[i] - 1 and every element equal to
14+
nums[i] + 1.
15+
16+
Return the maximum number of points you can earn by applying the above
17+
operation some number of times.
18+
"""
19+
if len(nums) == 1:
20+
return nums[0]
21+
22+
count = collections.defaultdict(int)
23+
minVal, maxVal = float("inf"), -float("inf")
24+
for n in nums:
25+
count[n] += n
26+
minVal = min(minVal, n)
27+
maxVal = max(maxVal, n)
28+
29+
dp = [0 for i in range(maxVal + 1)]
30+
for i in range(minVal, maxVal + 1):
31+
dp[i] = max(dp[i - 2] + count[i], dp[i - 1])
32+
33+
return max(dp[-1], dp[-2])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minCostClimbingStairs(self, cost: List[int]) -> int:
8+
"""
9+
You are given an integer array cost where cost[i] is the cost of ith step on a
10+
staircase. Once you pay the cost, you can either climb one or two steps.
11+
12+
You can either start from the step with index 0, or the step with index 1.
13+
14+
Return the minimum cost to reach the top of the floor.
15+
"""
16+
dp = [0 for _ in range(len(cost))]
17+
for i in range(len(cost)):
18+
dp[i] = cost[i] + min(dp[i - 1], dp[i - 2])
19+
return min(dp[-1], dp[-2])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minFallingPathSum(self, matrix: List[List[int]]) -> int:
8+
"""
9+
Given an n x n array of integers matrix, return the minimum sum of any falling
10+
path through matrix.
11+
12+
A falling path starts at any element in the first row and chooses the element
13+
in the next row that is either directly below or diagonally left/right.
14+
Specifically, the next element from position (row, col) will be
15+
(row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
16+
"""
17+
dp = [
18+
[matrix[i][j] if i == 0 else 0 for j in range(len(matrix[i]))]
19+
for i in range(len(matrix))
20+
]
21+
22+
for i in range(1, len(matrix)):
23+
for j in range(len(matrix[i])):
24+
if j == 0:
25+
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + 1]) + matrix[i][j]
26+
elif j == len(matrix[i]) - 1:
27+
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j]) + matrix[i][j]
28+
else:
29+
dp[i][j] = (
30+
min(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1])
31+
+ matrix[i][j]
32+
)
33+
return min(dp[-1])
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._1137_nth_tribonacci_number import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(4, 4),
10+
(25, 1389537),
11+
],
12+
)
13+
def test_func(n: int, expected: int):
14+
"""Tests the solution of a LeetCode problem."""
15+
tn = Solution().tribonacci(n)
16+
assert tn == expected

tests/test_118_pascals_triangle.py

+18
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._118_pascals_triangle import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["numRows", "expected"],
10+
argvalues=[
11+
(5, [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]),
12+
(1, [[1]]),
13+
],
14+
)
15+
def test_func(numRows: int, expected: List[List[int]]):
16+
"""Tests the solution of a LeetCode problem."""
17+
pascals = Solution().generate(numRows)
18+
assert pascals == expected

tests/test_509_fibonacci_number.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._509_fibonacci_number import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(2, 1),
10+
(3, 2),
11+
(4, 3),
12+
],
13+
)
14+
def test_func(n: int, expected: int):
15+
"""Tests the solution of a LeetCode problem."""
16+
fn = Solution().fib(n)
17+
assert fn == expected

tests/test_740_delete_and_earn.py

+19
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._740_delete_and_earn import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([3, 4, 2], 6),
12+
([2, 2, 3, 3, 3, 4], 9),
13+
([1], 1),
14+
],
15+
)
16+
def test_func(nums: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
earned = Solution().deleteAndEarn(nums)
19+
assert earned == expected
+18
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._746_min_cost_climbing_stairs import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["cost", "expected"],
10+
argvalues=[
11+
([10, 15, 20], 15),
12+
([1, 100, 1, 1, 1, 100, 1, 1, 100, 1], 6),
13+
],
14+
)
15+
def test_func(cost: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
min_cost = Solution().minCostClimbingStairs(cost)
18+
assert min_cost == expected
+18
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._931_minimum_falling_path_sum import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["matrix", "expected"],
10+
argvalues=[
11+
([[2, 1, 3], [6, 5, 4], [7, 8, 9]], 13),
12+
([[-19, 57], [-40, -5]], -59),
13+
],
14+
)
15+
def test_func(matrix: List[List[int]], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
falling_sum = Solution().minFallingPathSum(matrix)
18+
assert falling_sum == expected

0 commit comments

Comments
 (0)