当前位置:网站首页>previous permutation lintcode51
previous permutation lintcode51
2022-07-03 10:59:00 【换个名字就很好】
TLE版本
这个版本无法通过。只是为了自己加深dfs的理解。
时间复杂度O(N^N), 对于每一个位置,都要遍历n次, 有n个位置。
from typing import (
List,
)
class Solution:
"""
@param nums: A list of integers
@return: A list of integers that's previous permuation
"""
def dfs(self, nums, query, sub_ans, per_lst, visited):
if query == sub_ans:
return True
if len(sub_ans) == len(nums):
per_lst.append(sub_ans[:])
# print(per_lst)
return False
for i in range(len(nums)):
if i not in visited:
sub_ans.append(nums[i])
visited.append(i)
if self.dfs(nums, query, sub_ans, per_lst, visited):
# print(per_lst)
if not per_lst:
return nums[::-1]
return per_lst[-1]
sub_ans.pop()
visited.pop()
def previous_permuation(self, nums: List[int]) -> List[int]:
# write your code here
query = nums.copy()
nums = sorted(nums)
return self.dfs(nums, query, [], [], [])
边栏推荐
- R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
- Excel表格转到Word中,表格不超边缘纸张范围
- AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
- The world's most popular font editor FontCreator tool
- Excel快速跨表复制粘贴
- FL Studio 20无限试用版水果编曲下载
- Cadence background color setting
- Yintai department store ignites the city's "night economy"
- C language AES encryption and decryption
- Double linked list of linear list
猜你喜欢
C语言 AES加解密
基于I2C协议的驱动开发
LeetCode 46:全排列
2022 northeast four provinces match VP record / supplementary questions
836. 合并集合(DAY 63)并查集
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
How to get started embedded future development direction of embedded
多维度监控:智能监控的数据基础
Analysis of EPS electric steering system
Understand go language context in one article
随机推荐
聊聊Flink框架中的状态管理机制
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
Excel quick cross table copy and paste
(2) Base
Spl06-007 air pressure sensor (example of barometer)
Excel表格转到Word中,表格不超边缘纸张范围
Excel快速跨表复制粘贴
R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
FL Studio 20 unlimited trial fruit arranger Download
活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
How should intermediate software designers prepare for the soft test
How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
并发编程-单例
C语言二维数组
【学习笔记】dp 状态与转移
Technical experts from large factories: how can engineers improve their communication skills?
MATLAB提取不规则txt文件中的数值数据(简单且实用)
PHP server interacts with redis with a large number of close_ Wait analysis