Skip to content

Commit 3f5688d

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 5, 63, 64, 72, 97, 120, 123, 188 and 221
1 parent f37417b commit 3f5688d

19 files changed

+426
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
2828
- [2 Add Two Numbers](https://leetcode.com/problems/add-two-numbers/description/)
2929
- [3 Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/description/)
3030
- [4 Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/description/)
31+
- [5 Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/)
3132
- [6 Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/description/)
3233
- [9 Palindrome Number](https://leetcode.com/problems/palindrome-number/description/)
3334
- [11 Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/)
@@ -66,12 +67,15 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
6667
- [56 Merge Intervals](https://leetcode.com/problems/merge-intervals/description/)
6768
- [57 Insert Interval](https://leetcode.com/problems/insert-interval/description/)
6869
- [61 Rotate List](https://leetcode.com/problems/rotate-list/description/)
70+
- [63 Unique Paths II](https://leetcode.com/problems/unique-paths-ii/description/)
71+
- [64 Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/description/)
6972
- [66 Plus One](https://leetcode.com/problems/plus-one/description)
7073
- [67 Add Binary](https://leetcode.com/problems/add-binary/description/)
7174
- [68 Text Justification](https://leetcode.com/problems/text-justification/description/)
7275
- [69 Sqrt(x)](https://leetcode.com/problems/sqrtx/description/)
7376
- [70 Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/)
7477
- [71 Simplify Path](https://leetcode.com/problems/simplify-path/description/)
78+
- [72 Edit Distance](https://leetcode.com/problems/edit-distance/description/)
7579
- [73 Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/)
7680
- [74 Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
7781
- [76 Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/)
@@ -82,6 +86,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
8286
- [86 Partition List](https://leetcode.com/problems/partition-list/description/)
8387
- [88 Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/)
8488
- [92 Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/description/)
89+
- [97 Interleaving String](https://leetcode.com/problems/interleaving-string/description/)
8590
- [98 Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/)
8691
- [100 Same Tree](https://leetcode.com/problems/same-tree/description/)
8792
- [101 Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/)
@@ -94,8 +99,10 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
9499
- [112 Path Sum](https://leetcode.com/problems/path-sum/description/)
95100
- [114 Flatten Binary Tree to Linked List](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/description/)
96101
- [117 Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/description/)
102+
- [120 Triangle](https://leetcode.com/problems/triangle/description/)
97103
- [121 Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/)
98104
- [122 Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/)
105+
- [123 Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/)
99106
- [124 Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
100107
- [125 Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/)
101108
- [127 Word Ladder](https://leetcode.com/problems/word-ladder/description/)
@@ -122,6 +129,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
122129
- [169 Majority Element](https://leetcode.com/problems/majority-element/description/)
123130
- [172 Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/description/)
124131
- [173 Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/description/)
132+
- [188 Best Time to Buy and Sell Stock IV](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/)
125133
- [189 Rotate Array](https://leetcode.com/problems/rotate-array/description/)
126134
- [190 Reverse Bits](https://leetcode.com/problems/reverse-bits/description/)
127135
- [191 Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/description/)
@@ -138,6 +146,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
138146
- [211 Design Add and Search Words Data Structure](https://leetcode.com/problems/design-add-and-search-words-data-structure/description/)
139147
- [212 Word Search II](https://leetcode.com/problems/word-search-ii/description/)
140148
- [219 Contains Duplicates II](https://leetcode.com/problems/contains-duplicate-ii/description/)
149+
- [221 Maximal Square](https://leetcode.com/problems/maximal-square/description/)
141150
- [222 Count Complete Tree Nodes](https://leetcode.com/problems/count-complete-tree-nodes/description/)
142151
- [224 Basic Calculator](https://leetcode.com/problems/basic-calculator/description/)
143152
- [226 Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minimumTotal(self, triangle: List[List[int]]) -> int:
8+
"""
9+
Given a triangle array, return the minimum path sum from top to bottom.
10+
11+
For each step, you may move to an adjacent number of the row below. More
12+
formally, if you are on index i on the current row, you may move to either
13+
index i or index i + 1 on the next row.
14+
"""
15+
dp = [[float("inf")] * len(layer) for layer in triangle]
16+
dp[-1] = triangle[-1]
17+
for i in range(len(triangle) - 2, -1, -1):
18+
for j in range(len(triangle[i])):
19+
dp[i][j] = triangle[i][j] + min(dp[i + 1][j], dp[i + 1][j + 1])
20+
return dp[0][0]
Lines changed: 27 additions & 0 deletions
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 maxProfit(self, prices: List[int]) -> int:
8+
"""
9+
You are given an array prices where prices[i] is the price of a given stock on
10+
the ith day.
11+
12+
Find the maximum profit you can achieve. You may complete at most two
13+
transactions.
14+
15+
Note: You may not engage in multiple transactions simultaneously (i.e., you
16+
must sell the stock before you buy again).
17+
"""
18+
dp = [0 for _ in range(len(prices))]
19+
20+
for t in range(1, 3):
21+
pos = -prices[0]
22+
profit = 0
23+
for i in range(1, len(prices)):
24+
pos = max(pos, dp[i] - prices[i])
25+
profit = max(profit, pos + prices[i])
26+
dp[i] = profit
27+
return profit
Lines changed: 27 additions & 0 deletions
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 maxProfit(self, k: int, prices: List[int]) -> int:
8+
"""
9+
You are given an integer array prices where prices[i] is the price of a given
10+
stock on the ith day, and an integer k.
11+
12+
Find the maximum profit you can achieve. You may complete at most k
13+
transactions: i.e. you may buy at most k times and sell at most k times.
14+
15+
Note: You may not engage in multiple transactions simultaneously (i.e., you
16+
must sell the stock before you buy again).
17+
"""
18+
dp = [0 for _ in range(len(prices))]
19+
20+
for t in range(1, k + 1):
21+
pos = -prices[0]
22+
profit = 0
23+
for i in range(1, len(prices)):
24+
pos = max(pos, dp[i] - prices[i])
25+
profit = max(profit, pos + prices[i])
26+
dp[i] = profit
27+
return profit
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maximalSquare(self, matrix: List[List[str]]) -> int:
8+
"""
9+
Given an m x n binary matrix filled with 0's and 1's, find the largest square
10+
containing only 1's and return its area.
11+
"""
12+
dp = [[0 for _ in line] for line in matrix]
13+
maxArea = 0
14+
15+
for i in range(len(matrix) - 1, -1, -1):
16+
dp[i][-1] = int(matrix[i][-1])
17+
maxArea = max(maxArea, dp[i][-1])
18+
19+
for j in range(len(matrix[0]) - 1, -1, -1):
20+
dp[-1][j] = int(matrix[-1][j])
21+
maxArea = max(maxArea, dp[-1][j])
22+
23+
for i in range(len(matrix) - 2, -1, -1):
24+
for j in range(len(matrix[0]) - 2, -1, -1):
25+
if matrix[i][j] == "0":
26+
continue
27+
dp[i][j] = 1 + min(dp[i + 1][j], dp[i][j + 1], dp[i + 1][j + 1])
28+
maxArea = max(maxArea, dp[i][j])
29+
return maxArea**2
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def longestPalindrome(self, s: str) -> str:
5+
"""
6+
Given a string s, return the longest palindromic substring in s.
7+
8+
A string is palindromic if it reads the same forward and backward.
9+
"""
10+
res = ""
11+
for i in range(len(s)):
12+
left, right = i, i
13+
while left >= 0 and right < len(s) and s[left] == s[right]:
14+
if (right - left + 1) > len(res):
15+
res = s[left : right + 1]
16+
left -= 1
17+
right += 1
18+
19+
left, right = i, i + 1
20+
while left >= 0 and right < len(s) and s[left] == s[right]:
21+
if (right - left + 1) > len(res):
22+
res = s[left : right + 1]
23+
left -= 1
24+
right += 1
25+
return res
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
8+
"""
9+
You are given an m x n integer array grid. There is a robot initially located
10+
at the top-left corner (i.e., grid[0][0]). The robot tries to move to the
11+
bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either
12+
down or right at any point in time.
13+
14+
An obstacle and space are marked as 1 or 0 respectively in grid. A path that
15+
the robot takes cannot include any square that is an obstacle.
16+
17+
Return the number of possible unique paths that the robot can take to reach the
18+
bottom-right corner.
19+
20+
The testcases are generated so that the answer will be less than or equal to
21+
2 * 109.
22+
"""
23+
if obstacleGrid[0][0] == 1:
24+
return 0
25+
dp = [[0] * len(line) for line in obstacleGrid]
26+
dp[0][0] = 1
27+
for i in range(1, len(obstacleGrid)):
28+
if obstacleGrid[i][0]:
29+
break
30+
dp[i][0] = 1
31+
for j in range(1, len(obstacleGrid[0])):
32+
if obstacleGrid[0][j]:
33+
break
34+
dp[0][j] = 1
35+
for i in range(1, len(obstacleGrid)):
36+
for j in range(1, len(obstacleGrid[0])):
37+
if obstacleGrid[i][j]:
38+
continue
39+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
40+
return dp[-1][-1]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minPathSum(self, grid: List[List[int]]) -> int:
8+
"""
9+
Given a m x n grid filled with non-negative numbers, find a path from top left
10+
to bottom right, which minimizes the sum of all numbers along its path.
11+
12+
Note: You can only move either down or right at any point in time.
13+
"""
14+
dp = [[0] * len(line) for line in grid]
15+
dp[0][0] = grid[0][0]
16+
for j in range(1, len(dp[0])):
17+
dp[0][j] = dp[0][j - 1] + grid[0][j]
18+
for i in range(1, len(dp)):
19+
dp[i][0] = dp[i - 1][0] + grid[i][0]
20+
for i in range(1, len(dp)):
21+
for j in range(1, len(dp[i])):
22+
dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1])
23+
return dp[-1][-1]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def minDistance(self, word1: str, word2: str) -> int:
5+
"""
6+
Given two strings word1 and word2, return the minimum number of operations
7+
required to convert word1 to word2.
8+
9+
You have the following three operations permitted on a word:
10+
- Insert a character
11+
- Delete a character
12+
- Replace a character
13+
"""
14+
dp = [[0 for _ in range(len(word2) + 1)] for _ in range(len(word1) + 1)]
15+
16+
for i in range(len(word1) - 1, -1, -1):
17+
dp[i][-1] = dp[i + 1][-1] + 1
18+
19+
for j in range(len(word2) - 1, -1, -1):
20+
dp[-1][j] = dp[-1][j + 1] + 1
21+
22+
for i in range(len(word1) - 1, -1, -1):
23+
for j in range(len(word2) - 1, -1, -1):
24+
if word1[i] == word2[j]:
25+
dp[i][j] = dp[i + 1][j + 1]
26+
else:
27+
dp[i][j] = 1 + min(dp[i][j + 1], dp[i + 1][j], dp[i + 1][j + 1])
28+
return dp[0][0]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
5+
"""
6+
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of
7+
s1 and s2.
8+
9+
An interleaving of two strings s and t is a configuration where s and t are
10+
divided into n and m substrings respectively, such that:
11+
- s = s1 + s2 + ... + sn
12+
- t = t1 + t2 + ... + tm
13+
- |n - m| <= 1
14+
- The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 +
15+
t3 + s3 + ...
16+
17+
Note: a + b is the concatenation of strings a and b.
18+
"""
19+
if len(s1) + len(s2) != len(s3):
20+
return False
21+
dp = [[False for _ in range(len(s2) + 1)] for _ in range(len(s1) + 1)]
22+
dp[-1][-1] = True
23+
for i in range(len(s1), -1, -1):
24+
for j in range(len(s2), -1, -1):
25+
if i < len(s1) and (s3[i + j] == s1[i]) and dp[i + 1][j]:
26+
dp[i][j] = True
27+
if j < len(s2) and (s3[i + j] == s2[j]) and dp[i][j + 1]:
28+
dp[i][j] = True
29+
return dp[0][0]

0 commit comments

Comments
 (0)