Skip to content

Commit aaa2bb1

Browse files
committed
Solution of some problems
1 parent 0498712 commit aaa2bb1

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

1-100q/14.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Write a function to find the longest common prefix string amongst an array of strings.
3+
4+
If there is no common prefix, return an empty string "".
5+
6+
Example 1:
7+
8+
Input: ["flower","flow","flight"]
9+
Output: "fl"
10+
Example 2:
11+
12+
Input: ["dog","racecar","car"]
13+
Output: ""
14+
Explanation: There is no common prefix among the input strings.
15+
Note:
16+
17+
All given inputs are in lowercase letters a-z.
18+
'''
19+
20+
class Solution(object):
21+
def longestCommonPrefix(self, strs):
22+
"""
23+
:type strs: List[str]
24+
:rtype: str
25+
"""
26+
def prefix(strs, index):
27+
check_prefix = strs[0][:index]
28+
for index in range(1, len(strs)):
29+
if not strs[index].startswith(check_prefix):
30+
return False
31+
return True
32+
33+
34+
if not strs:
35+
return ""
36+
37+
minLength = float('inf')
38+
for string in strs:
39+
minLength = min(minLength, len(string))
40+
41+
low, high = 0, minLength
42+
43+
while low <= high:
44+
mid = (low+high)/2
45+
if (prefix(strs, mid)):
46+
low = mid + 1
47+
else:
48+
high = mid - 1
49+
50+
return strs[0][:(low+high)/2]

600-700q/673.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Given an unsorted array of integers, find the number of longest increasing subsequence.
3+
4+
Example 1:
5+
Input: [1,3,5,4,7]
6+
Output: 2
7+
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
8+
9+
Example 2:
10+
Input: [2,2,2,2,2]
11+
Output: 5
12+
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
13+
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
14+
'''
15+
16+
class Solution(object):
17+
def findNumberOfLIS(self, nums):
18+
length = [1]*len(nums)
19+
count = [1]*len(nums)
20+
result = 0
21+
for end, num in enumerate(nums):
22+
for start in range(end):
23+
if num > nums[start]:
24+
if length[start] >= length[end]:
25+
length[end] = 1+length[start]
26+
count[end] = count[start]
27+
elif length[start] + 1 == length[end]:
28+
count[end] += count[start]
29+
for index, max_subs in enumerate(count):
30+
if length[index] == max(length):
31+
result += max_subs
32+
return result

600-700q/674.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
3+
4+
Example 1:
5+
Input: [1,3,5,4,7]
6+
Output: 3
7+
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
8+
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.
9+
10+
Example 2:
11+
Input: [2,2,2,2,2]
12+
Output: 1
13+
14+
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
15+
Note: Length of the array will not exceed 10,000.
16+
'''
17+
18+
class Solution(object):
19+
def findLengthOfLCIS(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: int
23+
"""
24+
if not nums:
25+
return 0
26+
start, result = 0, 1
27+
for end in range(1, len(nums)):
28+
if nums[end-1] >= nums[end]:
29+
start = end
30+
result = max(result, end-start+1)
31+
return result

900-1000q/926.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
A string of '0's and '1's is monotone increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.)
3+
4+
We are given a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'.
5+
6+
Return the minimum number of flips to make S monotone increasing.
7+
8+
9+
10+
Example 1:
11+
12+
Input: "00110"
13+
Output: 1
14+
Explanation: We flip the last digit to get 00111.
15+
Example 2:
16+
17+
Input: "010110"
18+
Output: 2
19+
Explanation: We flip to get 011111, or alternatively 000111.
20+
Example 3:
21+
22+
Input: "00011000"
23+
Output: 2
24+
Explanation: We flip to get 00000000.
25+
26+
27+
Note:
28+
29+
1 <= S.length <= 20000
30+
S only consists of '0' and '1' characters.
31+
'''
32+
33+
class Solution(object):
34+
def minFlipsMonoIncr(self, S):
35+
"""
36+
:type S: str
37+
:rtype: int
38+
"""
39+
ones = [0]
40+
for char in S:
41+
ones.append(ones[-1] + int(char))
42+
# print ones
43+
result = float('inf')
44+
for index in range(len(ones)):
45+
zeroes = len(S) - index - (ones[-1]-ones[index])
46+
result = min(zeroes+ones[index], result)
47+
return result

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
6161
|983|[Minimum Cost For Tickets](https://leetcode.com/problems/minimum-cost-for-tickets/) | [Python](./900-1000q/983.py)|Medium|
6262
|981|[Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [Python](./900-1000q/981.py)|Medium|
6363
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](./900-1000q/977.py)|Easy|
64+
|926|[Flip String to Monotone Increasing](https://leetcode.com/problems/flip-string-to-monotone-increasing)|[Python](./900-1000q/926.py)|Medium|
6465

66+
##### [Problems 600-700](./600-700q/)
67+
| # | Title | Solution | Difficulty |
68+
|---| ----- | -------- | ---------- |
69+
|673|[Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence)|[Python](./600-700q/673.py)|Medium|
70+
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence)|[Python](./600-700/674.py)|Easy|
6571

6672
##### [Problems 400-500](./400-500q/)
6773
| # | Title | Solution | Difficulty |
@@ -231,6 +237,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
231237
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [Python](./1-100q/17.py)|Medium|
232238
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [Python](./1-100q/16.py)|Medium|
233239
|15|[3Sum](https://leetcode.com/problems/3sum/)| [Python](./1-100q/15.py)|Medium|
240+
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix)|[Python](./1-100q/14.py)|Easy|
234241
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [Python](./1-100q/11.py)|Medium|
235242
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| [Python](./1-100q/10.py)|Hard|
236243
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)| [Python](./1-100q/06.py)|Easy|

0 commit comments

Comments
 (0)