classSolution(object): defminimumCardPickup(self, cards): """ :type cards: List[int] :rtype: int """ from collections import defaultdict tmp_dict = defaultdict(list) for i inrange(len(cards)): tmp_dict[cards[i]].append(i)
# print(tmp_dict) res = 1e9 for value in tmp_dict.values(): iflen(value) < 2: continue for j inrange(len(value)-1): res = min(res, value[j+1]-value[j])
classSolution(object): defcountDistinct(self, nums, k, p): """ :type nums: List[int] :type k: int :type p: int :rtype: int """ # preprocess cnt_list = [] for i inrange(len(nums)): if nums[i] % p == 0: cnt_list.append(1) else: cnt_list.append(0) # print("cnt_list: ", cnt_list)
res = 0 # from collections import defaultdict hash_set = {} for i inrange(len(nums)): for j inrange(i, len(nums)): tmp_list = nums[i:j+1] if hash_set.get(tuple(tmp_list)) isnotNone: continue hash_set[tuple(tmp_list)] = 1 tmp = sum(cnt_list[i:j+1]) if tmp <= k: res += 1
classSolution(object): defappealSum(self, s): """ :type s: str :rtype: int """ # 一种每个字符贡献度的思想,还是很有特点的 res = 0 char_dict = {} for i inrange(len(s)): if char_dict.get(s[i]) isNone: res += (i + 1) * (len(s)-i) else: res += (i - char_dict[s[i]]) * (len(s) - i) char_dict[s[i]] = i return res
classSolution(object): defappealSum(self, s): """ :type s: str :rtype: int """ res = 0 d1 = {} for i inrange(len(s)): d1[i] = {s[i]: 1} res += 1 print("init: ", d1)
res_d = {}
# 这里感觉是一个base基础上的dp,每次要取几位,有点不像平常的遍历思路1 # 现在要开始每次取两位 for l inrange(2, len(s)+1): for i inrange(len(s)-l+1): # base base = s[i:i+l-1] # print("base: ", base)