Skip to content

Commit 27d7872

Browse files
committed
add more problems
1 parent 05f7aac commit 27d7872

File tree

6 files changed

+359
-0
lines changed

6 files changed

+359
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Regular Expression Matching
2+
#
3+
# [Hard] [AC:27.0% 463.7K of 1.7M] [filetype:python3]
4+
#
5+
# Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.
6+
#
7+
# '.' Matches any single character.
8+
#
9+
# '*' Matches zero or more of the preceding element.
10+
#
11+
# The matching should cover the entire input string (not partial).
12+
#
13+
# Note:
14+
#
15+
# s could be empty and contains only lowercase letters a-z.
16+
#
17+
# p could be empty and contains only lowercase letters a-z, and characters like . or *.
18+
#
19+
# Example 1:
20+
#
21+
# Input:
22+
#
23+
# s = "aa"
24+
#
25+
# p = "a"
26+
#
27+
# Output: false
28+
#
29+
# Explanation: "a" does not match the entire string "aa".
30+
#
31+
# Example 2:
32+
#
33+
# Input:
34+
#
35+
# s = "aa"
36+
#
37+
# p = "a*"
38+
#
39+
# Output: true
40+
#
41+
# Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
42+
#
43+
# Example 3:
44+
#
45+
# Input:
46+
#
47+
# s = "ab"
48+
#
49+
# p = ".*"
50+
#
51+
# Output: true
52+
#
53+
# Explanation: ".*" means "zero or more (*) of any character (.)".
54+
#
55+
# Example 4:
56+
#
57+
# Input:
58+
#
59+
# s = "aab"
60+
#
61+
# p = "c*a*b"
62+
#
63+
# Output: true
64+
#
65+
# Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".
66+
#
67+
# Example 5:
68+
#
69+
# Input:
70+
#
71+
# s = "mississippi"
72+
#
73+
# p = "mis*is*p*."
74+
#
75+
# Output: false
76+
#
77+
# [End of Description]:
78+
class Solution:
79+
def isMatch(self, s: str, p: str) -> bool:
80+
memo = dict()
81+
82+
def dp(i, j):
83+
if (i, j) in memo:
84+
return memo[(i, j)]
85+
if j == len(p):
86+
return i == len(s)
87+
first = i < len(s) and p[j] in {s[i], "."}
88+
89+
if j <= len(p) - 2 and p[j + 1] == "*":
90+
ans = dp(i, j + 2) or first and dp(i + 1, j)
91+
else:
92+
ans = first and dp(i + 1, j + 1)
93+
memo[(i, j)] = ans
94+
return ans
95+
96+
return dp(0, 0)

easy/312.burst_balloons.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Burst Balloons
2+
#
3+
# [Hard] [AC:52.2% 103.9K of 198.9K] [filetype:python3]
4+
#
5+
# Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You
6+
# are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins.
7+
# Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
8+
#
9+
# Find the maximum coins you can collect by bursting the balloons wisely.
10+
#
11+
# Note:
12+
#
13+
# You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
14+
#
15+
# 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
16+
#
17+
# Example:
18+
#
19+
# Input: [3,1,5,8]
20+
#
21+
# Output: 167
22+
#
23+
# Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
24+
#
25+
# coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
26+
#
27+
# [End of Description]:
28+
class Solution:
29+
def maxCoins(self, nums: List[int]) -> int:
30+
# we create array first
31+
sz = len(nums)
32+
# add two virtual balloons
33+
points = [None] * (sz + 2)
34+
points[0] = points[sz + 1] = 1
35+
for i in range(1, sz + 1):
36+
points[i] = nums[i - 1]
37+
38+
# initialize dp array
39+
dp = []
40+
for _ in range(sz + 2):
41+
inner = []
42+
for _ in range(sz + 2):
43+
inner.append(0)
44+
dp.append(inner)
45+
# according to dp table
46+
for i in range(sz, -1, -1):
47+
for j in range(i + 1, sz + 2):
48+
for k in range(i + 1, j):
49+
dp[i][j] = max(
50+
dp[i][j],
51+
dp[i][k] + dp[k][j] + points[i] * points[j] * points[k],
52+
)
53+
return dp[0][sz + 1]

easy/319.bulb_switcher.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Bulb Switcher
2+
#
3+
# [Medium] [AC:45.3% 82.8K of 182.8K] [filetype:python3]
4+
#
5+
# There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On
6+
# the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the i-th round,
7+
# you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.
8+
#
9+
# Example:
10+
#
11+
# Input: 3
12+
#
13+
# Output: 1
14+
#
15+
# Explanation:
16+
#
17+
# At first, the three bulbs are [off, off, off].
18+
#
19+
# After first round, the three bulbs are [on, on, on].
20+
#
21+
# After second round, the three bulbs are [on, off, on].
22+
#
23+
# After third round, the three bulbs are [on, off, off].
24+
#
25+
# So you should return 1, because there is only one bulb is on.
26+
#
27+
# [End of Description]:
28+
import math
29+
30+
31+
class Solution:
32+
def bulbSwitch(self, n: int) -> int:
33+
return int(math.sqrt(n))

