「Python」LeetCode周赛406 - 三题题解

T1:100352. 交换后字典序最小的字符串

思路

暴力模拟,寻找到一个字典序最小的数

代码

class Solution:
    def getSmallestString(self, s: str) -> str:

        n = len(s)
        _min = int(s)
        ans = s
        for i in range(n-1):
            if (int(s[i]) + int(s[i+1]))%2 == 0:
                tmp = s[:i] + s[i+1] + s[i]
                if i+2 < n:
                    tmp += s[i+2:]
                if int(tmp) < _min:
                    _min = int(tmp)
                    ans = tmp
        return ans

T2:100368. 从链表中移除在数组中存在的节点

思路

因为1 <= nums[i] <= 10^5,数据量不算大,所以可以考虑桶排序,用空间换一次遍历。
接下来遍历链表,原地删除即可

代码

from typing import List, Optional

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:
        m = max(nums)
        target = [0 for num in range(m+1)]
        for num in nums:
            target[num] = 1
        cur_node = head
        pre = head
        while cur_node:
            if cur_node.val<=m and target[cur_node.val] == 1:
                if cur_node == head:
                    head = head.next
                    pre = head
                    cur_node = head
                    continue
                else:
                    pre.next = cur_node.next
                    cur_node = cur_node.next
            else:
                if cur_node != head:
                    pre = cur_node
                cur_node = cur_node.next
        return head

T3:100361. 切蛋糕的最小总开销 I

思路

切完一刀变两个,可以用递归模拟切蛋糕(分治?)

递归出口:当m=1或n=1时,也就是横排一行或纵向一列的时候,返回sum(v)或sum(h)。

例如:[2 3 2 3 1][1 2],则有

ans = fun([2 3 2 3 1],[1 2])

找到最大值3及其索引

ans = 3 + fun([2],[1 2]) + fun([2 3 1],[1 2])

fun([2],[1 2]) = 2 + fun([],[1 2]) + fun([],[1 2]) 
                = 2 + sum([1,2]) + sum[1,2] 
                = 8

fun([2 3 1],[1 2]) = 3 + fun([2],[1 2]) + fun([1],[1 2]) 
                    = 3 + 8 + ( 2 + fun([1],[1]) + fun([1],[]) )
                    = 3 + 8 + (2 + 3 + 1) = 17

Ans = 3 + 8 + 17 = 28

代码

class Solution:
    def minimumCost(self, m: int, n: int, horizontalCut: List[int], verticalCut: List[int]) -> int:
        if m == 1:
            return sum(verticalCut)
        elif n == 1:
            return sum(horizontalCut)
        max_cost_1 = -1
        idx = -1
        for i,h in enumerate(horizontalCut):
            if h > max_cost_1:
                idx = i
                max_cost_1 = h
        max_cost_2 = -1
        idx2 = -1
        for i,v in enumerate(verticalCut):
            if v > max_cost_2:
                idx2 = i
                max_cost_2 = v
        if max_cost_1 >= max_cost_2:
            if idx+1 > len(horizontalCut)-1:
                return max_cost_1 + self.minimumCost(idx+1,n,horizontalCut[:idx],verticalCut)+ self.minimumCost(1,n,[],verticalCut)
            return max_cost_1 + self.minimumCost(idx+1,n,horizontalCut[:idx],verticalCut)+ self.minimumCost(m-idx-1,n,horizontalCut[idx+1:],verticalCut)
        else:
            if idx2+1 > len(verticalCut)-1:
                return max_cost_2 + self.minimumCost(m,idx2+1,horizontalCut,verticalCut[:idx2]) + self.minimumCost(m,1,horizontalCut,[])
            return max_cost_2 + self.minimumCost(m,idx2+1,horizontalCut,verticalCut[:idx2]) + self.minimumCost(m,n-idx2-1,horizontalCut,verticalCut[idx2+1:])

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值