LeetCode Weekly Contest 127

第一次参加 LeetCode 的每周编程赛,排在 178 / 4682 ,还是可以接受的。(第一题交了次错答案罚时 5分钟真的伤不起啊不然都可以挺进前100了)

1、1005. Maximize Sum Of Array After K Negations

Given an array A of integers, we must modify the array in the following way: we choose an i and replace A[i] with -A[i], and we repeat this process K times in total. (We may choose the same index i multiple times.)

Return the largest possible sum of the array after modifying it in this way.

Example 1:

Input: A = [4,2,3], K = 1
Output: 5
Explanation: Choose indices (1,) and A becomes [4,-2,3].

题意

一个数组,进行 K 次操作(一次操作是对某个数正负取反,可以对同一个数进行操作),求操作后数组和的最大值。

解题思路

贪心法,对数组排序,先只对负数操作,从最小的开始。全操作完之后,如果K不剩,直接返回和;如果K还剩,说明此时已经全部转化为正数,那么对最小的正数操作。判断一下 K 是否是2的倍数来决定是否操作。

class Solution:
    def largestSumAfterKNegations(self, A: List[int], K: int) -> int:
        A.sort()		# 排序
        target = -1
        for i, ele in enumerate(A):
            if K and ele < 0:
                A[i] = -A[i]
                target = i
                K -= 1
            else:
                break
              
        K = K % 2
        if K > 0:		# 需要对最小的正数取反,找到最小的正数
            if target+1<len(A) and A[target]>A[target+1]:
                A[target+1] = -A[target+1]
            else:
                A[target] = -A[target]
                
        return sum(A)

2、1006. Clumsy Factorial

Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1.

We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.

For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right.

Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This guarantees the result is an integer.

Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.

Example 1:

Input: 4
Output: 7
Explanation: 7 = 4 * 3 / 2 + 1

题意

定义一个新的阶乘运算,依次使用 + - * / 几个符号,求最后结果。

解题思路

直接模拟这个运算,除了遇到减法要考虑到后面三个数字县做运算,其余符号直接按从左到右的顺序来计算即可。

class Solution:
    import math
    def clumsy(self, N: int) -> int:
        oper = 0
        ret = N
        N -= 1
        
        while N >= 1:		# N是当前要处理的数
            if oper == 0:	# *
                ret *= N
                oper = (oper+1)%4
                N -= 1
            elif oper == 1:     # /
                ret = math.floor(ret/N)
                oper = (oper+1)%4
                N -= 1
            elif oper == 2:		# +
                ret += N
                oper = (oper+1)%4
                N -= 1
            else:		# -
                if N >= 3:
                    ret -= (math.floor(N*(N-1)/(N-2)))
                    N -= 3
                    oper = 2		# 运算符变成 +
                elif N == 2:
                    ret -= 2
                    return ret
                elif N == 1:
                    ret -= 1
                    return ret
        return ret

3、1007. Minimum Domino Rotations For Equal Row

In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the i-th domino, so that A[i] and B[i] swap values.

Return the minimum number of rotations so that all the values in A are the same, or all the values in B are the same.

If it cannot be done, return -1.

Example 1:

Input: A = [2,1,2,4,2,2], B = [5,2,6,2,3,2]
Output: 2
Explanation: 
The first figure represents the dominoes as given by A and B: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.

题意

两行数组,至少交换多少次,可以使得上面一行数组或下面的数组一行内内元素全相等。

解题思路

直接暴力枚举。假设最后调整之后一行内元素全为 K,枚举数组A的每个元素作为这个K的可能值,判断AB对应位置是否等于K来判断是否要进行交换操作。加入visit数组去重,加快查找速度。

class Solution:
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        def tryswap(A, B):
            visit = []
            n = len(A)
            minSwapNum = 99999
            for i,ele in enumerate(A):
                if ele in visit:
                    continue
                visit.append(ele)		# 假设它是最后转化的结果
                swapNum = 0
                for j in range(n):
                    if A[j] == ele:
                        continue
                    elif B[j] == ele:
                        swapNum += 1
                    else:
                        swapNum = 99999
                        break
                minSwapNum = min(minSwapNum, swapNum)
            return minSwapNum
        
        swapNum = min(tryswap(A, B), tryswap(B, A))
        if swapNum == 99999:
            return -1
        return swapNum