easy/651.4_keys_keyboard.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 4 Keys Keyboard
2+
#
3+
# [Medium] [AC:52.7% 16.7K of 31.7K] [filetype:python3]
4+
#
5+
# Imagine you have a special keyboard with the following keys:
6+
#
7+
# Key 1: (A): Print one 'A' on screen.
8+
#
9+
# Key 2: (Ctrl-A): Select the whole screen.
10+
#
11+
# Key 3: (Ctrl-C): Copy selection to buffer.
12+
#
13+
# Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.
14+
#
15+
# Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of 'A' you
16+
# can print on screen.
17+
#
18+
# Example 1:
19+
#
20+
# Input: N = 3
21+
#
22+
# Output: 3
23+
#
24+
# Explanation:
25+
#
26+
# We can at most get 3 A's on screen by pressing following key sequence:
27+
#
28+
# A, A, A
29+
#
30+
# Example 2:
31+
#
32+
# Input: N = 7
33+
#
34+
# Output: 9
35+
#
36+
# Explanation:
37+
#
38+
# We can at most get 9 A's on screen by pressing following key sequence:
39+
#
40+
# A, A, A, Ctrl A, Ctrl C, Ctrl V, Ctrl V
41+
#
42+
# Note:
43+
#
44+
# 1 <= N <= 50
45+
#
46+
# Answers will be in the range of 32-bit signed integer.
47+
#
48+
# [End of Description]:
49+
class Solution:
50+
def maxA(self, N: int) -> int:
51+
dp = [None] * (N + 1)
52+
dp[0] = 0
53+
for i in range(1, N + 1):
54+
# press A
55+
dp[i] = dp[i - 1] + 1
56+
for j in range(2, i):
57+
# C-A & C-C dp[j-2], C-V (i - j) times
58+
# total: dp[j - 2] * (i - j + 1) A
59+
dp[i] = max(dp[i], dp[j - 2] * (i - j + 1))
60+
# After N times
61+
return dp[N]

easy/72.edit_distance.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Edit Distance
2+
#
3+
# [Hard] [AC:45.4% 302.7K of 666.6K] [filetype:python3]
4+
#
5+
# Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
6+
#
7+
# You have the following 3 operations permitted on a word:
8+
#
9+
# Insert a character
10+
#
11+
# Delete a character
12+
#
13+
# Replace a character
14+
#
15+
# Example 1:
16+
#
17+
# Input: word1 = "horse", word2 = "ros"
18+
#
19+
# Output: 3
20+
#
21+
# Explanation:
22+
#
23+
# horse -> rorse (replace 'h' with 'r')
24+
#
25+
# rorse -> rose (remove 'r')
26+
#
27+
# rose -> ros (remove 'e')
28+
#
29+
# Example 2:
30+
#
31+
# Input: word1 = "intention", word2 = "execution"
32+
#
33+
# Output: 5
34+
#
35+
# Explanation:
36+
#
37+
# intention -> inention (remove 't')
38+
#
39+
# inention -> enention (replace 'i' with 'e')
40+
#
41+
# enention -> exention (replace 'n' with 'x')
42+
#
43+
# exention -> exection (replace 'n' with 'c')
44+
#
45+
# exection -> execution (insert 'u')
46+
#
47+
# [End of Description]:
48+
class Solution:
49+
def minDistance(self, word1: str, word2: str) -> int:
50+
memo = dict()
51+
52+
def dp(i, j):
53+
# base case
54+
if i == -1:
55+
return j + 1
56+
if j == -1:
57+
return i + 1
58+
59+
if (i, j) in memo:
60+
return memo[(i, j)]
61+
if word1[i] == word2[j]:
62+
memo[(i, j)] = dp(i - 1, j - 1)
63+
else:
64+
memo[(i, j)] = min(
65+
dp(i - 1, j) + 1, dp(i - 1, j - 1) + 1, dp(i, j - 1) + 1
66+
)
67+
return memo[(i, j)]
68+
69+
return dp(len(word1) - 1, len(word2) - 1)

easy/877.stone_game.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Stone Game
2+
#
3+
# [Medium] [AC:65.7% 66.4K of 101.1K] [filetype:python3]
4+
#
5+
# Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has
6+
# a positive integer number of stones piles[i].
7+
#
8+
# The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
9+
#
10+
# Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either
11+
# the beginning or the end of the row. This continues until there are no more piles left, at which point the person
12+
# with the most stones wins.
13+
#
14+
# Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
15+
#
16+
# Example 1:
17+
#
18+
# Input: piles = [5,3,4,5]
19+
#
20+
# Output: true
21+
#
22+
# Explanation:
23+
#
24+
# Alex starts first, and can only take the first 5 or the last 5.
25+
#
26+
# Say he takes the first 5, so that the row becomes [3, 4, 5].
27+
#
28+
# If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
29+
#
30+
# If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
31+
#
32+
# This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
33+
#
34+
# Constraints:
35+
#
36+
# 2 <= piles.length <= 500
37+
#
38+
# piles.length is even.
39+
#
40+
# 1 <= piles[i] <= 500
41+
#
42+
# sum(piles) is odd.
43+
#
44+
# [End of Description]:
45+
class Solution:
46+
def stoneGame(self, piles: List[int]) -> bool:
47+
return True

0 commit comments

Comments
 (0)