【268周赛】贪心_模拟_哈希_二分_枚举

2078.两栋颜色不同且距离最远的房子

我的方法

  1. 递归 超时
class Solution(object):
    def maxDistance(self, colors):
        left = 0
        right = len(colors) - 1
        if left >= right:
            return 0
        if colors[left] == colors[right]:
            return max(self.maxDistance(colors[left + 1:right+1]), self.maxDistance(colors[left:right]))
        else:
            return right - left
  1. 参考题目二进制子串(垃圾解法)
class Solution(object):
    def maxDistance(self, colors):
        cur = colors[0]
        lis = []
        color = []
        num = 0
        for item in colors:
            if cur == item:
                num = num + 1
            else:
                color.append(cur)
                cur = item
                lis.append(num)
                num = 1
        lis.append(num)
        color.append(item)
        #return lis, color
        theMax = 0
        for i in range(len(lis) - 1):
            for j in range(i, len(lis)):
                if color[i] != color[j]: 
                    theMax = max(theMax, sum(lis[i:j+1])- 1)
        return theMax

常见解法

注意看提示,数据量很少,完全可以用暴力

  1. 暴力
lass Solution(object):
    def maxDistance(self, colors):
        #暴力
        theMax = 0
        for i in range(len(colors)):
            for j in range(len(colors)):
                if colors[i] != colors[j]:
                    theMax = max(theMax, abs(j - i))
        return theMax

真是:不刷题都能想到的方法,刷了题反而忘记了最简单的解法

  1. 贪心
class Solution(object):
    def maxDistance(self, colors):
        # Greedy Algorithm
        ans = 0
        # left
        num = len(colors)
        i = num - 1
        while colors[i] == colors[0]:
            i -= 1
        ans = max(ans, i)
        # right
        i = 0
        while colors[i] == colors[num - 1]:
            i += 1
        ans = max(ans, num - i - 1)
        return ans

时间复杂度: O ( n ) O(n) O(n)

空间复杂度: O ( 1 ) O(1) O(1)


2079.给植物浇水

class Solution(object):
    def wateringPlants(self, plants, capacity):
        cur = capacity
        ans = 0
        for i in range(len(plants)):
            if plants[i] <= cur:
                ans += 1
                cur = cur - plants[i]
            else:
                ans = ans + i + i + 1
                cur = capacity
                cur = cur - plants[i]
        return ans

时间复杂度: O ( n ) O(n) O(n)

空间复杂度: O ( 1 ) O(1) O(1)


2080.区间内查询数字的频率

在初始化的时候,就将下标记录进默认字典

class RangeFreqQuery(object):
    def __init__(self, arr):
        self.d = defaultdict(list)
        for i in range(len(arr)):
            self.d[arr[i]].append(i)
    def query(self, left, right, value):
        temp = self.d[value]
        r = bisect_right(temp, right)
        l = bisect_left(temp, left)
        return r - l

时间复杂度: O ( n + q log ⁡ n ) O(n+q\log n) O(n+qlogn)

空间复杂度: O ( n ) O(n) O(n)


2081.k镜像数字的和

方法一:打表表

根据方法二制作表,求和改成数组追加即可

ans = []
for k in range(2, 10):
    ans.append(kMirror(k, 30))
print(ans)
class Solution(object):
    
    def __init__(self):
        self.ans = [[1, 3, 5, 7, 9, 33, 99, 313, 585, 717, 7447, 9009,15351, 32223, 39993, 53235, 53835, 73737, 585585, 1758571, 1934391, 1979791, 3129213, 5071705, 5259525, 5841485, 13500531, 719848917, 910373019, 939474939], 
        			[1, 2, 4, 8, 121, 151, 212, 242, 484, 656, 757, 29092, 48884, 74647, 75457, 76267, 92929, 93739, 848848, 1521251, 2985892, 4022204, 4219124, 4251524, 4287824, 5737375, 7875787, 7949497, 27711772, 83155138], 
        			[1, 2, 3, 5, 55, 373, 393, 666, 787, 939, 7997, 53235, 55255, 55655, 57675, 506605, 1801081, 2215122, 3826283, 3866683, 5051505, 5226225, 5259525, 5297925, 5614165, 5679765, 53822835, 623010326, 954656459, 51717171715], 
        			[1, 2, 3, 4, 6, 88, 252, 282, 626, 676, 1221, 15751, 18881, 10088001, 10400401, 27711772, 30322303, 47633674, 65977956, 808656808, 831333138, 831868138, 836131638, 836181638, 2512882152, 2596886952, 2893553982, 6761551676, 12114741121, 12185058121], 
        			[1, 2, 3, 4, 5, 7, 55, 111, 141, 191, 343, 434, 777, 868, 1441, 7667, 7777, 22022, 39893, 74647, 168861, 808808, 909909, 1867681, 3097903, 4232324, 4265624, 4298924, 4516154, 4565654], 
        			[1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596, 9470749, 61255216, 230474032, 466828664, 485494584, 638828836, 657494756, 858474858, 25699499652, 40130703104, 45862226854, 61454945416, 64454545446], 
        			[1, 2, 3, 4, 5, 6, 7, 9, 121, 292, 333, 373, 414, 585, 3663, 8778, 13131, 13331, 26462, 26662, 30103, 30303, 207702, 628826, 660066, 1496941, 1935391, 1970791, 4198914, 55366355], 
        			[1, 2, 3, 4, 5, 6, 7, 8, 191, 282, 373, 464, 555, 646, 656, 6886, 25752, 27472, 42324, 50605, 626626, 1540451, 1713171, 1721271, 1828281, 1877781, 1885881, 2401042, 2434342, 2442442]]
        
    def kMirror(self, k, n):
        return sum(self.ans[k - 2][: n])

时间复杂度: O ( 1 ) O(1) O(1)

空间复杂度: O ( 1 ) O(1) O(1)

方法二:折半搜索

  • 分奇偶性产生十进制的回文数,再判断k进制下是否也是回文数
class Solution(object):
    def kMirror(self, k, n):
        def isPalindrome(n):
            l = list()
            while n:
                l.append(n % k)
                n = n // k
            return l == l[:: -1]  # 判断是否是回文数的常用手法
        left, right = 1, 0
        ans, count = 0, 0  # cnt是count的缩写,表示计数器
        while count < n:
            right = left * 10
            for op in range(2):
                for i in range(left, right):
                    if count == n:
                        break
                    combined = i
                    if op == 1:
                        x = i
                    else:
                        x = i // 10
                    while x:
                        combined = combined * 10 + x % 10
                        x = x // 10
                    if isPalindrome(combined):
                        count += 1
                        ans += combined
            left = right
        return ans

时间复杂度:最大是 O ( 1 0 5 ) O(10^5) O(105)
空间复杂度: O ( 10 ) O(10) O(10)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值