leetcode-周赛295

1.重排字符形成目标字符串(6078)

给你两个下标从 0 开始的字符串 s 和 target 。你可以从 s 取出一些字符并将其重排,得到若干新的字符串。
从 s 中取出字符并重新排列,返回可以形成 target 的 最大 副本数。

示例 1:

输入:s = "ilovecodingonleetcode", target = "code"
输出:2
解释:
对于 "code" 的第 1 个副本,选取下标为 4567 的字符。
对于 "code" 的第 2 个副本,选取下标为 17181920 的字符。
形成的字符串分别是 "ecod""code" ,都可以重排为 "code" 。
可以形成最多 2"code" 的副本,所以返回 2 。
示例 2:

输入:s = "abcba", target = "abc"
输出:1
解释:
选取下标为 012 的字符,可以形成 "abc"1 个副本。 
可以形成最多 1"abc" 的副本,所以返回 1 。
注意,尽管下标 34 分别有额外的 'a''b' ,但不能重用下标 2 处的 'c' ,所以无法形成 "abc" 的第 2 个副本。
示例 3:

输入:s = "abbaccaddaeea", target = "aaaaa"
输出:1
解释:
选取下标为 036912 的字符,可以形成 "aaaaa"1 个副本。
可以形成最多 1"aaaaa" 的副本,所以返回 1 。
 

提示:

1 <= s.length <= 100
1 <= target.length <= 10
s 和 target 由小写英文字母组成

解法:

class Solution:
    def rearrangeCharacters(self, s: str, target: str) -> int:
        res = []    
        for i, v in enumerate(target):         
            m = target.count(v)
            n = s.count(v)
            res.append(n // m)
        return min(res)

2.价格减免(6079)

句子 是由若干个单词组成的字符串,单词之间用单个空格分隔,其中每个单词可以包含数字、小写字母、和美元符号 ‘$’ 。如果单词的形式为美元符号后跟着一个非负实数,那么这个单词就表示一个价格。
例如 “$100”、“$23” 和 “ 6.75 " 表 示 价 格 , 而 " 100 " 、 " 6.75" 表示价格,而 "100"、" 6.75""100""” 和 “2$3” 不是。
注意:本题输入中的价格均为整数。
给你一个字符串 sentence 和一个整数 discount 。对于每个表示价格的单词,都在价格的基础上减免 discount% ,并 更新 该单词到句子中。所有更新后的价格应该表示为一个 恰好保留小数点后两位 的数字。
返回表示修改后句子的字符串。

示例 1:

输入:sentence = "there are $1 $2 and 5$ candies in the shop", discount = 50
输出:"there are $0.50 $1.00 and 5$ candies in the shop"
解释:
表示价格的单词是 "$1""$2"- "$1" 减免 50%"$0.50" ,所以 "$1" 替换为 "$0.50"- "$2" 减免 50%"$1" ,所以 "$1" 替换为 "$1.00" 。
示例 2:

输入:sentence = "1 2 $3 4 $5 $6 7 8$ $9 $10$", discount = 100
输出:"1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$"
解释:
任何价格减免 100% 都会得到 0 。
表示价格的单词分别是 "$3""$5""$6""$9"。
每个单词都替换为 "$0.00"。
 

提示:

1 <= sentence.length <= 105
sentence 由小写英文字母、数字、' ''$' 组成
sentence 不含前导和尾随空格
sentence 的所有单词都用单个空格分隔
所有价格都是 正 整数且不含前导零
所有价格 最多 为  10 位数字
0 <= discount <= 100

解法一:(遍历拼接)

class Solution:
    def discountPrices(self, sentence: str, discount: int) -> str:
        contexts = sentence.split(" ")
        for index, context in enumerate(contexts):
            if context[0] == "$":
                t = context[1:]
                try:
                    t = float(t)
                    t = str(round(t * (1 - discount / 100), 2))
                    context = context[0] + t
                    if len(context.split(".")[-1]) == 1:
                        context += "0"
                    contexts[index] = context
                except:
                    continue
        return " ".join(contexts)

解法二:

class Solution:
    def discountPrices(self, sentence: str, discount: int) -> str:
        words = sentence.split()  
        res = []
        for w in words:
            if w[0] == '$' and w[1:].isnumeric():  
                price = int(w[1:])
                price -= price * 0.01 *discount  
                res.append('$%.2f'%price)  
            else:
                res.append(w)
        return ' '.join(res)

解法三:(正则)

class Solution:
    def discountPrices(self, sentence: str, discount: int) -> str:
        res = ''
        for ch in sentence.split():
            if re.findall("^\$\d+$",ch):
                res += '$' + str("%.2f" % ( float(ch[1:]) * (100 - discount) / 100)) + ' '
            else:
                res += ch  + ' '
        return res[:-1]

3.使数组按非递减顺序排列

给你一个下标从 0 开始的整数数组 nums 。在一步操作中,移除所有满足 nums[i - 1] > nums[i] 的 nums[i] ,其中 0 < i < nums.length 。
重复执行步骤,直到 nums 变为 非递减 数组,返回所需执行的操作数。

示例 1:

输入:nums = [5,3,4,4,7,3,6,11,8,5,11]
输出:3
解释:执行下述几个步骤:
- 步骤 1[5,3,4,4,7,3,6,11,8,5,11] 变为 [5,4,4,7,6,11,11]
- 步骤 2[5,4,4,7,6,11,11] 变为 [5,4,7,11,11]
- 步骤 3[5,4,7,11,11] 变为 [5,7,11,11]
[5,7,11,11] 是一个非递减数组,因此,返回 3 。
示例 2:

输入:nums = [4,5,7,7,13]
输出:0
解释:nums 已经是一个非递减数组,因此,返回 0 。
 

提示:

1 <= nums.length <= 105
1 <= nums[i] <= 109

解法:

class Solution:
    def totalSteps(self, nums: List[int]) -> int:
        ans, st = 0, []
        for num in nums:
            max_t = 0
            while st and st[-1][0] <= num:
                max_t = max(max_t, st.pop()[1])
            if st: max_t += 1
            ans = max(ans, max_t)
            st.append((num, max_t))
        return ans

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

年中初界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值