Split a String Into the Max Number of Unique Substrings
1593. Split a String Into the Max Number of Unique Substrings
Given a string s, return the maximum number of unique substrings that the given string can be split into.
You can split string s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such th...
Rearrange Spaces Between Words
1592. Rearrange Spaces Between Words
You are given a string text of words that are placed among some number of spaces. Each word consists of one or more lowercase English letters and are separated by at least one space. It’s guaranteed that text contains at least one word.
Rearrange the spaces so that there is an equal number of spaces between...
Maximum Non Negative Product in a Matrix
1594. Maximum Non Negative Product in a Matrix
You are given a rows x cols matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix.
Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (rows - 1, cols - 1), find the...
Sum of All Odd Length Subarrays
1588. Sum of All Odd Length Subarrays
Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays.
A subarray is a contiguous subsequence of the array.
Return the sum of all odd-length subarrays of arr.
Solution 1 - Brute Force
def sumOddLengthSubarrays(self, arr):
"""
:type arr: List[int]
:rtyp...
Maximum Sum Obtained of Any Permutation
1589. Maximum Sum Obtained of Any Permutation
We have an array of integers, nums, and an array of requests where requests[i] = [starti, endi]. The ith request asks for the sum of nums[starti] + nums[starti + 1] + … + nums[endi - 1] + nums[endi]. Both starti and endi are 0-indexed.
Return the maximum total sum of all requests among all permutati...
Make Sum Divisible by P
1590. Make Sum Divisible by P
Given an array of positive integers nums, remove the smallest subarray (possibly empty) such that the sum of the remaining elements is divisible by p. It is not allowed to remove the whole array.
Return the length of the smallest subarray that you need to remove, or -1 if it’s impossible.
A subarray is defined as ...
Regular Expression Matching
10. Regular Expression Matching
Solution
Reference
dp[i][j] represents if p[j-1] and s[i-1] can match each other.
When s is empty, if all previous even indexs of p are *, they can match. Otherwise, cannot.
When p[j-1] == '*'
if pre_p == curr_s:
a* counts as multiple a: dp[i][j] = dp[i-1][j]
a* counts a...
Median of Two Sorted Arrays
4. Median of Two Sorted Arrays
Solution - O(m+n)
Use two pointers to track until visited mid numbers.
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
i, j = 0, 0
l1, l2 = len(nums1), len(nums2)
mid = (l1 + l2 + 1) // 2
is_odd = (l1 + l2) ...
199 post articles, 25 pages.