題目一:
解題思路:
判斷每一位加上額外糖果數,是否大於等於原始數組中的最大值
代碼實現:
class Solution:
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
return [a + extraCandies >= max(candies) for a in candies]
題目二:
解題思路:
最大值:找到第一位不是9的修改成9
最小值:1.、如果第一位大於1,則修改成1 2、如果第一位爲1,則向後找到第一個不爲1的數字,如果和首數字相同,則修改爲1;如果和首數字不相同,修改爲0
代碼實現:
class Solution:
def maxDiff(self, num: int) -> int:
str_num = str(num)
flag1 = False
flag2 = False
new_num1 = num
new_num2 = num
for idx, a in enumerate(str_num):
if new_num1 != num and new_num2 != num:
break
b = int(a)
if b < 9 and new_num1 == num:
new_num1 = int(str_num.replace(a, '9'))
if new_num2 != num:
continue
elif b == 1:
if idx == 0:
continue
elif str_num[0] == '1':
continue
else:
new_num2 = int(str_num.replace(a, '0'))
elif b > 1:
if idx == 0:
new_num2 = int(str_num.replace(a, '1'))
elif idx > 0:
if str_num[idx] == str_num[0]:
new_num2 = int(str_num.replace(a, '1'))
else:
new_num2 = int(str_num.replace(a, '0'))
return new_num1 - new_num2
題目三:
解題思路:
根據字典序對兩個數字進行排序,然後按位遍歷是否符合第一個數組全大於等於第二個數組,或者全小於
代碼實現:
class Solution:
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
list1 = list(s1)
list2 = list(s2)
list1.sort()
list2.sort()
compare_type = 0
for idx in range(len(list1)):
if list1[idx] == list2[idx]:
continue
elif list1[idx] > list2[idx]:
compare_type = 1
else:
compare_type = 2
break
if 0 == compare_type:
return True
for idx in range(len(list1)):
if compare_type == 1 and list1[idx] < list2[idx]:
return False
elif compare_type == 2 and list1[idx] > list2[idx]:
return False
return True
題目四:
解題思路:
方法一:回溯 + 剪枝
遍歷所有的可能性,然後找到一種情況,就對結果加1(果然超時了
方法二:DP
狀態壓縮DP
代碼實現:
方法一:
class Solution:
def numberWays(self, hats: List[List[int]]) -> int:
ans = 0
max_num = 1000000007
def sort_help(start, end, hats, rec):
nonlocal ans
print(start, end, rec)
if start >= end:
ans += 1
ans %= max_num
return
curr_hats = hats[start]
for hat in curr_hats:
if hat in rec:
continue
rec.add(hat)
sort_help(start + 1, end, hats, rec)
rec.remove(hat)
rec = set()
sort_help(0, len(hats), hats, rec)
return ans
方法二:
import collections
class Solution(object):
def numberWays(self, hats):
"""
:type hats: List[List[int]]
:rtype: int
"""
n = len(hats)
self.caps = collections.defaultdict(list) #記錄每個帽子都可以被誰帶
for i in range(n):
for j in hats[i]:
self.caps[j].append(i)
n = len(hats)
target = (1 << n) -1 # target就是所有人都會有帽子
dp = [[-1 for j in range(40 + 1)] for i in range(2 ** n)] # dp[i][j] 告訴你每個bitset 用到了第幾號帽子
return self.dp(dp, 0, 1, target) % (10**9+7) #咱們先給第一個人第一個帽子試試看
def dp(self,dp, mask, cap_no, target):
if mask == target: #所有人都有帽子了
return 1
if cap_no > 40: #帽子超了
return 0
if dp[mask][cap_no]!= -1 : #說明這個狀態以記錄過了
return dp[mask][cap_no]
ways = self.dp(dp, mask, cap_no + 1, target) 好, 咱們dfs看看讓第一個人用下一個帽子, 頭一個帽子誰也別帶
if cap_no in self.caps:
for ppl in self.caps[cap_no]:
if mask & (1 << ppl) : continue
ways += self.dp(dp, mask | (1 << ppl), cap_no + 1, target) bitmask常規操作
ways = ways % (10**9 + 7)
dp[mask][cap_no] = ways
return dp[mask][cap_no]
作者:ycui11
鏈接:https://leetcode-cn.com/problems/number-of-ways-to-wear-different-hats-to-each-other/solution/dp-bitmask-ji-lu-ren-er-bu-shi-mao-zi-by-ycui11/
來源:力扣(LeetCode)
著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。