LeetCode知识点总结 - 955

LeetCode 955. Delete Columns to Make Sorted II

考点难度
GreedyEasy
题目

You are given an array of n strings strs, all of the same length.

We may choose any deletion indices, and we delete all the characters in those indices for each string.

For example, if we have strs = [“abcdef”,“uvwxyz”] and deletion indices {0, 2, 3}, then the final array after deletions is [“bef”, “vyz”].

Suppose we chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2] <= … <= strs[n - 1]). Return the minimum possible value of answer.length.

思路

用counter记录每个数的个数。sort array,从最小的数x开始,如果是正数找x*2,其他情况找x/2,如果没有返回false。

答案
class Solution(object):
    def canReorderDoubled(self, arr):
        cnt = Counter(arr)
        arr.sort()
        for x in arr:
            if cnt[x] == 0: continue
            if x < 0 and x % 2 != 0: return False 
            y = x * 2 if x > 0 else x // 2
            if cnt[y] == 0: return False  
            cnt[x] -= 1
            cnt[y] -= 1
        return True      
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值