Skip to content

Commit b42ea71

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 1035 and 1312
1 parent fe5c8a6 commit b42ea71

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,10 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
232232
- [918 Maximum Sum Circular Subarray](https://leetcode.com/problems/maximum-sum-circular-subarray/description/)
233233
- [931 Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/description/)
234234
- [983 Minimum Cost for Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/description/)
235+
- [1035 Uncrossed Lines](https://leetcode.com/problems/uncrossed-lines/description/)
235236
- [1137 N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/description/)
236237
- [1143 Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/description/)
238+
- [1312 Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/description/)
237239
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description)
238240
- [1922 Count Good Numbers](https://leetcode.com/problems/count-good-numbers/description/)
239241
- [2140 Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/description/)
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 maxUncrossedLines(self, nums1: List[int], nums2: List[int]) -> int:
8+
"""
9+
You are given two integer arrays nums1 and nums2. We write the integers of
10+
nums1 and nums2 (in the order they are given) on two separate horizontal lines.
11+
12+
We may draw connecting lines: a straight line connecting two numbers nums1[i]
13+
and nums2[j] such that:
14+
- nums1[i] == nums2[j], and
15+
- the line we draw does not intersect any other connecting (non-horizontal)
16+
line.
17+
18+
Note that a connecting line cannot intersect even at the endpoints (i.e., each
19+
number can only belong to one connecting line).
20+
21+
Return the maximum number of connecting lines we can draw in this way.
22+
"""
23+
dp = [[0 for _ in range(len(nums2) + 1)] for _ in range(len(nums1) + 1)]
24+
for i in range(len(nums1) - 1, -1, -1):
25+
for j in range(len(nums2) - 1, -1, -1):
26+
if nums1[i] == nums2[j]:
27+
dp[i][j] = 1 + dp[i + 1][j + 1]
28+
else:
29+
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1])
30+
return dp[0][0]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def minInsertions(self, s: str) -> int:
5+
"""
6+
Given a string s. In one step you can insert any character at any index of the
7+
string.
8+
9+
Return the minimum number of steps to make s palindrome.
10+
11+
A Palindrome String is one that reads the same backward as well as forward.
12+
"""
13+
dp = [[0 for _ in range(len(s) + 1)] for _ in range(len(s) + 1)]
14+
for i in range(len(s) - 1, -1, -1):
15+
for j in range(len(s) - 1, -1, -1):
16+
if s[i] == s[len(s) - 1 - j]:
17+
dp[i][j] = 1 + dp[i + 1][j + 1]
18+
else:
19+
dp[i][j] = max(dp[i + 1][j], dp[i][j + 1])
20+
return len(s) - dp[0][0]

tests/test_1035_uncrossed_lines.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._1035_uncrossed_lines import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums1", "nums2", "expected"],
10+
argvalues=[
11+
([1, 4, 2], [1, 2, 4], 2),
12+
([2, 5, 1, 2, 5], [10, 5, 2, 1, 5, 2], 3),
13+
([1, 3, 7, 1, 7, 5], [1, 9, 2, 5, 1], 2),
14+
],
15+
)
16+
def test_func(nums1: List[int], nums2: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
max_crossed_lines = Solution().maxUncrossedLines(nums1, nums2)
19+
assert max_crossed_lines == expected
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# flake8: noqa
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1312_minimum_insertion_steps_to_make_a_string_palindrome import (
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["s", "expected"],
12+
argvalues=[
13+
("zzazz", 0),
14+
("mbadm", 2),
15+
("leetcode", 5),
16+
],
17+
)
18+
def test_func(s: str, expected: int):
19+
"""Tests the solution of a LeetCode problem."""
20+
min_insertions = Solution().minInsertions(s)
21+
assert min_insertions == expected

0 commit comments

Comments
 (0)