4、1008. Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal.

(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traversesnode.left, then traverses node.right.)

Example 1:

Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]

在这里插入图片描述

题意

根据先序序列重建二叉搜索树。

解题思路

先序序列第一个元素肯定是根节点。然后向后找到第一个大于它的元素的位置为K,那么list[1:K]是它的左子树,list[K:]是它的右子树,递归建树。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def bstFromPreorder(self, preorder: List[int]) -> TreeNode:
        if len(preorder)==0:
            return None
        root = TreeNode(preorder[0])
        
        target = len(preorder)
        for i,ele in enumerate(preorder):
            if ele > preorder[0]:
                target = i
                break
        root.left = self.bstFromPreorder(preorder[1:target])
        root.right = self.bstFromPreorder(preorder[target:])
        
        return root
内容概要:本文系统研究了双环模型预测控制(MPC)在表贴式永磁同步电机(SPMSM)中的应用,聚焦于转速-电流双环控制结构的建模与Simulink仿真实现。通过建立电机的离散化数学模型,结合模型预测控制理论,详细阐述了预测模型构建、目标函数设计、约束条件处理及优化求解等核心环节,实现了对电机转速与电流的高性能动态调控。研究在Simulink环境中搭建了完整的仿真系统,验证了所提控制策略在动态响应速度、抗干扰能力及稳态精度方面的显著优势,充分展现了MPC在高精度电机驱动领域的应用潜力,为先进电机控制技术的工程化提供了有效的理论依据与实践参考。; 适合人群:具备自动控制理论、电机控制基础知识及Simulink仿真操作经验的电气工程、自动化、电力电子等相关专业的研究生、科研人员和工程技术人员。; 使用场景及目标:①用于高校及科研机构开展先进电机控制算法的教学演示与科研攻关;②为工业界中对高动态性能、高精度要求的电机驱动系统(如数控机床、机器人、新能源汽车电驱动系统)的设计与优化提供技术验证平台;③支撑永磁同步电机在高端制造、绿色能源等战略新兴产业中的先进控制技术研发。; 阅读建议:读者应结合提供的Simulink仿真模型进行深入探究,重点关注预测域、控制域、权重系数等关键参数的整定方法及其对系统整体性能的影响机制,建议通过设置不同工况、引入外部扰动等方式进行对比仿真实验,以深化对模型预测控制内在机理的理解与掌握。
内容概要:本文围绕“基于多VSG独立微网的多目标二次控制MATLAB模型研究”展开,详细阐述了利用Simulink对多虚拟同步发电机(VSG)构成的独立微网系统进行建模与仿真,实现频率调节、电压支撑与有功无功功率均分等多目标协同优化的二次控制策略。研究引入先进的最优控制算法,解决微网在孤岛运行模式下的功率动态分配、频率电压恢复及系统稳定性问题,并通过MATLAB/Simulink平台构建完整仿真模型,验证所提控制策略在不同负载扰动下的有效性、鲁棒性与动态响应性能。; 适合人群:具备电力系统分析、现代控制理论基础以及MATLAB/Simulink仿真能力的电气工程、自动化等相关专业的硕士研究生、科研人员及从事微网控制系统开发的工程技术人才。; 使用场景及目标:① 深入理解多VSG在独立微网中的并联运行机理与协同控制架构;② 掌握基于Simulink的微网二次控制系统的建模方法与仿真流程;③ 实现频率、电压与功率分配的多目标优化控制仿真验证;④ 为微网控制系统的设计、算法优化及科研课题提供可靠的仿真依据和技术参考。; 阅读建议:建议读者结合文中控制策略,动手搭建Simulink模型,重点关注控制器参数整定对系统动态性能的影响,可通过对比不同工况下的仿真结果,进一步优化控制算法以提升系统鲁棒性与响应精度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值