Lintcode 945. Task Scheduler (Medium) (Python)

Task Scheduler

Description:

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.
Example

Given tasks = [‘A’,’A’,’A’,’B’,’B’,’B’], n = 2, return 8.

Explanation:
A -> B -> idle -> A -> B -> idle -> A -> B.

Notice

The number of tasks is in the range [1, 10000].
The integer n is in the range [0, 100].

Code:

1.

class Solution:
    """
    @param tasks: the given char array representing tasks CPU need to do
    @param n: the non-negative cooling interval
    @return: the least number of intervals the CPU will take to finish all the given tasks
    """
    def leastInterval(self, tasks, n):
        # write your code here
        cnt=collections.Counter(tasks)
        vmax = max(cnt.values())
        count = 0
        for i in cnt:
            if cnt[i]==vmax:
                count+=1
        return max(len(tasks), (vmax-1)*(n+1)+count)

2.

class Solution:
    """
    @param tasks: the given char array representing tasks CPU need to do
    @param n: the non-negative cooling interval
    @return: the least number of intervals the CPU will take to finish all the given tasks
    """
    def leastInterval(self, tasks, n):
        # write your code here
        cnt = collections.Counter(tasks)
        vmax = max(cnt.values())
        slots = (vmax - 1) * n
        return max(len(tasks), len(tasks)+slots+vmax-1-sum(n-(n==vmax) for n in cnt.values()))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
torch.optim.lr_scheduler.LambdaLR是PyTorch中的学习率调度器。它允许我们通过自定义函数来调整优化器的学习率。具体来说,我们可以定义一个接受一个整数参数epoch并返回一个浮点数的函数,该函数的返回值将作为相应时期的学习率因子。也就是说,如果在第epoch个时期调用该函数,那么这个时期的学习率将被设置为当前学习率乘上lr_lambda(epoch)的返回值。我们可以通过传入优化器对象和lr_lambda函数来创建一个LambdaLR对象,然后在训练过程中使用scheduler.step()来更新学习率。\[2\] 举个例子,假设我们想每3个epoch将学习率减半,我们可以定义一个规则函数,如下所示: ```python import torch from torch import nn import math class Net(nn.Module): def __init__(self): super().__init__() self.conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=2, stride=1, padding=0) def forward(self, x): out = self.conv(x) return out net = Net() def rule(epoch): lamda = math.pow(0.5, int(epoch / 3)) return lamda optimizer = torch.optim.SGD(\[{'params': net.parameters(), 'initial_lr': 0.1}\], lr=0.1) scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=rule) for i in range(9): print("lr of epoch", i, "=>", scheduler.get_lr()) optimizer.step() scheduler.step() ``` 在这个例子中,我们定义了一个包含一个卷积层的神经网络模型Net,并定义了一个规则函数rule,该函数根据epoch的值返回一个学习率因子。然后,我们创建了一个SGD优化器对象optimizer和一个LambdaLR学习率调度器对象scheduler,并在每个epoch中使用optimizer.step()来更新模型参数,使用scheduler.step()来更新学习率。最后,我们打印出每个epoch的学习率。\[3\] #### 引用[.reference_title] - *1* *2* [【pytorch】torch.optim.lr_scheduler.LambdaLR() 学习率调整](https://blog.csdn.net/weixin_37804469/article/details/110939799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Pytorch lr_scheduler.LambdaLR()的简单理解与用法](https://blog.csdn.net/qq_40714949/article/details/126287769)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值