Skip to main content

周赛 Weekly Contest 407

100372. Number of Bit Changes to Make Two Integers Equal

class Solution:
def minChanges(self, n: int, k: int) -> int:
cnt = 0
while n > 0 and k > 0:
if k % 2 == 1:
if n % 2 != 1:
return - 1
else:
if n % 2 == 1:
cnt += 1
k >>= 1
n >>= 1
if k != 0:
return - 1
while n > 0:
if n % 2 == 1:
cnt += 1
n >>= 1
return cnt

100335. Vowels Game in a String

  • A fails only when the Vowels count is 0
  • if Vowels count is odd
    • A just need to remove whole string
  • if Vowels count is even
    • A can remove the string with odd number of Vowels and left only one Vowels
      • If there is only one char left, B lose
      • Else, B remove a non-empty string, and A loses
class Solution:
def doesAliceWin(self, s: str) -> bool:
vd = {
"a": True,
"e": True,
"i": True,
"o": True,
"u": True,
}
cnt = 0
for c in s:
if c in vd:
cnt += 1
# print(cnt)
if cnt == 0:
return False
else:
return True

100360. Maximum Number of Operations to Move Ones to the End

  • Count the 1 number from left to right
  • For each "0" gap, add the previous count of 1
class Solution:
def maxOperations(self, s: str) -> int:
res = 0
iCnt = 0
i = 0
while i < len(s):
if s[i] == "1":
iCnt += 1
i += 1
else:
while i < len(s) and s[i] == "0":
i += 1
res += iCnt
return res

100329. Minimum Operations to Make Array Equal to Target

  • Iterate one by one and summarize the count
class Solution:
def minimumOperations(self, nums: List[int], target: List[int]) -> int:
for i in range(len(nums)):
nums[i] = target[i] - nums[i]
# print(nums)
i = 1
flag = nums[0] >= 0
cnt = abs(nums[0])
res = cnt
while i < len(nums):
print(i, cnt, res, abs(nums[i]), flag, nums[i] > 0)
if (nums[i] > 0) == flag:
if cnt <= abs(nums[i]):
# print("here1")
res += abs(nums[i]) - cnt
cnt = abs(nums[i])
else:
cnt = abs(nums[i])
else:
# print("here")
cnt = abs(nums[i])
flag = nums[i] > 0
res += cnt
i += 1
return res