建设银行官网首页网站招聘在线科技成都网站推广公司
工厂有若干条生产线,可以生产不同型号的产品,要求实现功能如下:1. ProductionLineMgmtSys 初始化生产线和产品的生产周期有num条生产线,编号从0开始periods[i]表示生产一个型号为i的产品所需的生产周期,单位为天2. Produce 在生产线assemblyId上,从日期date开始持续生产型号productId的产品若生产线空闲,开始生产并返回1若生产线正在生产该型号产品,则命令忽略,返回0若正在生产其他型号,立刻切换,返回-1产品完成日期:若某生产线在date开始生产某型号产品,生产周期为period,则日期date+N*period均为「产品完成日期」假设date为2,period为3,则完成日期为5、8、11...生产切换规则:若非在产品完成日期发生切换,则正在生产的产品未完成,废弃不计若在产品完成日期发生切换,则计数,并同时启动下一产品的生产。3. GetProductCount 查询截止date(含),累计完成型号productId的产品数量。生产完成即 产品完成日期<=date
输入输出样例:
["ProductionLineMgmtSys","produce","getProductCount","getProductCount"]
[[2,[1,3]],[0,1,1],[8,1],[9,1]]
[null,1,2,3]["ProductionLineMgmtSys","produce","produce","getProductCount","produce","produce","getProductCount","produce","produce","produce","getProductCount","produce"]
[[5,[1,4,5,8,2,3]],[0,1,0],[1,1,5],[4,0],[5,0,5],[7,0,0],[7,5],[8,1,5],[8,4,0],[9,1,0],[10,0],[1000,4,0]]
[null,1,-1,1,1,-1,2,0,1,-1,7,0]题给代码:
class ProductionLineMgmtSys {public:ProductionLineMgmtSys(int num, const vector<int>& periods){}int Produce(int date, int assemblyId, int productId){}int GetProductCount(int date, int productId){}
};
from typing import List
import copyclass ProductionLineMgmtSys:def __init__(self, num: int, periods: List[int]):
self.produc_line = dict.fromkeys(range(num)) #key:生产线id value:生产产品id
self.produc_number = dict.fromkeys(range(len(periods)), 0) #key:产品id value:生产数量
self.produc_line_product_start_date = dict.fromkeys(range(num)) #key:生产线id value:生产产品开始时间
self.periods = periodsdef produce(self, date: int, assembly_id: int, product_id: int) -> int:
if self.produc_line[assembly_id] == None:
self.produc_line[assembly_id] = product_id
self.produc_line_product_start_date[assembly_id] = date
return 1
elif self.produc_line[assembly_id] == product_id:
return 0
else:
i = 0
while(self.produc_line_product_start_date[assembly_id] + i * self.periods[self.produc_line[assembly_id]] <= date):
i += 1
self.produc_number[self.produc_line[assembly_id]] += i - 1
self.produc_line_product_start_date[assembly_id] = date
self.produc_line[assembly_id] = product_id
return -1def get_product_count(self, date: int, product_id: int) -> int:
product_achive_number = 0
if product_id in self.produc_line.values():
product_id_in_produc_line = [i for i,x in enumerate(self.produc_line.values()) if x == product_id]
#该生产线还在生产对应产品
produc_number1 = copy.deepcopy(self.produc_number)
for assembly_id1 in product_id_in_produc_line:
i = 0
while(self.produc_line_product_start_date[assembly_id1] + i * self.periods[product_id] <= date):
i += 1
produc_number1[product_id] += copy.deepcopy(i - 1)
product_achive_number += copy.deepcopy(produc_number1[product_id])
else:
product_achive_number = copy.deepcopy(self.produc_number[product_id])
return product_achive_numberif __name__ == '__main__':# ms = ProductionLineMgmtSys(2,[1,3])
# p = ms.produce(0,1,1)
# g = ms.get_product_count(8,1)
# g1 = ms.get_product_count(9,1)
#["ProductionLineMgmtSys","produce","produce","getProductCount","produce","produce","getProductCount","produce","produce","produce","getProductCount","produce"]
#
#[[5,[1,4,5,8,2,3]],[0,1,0],[1,1,5],[4,0],[5,0,5],[7,0,0],[7,5],[8,1,5],[8,4,0],[9,1,0],[10,0],[1000,4,0]]ms = ProductionLineMgmtSys(5,[1,4,5,8,2,3])
p1 = ms.produce(0,1,0)
p2 = ms.produce(1,1,5)
g3 = ms.get_product_count(4,0)
p4 = ms.produce(5,0,5)
p5 = ms.produce(7,0,0)
g6 = ms.get_product_count(7,5)
p7 = ms.produce(8,1,5)
p8 = ms.produce(8,4,0)
p9 = ms.produce(9,1,0)
g10 = ms.get_product_count(10,0)
p11 = ms.produce(1000,4,0)
s = 2
某工厂使用激光刀切割材料。激光刀具有开启和关闭两种状态,并可转向和移动。支持指令如下:
O —— 开启激光刀(即 “OPEN”),指令执行后激光刀处于开启状态
C —— 关闭激光刀(即 “CLOSE”),指令执行后激光刀处于关闭状态
M —— 激光刀沿着当前方向前进一段距离(即 “MOV”),若激光刀在开启状态下,会同步进行材料切割,否则只移动不切割
U,D,L,R —— 改变激光刀的前进方向、但不移动(上:U,下:D,左:L,右:R)
给定一块高为 height,宽为 width 的材料。初始时,激光刀位于材料左上角,处于关闭状态,前进方向为朝下 (D)。
激光刀的操作指令依次记录于字符串 operations,所有 M 指令的移动距离依次记录于数组 distances,distances[i] 表示第 i 个 M 指令的移动距离。
请计算完成操作指令后,材料板上被切割出了多少个 1 x 1 的方块。
注意:输入数据保证激光刀始终在材料初始范围内,可在材料边上,即:激光刀的位置 [row, col] 始终满足 0 <= row <= height,0 <= col <= width。
示例 1:
输入:
height = 3
width = 4
operations = “MRMOMDMLMUMCRMODMC”
distances = [1,1,2,1,2,2,1,2]
输出:3
解释:operations 中有 8 个 M,对应的移动距离在数组distances中。
如图所示,激光刀移动的位置顺序 0->1->2->3->…->8 。其中黄色的线是激光刀开启情形下切割的路径。这样最终图中的 3 个阴影位置是切割所得的 1 x 1 方块。
示例 2:
输入:
height = 3
width = 4
operations = “MRMOMDMLMUMC”
distances = [1,1,2,1,2,2]
输出:0
解释:如图所示,没有切割出 1 x 1 的方块,故返回 0。
注意:下图中切出的是一个 2 x 1 的材料块,不是两个 1 x 1 的方块。
示例 3:
输入:
height = 3
width = 3
operations = “MROMDMLMUMRMDMRMUMLMC”
distances = [1,2,1,2,1,1,1,1,1,1]
输出:2
提示:
1 <= height, width <= 100
0 <= operations.length <= 10^4
distances.length 等于 operations 中 M 的个数
operations[i] 仅为 ‘O’、‘C’、‘U’、‘D’、‘L’、‘R’、‘M’
一条边可能会被多次切割
解题思路: 判断格子的四条边是否被切割过, 一共有水平边w * (h + 1) ,垂直边 h * (w + 1)
from typing import Listclass Solution:def __init__(self):self.block_num = 0self.line_h = []self.line_v = []def init_block(self, height, width):self.line_h = [[0 for _ in range(width)] for _ in range(height + 1)]self.line_v = [[0 for _ in range(height)] for _ in range(width + 1)]for x in range(0, width):self.line_h[0][x] = 1self.line_h[height][x] = 1for y in range(0, height):self.line_v[0][y] = 1self.line_v[width][y] = 1def mv_knife(self, height: int, width: int, operations: str, distances: List[int]):knife_status = 'C'move_command = 'D'command_count = -1cur_x = 0cur_y = 0for command in list(operations):if command == 'O' or command == 'C':knife_status = commandcontinueelif command == 'M':command_count += 1else:move_command = commandcontinueif move_command == 'U':next_y = cur_y - distances[command_count]if knife_status == 'O':for position in range(next_y, cur_y):self.line_v[cur_x][position] = 1cur_y = next_yelif move_command == 'D':next_y = cur_y + distances[command_count]if knife_status == 'O':for position in range(cur_y, next_y):self.line_v[cur_x][position] = 1cur_y = next_yelif move_command == 'L':next_x = cur_x - distances[command_count]if knife_status == 'O':for position in range(next_x, cur_x):self.line_h[cur_y][position] = 1cur_x = next_xelif move_command == 'R':next_x = cur_x + distances[command_count]if knife_status == 'O':for position in range(cur_x, next_x):self.line_h[cur_y][position] = 1cur_x = next_xdef get_block_num(self, height, width):for x in range(0, width):for y in range(0, height):if self.line_h[y][x] and self.line_h[y + 1][x] and self.line_v[x][y] and self.line_v[x + 1][y]:self.block_num += 1return self.block_numdef get_unit_block_num(self, height: int, width: int, operations: str, distances: List[int]) -> int:self.init_block(height, width)self.mv_knife(height, width, operations, distances)self.get_block_num(height, width)return self.block_numheight = 3
width = 4
operations = "MRMOMDMLMUMCRMODMC"
distances = [1, 1, 2, 1, 2, 2, 1, 2]ss = Solution()
print(ss.get_unit_block_num(height, width, operations, distances))