Skip to main content

周赛 Weekly Contest 395

https://leetcode.cn/contest/weekly-contest-395/

3131. Find the Integer Added to Array I

Easy

class Solution:
def addedInteger(self, nums1: List[int], nums2: List[int]) -> int:
if (len(nums1)==0): return 0
sumA1 = sum(nums1)
sumA2 = sum(nums2)
diff = (sumA2 - sumA1) // len(nums1)
return diff

3132. Find the Integer Added to Array II

Medium

class Solution:
def minimumAddedInteger(self, nums1: List[int], nums2: List[int]) -> int:
if (len(nums1)==0): return 0
nums1 = sorted(nums1)
nums2 = sorted(nums2)
res = float('inf')
sum1 = sum(nums1)
sum2 = sum(nums2)
for i in range(len(nums1)-1):
for j in range(i+1, len(nums1)):
curSum1 = sum1-nums1[i]-nums1[j]
curDiff = (sum2-curSum1)//len(nums2)
if curDiff < res:
isTrue = True
hasSkip = 0
for k in range(len(nums1)):
if k == i or k == j:
hasSkip+=1
continue
if nums2[k-hasSkip]-curDiff != nums1[k]:
isTrue = False
break
if isTrue:
res = curDiff

return res

3133. Minimum Array End

Medium

class Solution:
def minEnd(self, n: int, x: int) -> int:
# 0111
# 1111

# 0100 -> 4
# 0101 -> 5, 0111 7
# 0110
# 0111
# 1100
oneBitIdxOfNum = []
tempNum = x
curBitIdx = 0
while tempNum != 0:
if tempNum & 1:
oneBitIdxOfNum.append(curBitIdx)
tempNum >>= 1
curBitIdx+=1
zeroBitIdxOfNum = []
for i in range(0, 64):
if i not in oneBitIdxOfNum:
zeroBitIdxOfNum.append(i)

def setIdxBit(target, num):
tBitIdx = 0
res = num
while target != 0:
curBit = target & 1
target >>= 1
tBitIdx+=1
curNumBit = zeroBitIdxOfNum[tBitIdx-1]
res |= (curBit << (curNumBit))
return res

if (n==0): return 0
return setIdxBit(n-1, x)

3134. Find the Median of the Uniqueness Array

Hard

TODO