LeetCode笔记:Weekly Contest 327

0. 做题感受

这次的题目一看大佬们的用时,最快的居然也花了将近30min,把我吓了一大跳,结果真正做下来就有点无语了,就是超级繁琐的各种分类讨论以及边界条件,难度倒是感觉还好,但是真要再做一遍估计还得去掉半条命,简直了……

1. 题目一

给出题目一的试题链接如下:

1. 解题思路

这一题有序数列的话其实可以二分找边界,不过我这边就比较暴力的直接遍历统计了一遍就是了……

2. 代码实现

给出python代码实现如下:

class Solution:
    def maximumCount(self, nums: List[int]) -> int:
        pos, neg = 0, 0
        for x in nums:
            if x < 0:
                neg += 1
            elif x > 0:
                pos += 1
        return max(pos, neg)

提交代码评测得到:耗时135ms,占用内存14.2MB。

3. 题目二

给出题目二的试题链接如下:

1. 解题思路

这一题其实就是不断地去最大元素进行操作,然后更新数组就行了。

问题就在于如何快速地更新数组以及取最大值,我是采用了堆排,不过全有序排序也问题不大就是了。

2. 代码实现

给出python代码实现如下:

class Solution:
    def maxKelements(self, nums: List[int], k: int) -> int:
        nums = [-x for x in nums]
        score = 0
        heapq.heapify(nums)
        for _ in range(k):
            x = -heapq.heappop(nums)
            score += x
            heapq.heappush(nums, - ceil(x/3))
        return score

提交代码评测得到:耗时1132ms,占用内存29.1MB。

2. 题目三

给出题目三的试题链接如下:

1. 解题思路

这一题我这边的做法就是一个非常暴力的分类讨论,首先统计两个单词之中出现了那些字符,然后分别考察交换两个字符之后可能出现的情况,将所有情况分别枚举一边就行了……

2. 代码实现

给出python代码实现如下:

class Solution:
    def isItPossible(self, word1: str, word2: str) -> bool:
        cnt1 = Counter(word1)
        cnt2 = Counter(word2)
        
        diff = len(cnt1) - len(cnt2)
        
        for ch1 in cnt1:
            for ch2 in cnt2:
                if ch1 == ch2:
                    change = 0
                elif cnt1[ch1] == 1 and cnt2[ch2] == 1:
                    if cnt2[ch1] > 0 and cnt1[ch2] > 0:
                        change = 0
                    elif cnt2[ch1] > 0 and cnt1[ch2] == 0:
                        change = 1
                    elif cnt2[ch1] == 0 and cnt1[ch2] > 0:
                        change = -1
                    else:
                        change = 0
                elif cnt1[ch1] == 1 and cnt2[ch2] > 1:
                    if cnt2[ch1] > 0 and cnt1[ch2] > 0:
                        change = -1
                    elif cnt2[ch1] > 0 and cnt1[ch2] == 0:
                        change = 0
                    elif cnt2[ch1] == 0 and cnt1[ch2] > 0:
                        change = -2
                    else:
                        change = -1
                elif cnt1[ch1] > 1 and cnt2[ch2] == 1:
                    if cnt2[ch1] > 0 and cnt1[ch2] > 0:
                        change = 1
                    elif cnt2[ch1] > 0 and cnt1[ch2] == 0:
                        change = 2
                    elif cnt2[ch1] == 0 and cnt1[ch2] > 0:
                        change = 0
                    else:
                        change = 1
                else:
                    if cnt2[ch1] > 0 and cnt1[ch2] > 0:
                        change = 0
                    elif cnt2[ch1] > 0 and cnt1[ch2] == 0:
                        change = 1
                    elif cnt2[ch1] == 0 and cnt1[ch2] > 0:
                        change = -1
                    else:
                        change = 0
                
                if change == -diff:
                    return True
                        
        return False

提交代码评测得到:耗时114ms,占用内存15.4MB。

4. 题目四

给出题目四的试题链接如下:

1. 解题思路

这一题其实思路不怎么复杂,就是按照题意完全用代码翻译一下,然后模拟一下其完整的过程就可以了。

不过,如前所述,问题就是繁琐……

2. 代码实现

给出python代码实现如下:

class Solution:
    def findCrossingTime(self, n: int, k: int, time: List[List[int]]) -> int:
        eff = sorted([(t[0] + t[2], i) for i, t in enumerate(time)], reverse=True)
        orders = [0 for _ in range(k)]
        for i in range(k):
            orders[eff[i][1]] = i

        right, left = 0, 1
        q = [(left, orders[i], i) for i in range(k)]
        heapq.heapify(q)
        waited = []
        t = 0
        while q or waited:
            while q == [] and waited:
                nxt, side, order, wid = heapq.heappop(waited)
                if n > 0 or side == right:
                    t = max(t, nxt)
                    heapq.heappush(q, (side, order, wid))
            while waited and waited[0][0] <= t:
                _, side, order, wid = heapq.heappop(waited)
                heapq.heappush(q, (side, order, wid))
            if q == []:
                break
            side, order, wid = heapq.heappop(q)
            if side == left:
                if n == 0:
                    continue
                t += time[wid][0]
                heapq.heappush(waited, (t + time[wid][1], right, order, wid))
                n -= 1
            else:
                t += time[wid][2]
                heapq.heappush(waited, (t + time[wid][3], left, order, wid))
        return t      

提交代码评测得到:744ms,占用内存21.2MB。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值