Skip to main content

周赛 Weekly Contest 408

https://leetcode.cn/contest/weekly-contest-408/ranking/9/

Q1. Find if Digit Game Can Be Won

class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
s = sum(nums)
singleS = 0
for n in nums:
if n < 10:
singleS += n
doubleS = s - singleS
if doubleS > singleS or singleS > doubleS:
return True
return False

Q2. Find the Count of Numbers Which Are Not Special

class Solution:
def nonSpecialCount(self, l: int, r: int) -> int:
res = r-l+1
left = int(math.sqrt(l))
right = int(math.sqrt(r)) + 1
def is_prime(n):
"""判断一个数是否为质数"""
if n <= 1:
return False
cnt = 1
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
cnt += 1
if cnt >= 2:
return False
return True
for i in range(left, right+1):
if i**2 < l:
continue
elif i**2 > r:
break
else:
# print(i, is_prime(i))
if is_prime(i):
res -= 1
return res

Q3. Count the Number of Substrings With Dominant Ones

class Solution:
def numberOfSubstrings(self, s: str) -> int:
res = 0
for i in range(len(s)):
cntZ = 0
for j in range(i, len(s), 1):
l = j - i + 1
if s[j] == "0":
cntZ += 1
if cntZ**2 <= l - cntZ:
res += 1
if cntZ**2 > len(s) - j + 1 + l - cntZ:
break
return res

Q4. Check if the Rectangle Corner Is Reachable

todo