avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

6347Number of vowel string within the scope of statistics

  2024-01-01        
字数统计: 414字   |   阅读时长: 2min

topic

2023-02-05 (2).png

Thought:

Pay attention to pre -processing and prefixing
Prefix and an algorithm,Used to find the prefix and the array。The prefix and definition of the array are defined as the harmony of the prefix element in a certain position of the array。in other words,The prefix and the elements of each position of an array are added to the previous element。

This code uses prefix and to optimize the query process。It pre -processed whether the first letter of each word is a meta sound,Then save these results in a list。Then,It uses prefix and calculate each query result。

Specifically,For each query (i, j),It will be statistics from words[i] arrive words[j] Conditional conditions(The first letter is a Yuan Yin)Number of words。This can be used by using counts[j+1] - counts[i] To be done。This is because counts[j+1] Contain words[0] arrive words[j] Conditional conditionsNumber of words,and counts[i] Contain words[0] arrive words[i-1] Conditional conditionsNumber of words。therefore,counts[j+1] - counts[i] That words[i] arrive words[j] Conditional conditionsNumber of words。

In the code,counts Arrays use prefix and pre -processing,therefore可以在 O(1) Calculate the results of each query within time。This makes the total running time of the code from coming from O(n^2) Fall to O(n)

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
yuanyin = {'a', 'e', 'i', 'o', 'u'}
ans = []
for i, j in queries:
count = 0
for x in range(i, j + 1):
if words[x][0] in yuanyin and words[x][-1] in yuanyin:
count += 1
ans.append(count)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
yuanyin = set('aeiou')
# Traversing each word,Determine whether its first letter is a metaphysical sound
# Save the results in words_first_last List
words_first_last = [(word[0] in yuanyin, word[-1] in yuanyin) for word in words]
# Create counts List,Used to store prefixes and
counts = [0] * (len(words) + 1)
# Traversing each word,Calculate prefix and
for i in range(len(words)):
counts[i + 1] = counts[i] + int(words_first_last[i][0] and words_first_last[i][1])
# Traversing each query,Calculate the result of the query
ans = [counts[j + 1] - counts[i] for i, j in queries]
# Return result
return ans
  • Python
  • answer

扫一扫,分享到微信

微信分享二维码
6331The most prizes won in the two lines make()usage
6351. The scores of the array after the marking all elements
目录
  1. 1. topic
  2. 2. Thought:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号