其他分享
首页 > 其他分享> > 每日一练 — 2022.02.08

每日一练 — 2022.02.08

作者:互联网

文章目录


一,买卖股票的最佳时机 III

1,程序简介

  1. 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

  2. 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意:

示例 1:

示例 2:

示例 3:

示例 4:

提示:

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Feb  8 18:43:26 2022
Function: 买卖股票的最佳时机 III
@author: 小梁aixj
"""
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices) == 0:
            return 0
        k = 2
        n = len(prices)
        dp_i_1_0 = 0
        dp_i_1_1 = -prices[0]
        dp_i_2_0 = 0
        dp_i_2_1 = -prices[0]
        for i in range(1, n):
            dp_i_1_0 = max(dp_i_1_0, dp_i_1_1 + prices[i])
            dp_i_1_1 = max(dp_i_1_1, -prices[i])
            dp_i_2_0 = max(dp_i_2_0, dp_i_2_1 + prices[i])
            dp_i_2_1 = max(dp_i_2_1, dp_i_1_0 - prices[i])
        return dp_i_2_0

二,单词接龙

1,程序简介

字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列:

给你两个单词 beginWord 和 endWord 和一个字典 wordList ,找到从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0。

示例 1:

示例 2:

提示:

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Feb  8 18:44:21 2022
Function: 单词接龙
@author: 小梁aixj
"""
class Solution:
    def ladderLength(self, beginWord, endWord, wordList):
        """
        :type beginWord: str
        :type endWord: str
        :type wordList: List[str]
        :rtype: int
        """
        if endWord not in wordList:
            return 0
        if beginWord in wordList:
            wordList.remove(beginWord)
        wordDict = dict()
        for word in wordList:
            for i in range(len(word)):
                tmp = word[:i] + "_" + word[i + 1 :]
                wordDict[tmp] = wordDict.get(tmp, []) + [word]
        stack, visited = [(beginWord, 1)], set()
        while stack:
            word, step = stack.pop(0)
            if word not in visited:
                visited.add(word)
                if word == endWord:
                    return step
                for i in range(len(word)):
                    tmp = word[:i] + "_" + word[i + 1 :]
                    neigh_words = wordDict.get(tmp, [])
                    for neigh in neigh_words:
                        if neigh not in visited:
                            stack.append((neigh, step + 1))
        return 0

三,二叉树展开为链表

1,程序简介

给你二叉树的根结点 root ,请你将它展开为一个单链表:

示例 1:

在这里插入图片描述

示例 2:

示例 3:

提示:

进阶:

2,程序代码

# -*- coding: utf-8 -*-
"""
Created on Tue Feb  8 18:44:45 2022
Function: 二叉树展开为链表
@author: 小梁aixj
"""
class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        while root != None:
            if root.left == None:
                root = root.right
            else:
                pre = root.left
                while pre.right != None:
                    pre = pre.right
                pre.right = root.right
                root.right = root.left
                root.left = None
                root = root.right

标签:wordList,示例,08,2022.02,beginWord,endWord,root,每日,dp
来源: https://blog.csdn.net/m0_62617719/article/details/122829303