LeetCode【514】Freedom Trail

避开生产计划大坑:不懂MPS和MRP的区别,你的SAP PP模块白学了 本文深入解析SAP PP模块中MPS(主生产计划)与MRP(物料需求计划)的核心区别,揭示如何通过正确配置避免生产计划中的常见陷阱。文章详细探讨了MPS的三大缓冲特性和动态平衡算法,帮助企业提升生产计划稳定性,优化资源配置,避免生产线波动。 阅读详情

In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open the door.
Given a string ring, which represents the code engraved on the outer ring and another string key, which represents the keyword needs to be spelled. You need to find the minimum number of steps in order to spell all the characters in the keyword.
Initially, the first character of the ring is aligned at 12:00 direction. You need to spell all the characters in the string key one by one by rotating the ring clockwise or anticlockwise to make each character of the string key aligned at 12:00 direction and then by pressing the center button.
At the stage of rotating the ring to spell the key character key[i]:
You can rotate the ring clockwise or anticlockwise one place, which counts as 1 step. The final purpose of the rotation is to align one of the string ring’s characters at the 12:00 direction, where this character must equal to the character key[i].
If the character key[i] has been aligned at the 12:00 direction, you need to press the center button to spell, which also counts as 1 step. After the pressing, you could begin to spell the next character in the key (next stage), otherwise, you’ve finished all the spelling.

Example:

这里写图片描述

Input: ring = “godding”, key = “gd”
Output: 4
Explanation:
For the first key character ‘g’, since it is already in place, we just need 1 step to spell this character.
For the second key character ‘d’, we need to rotate the ring “godding” anticlockwise by two steps to make it become “ddinggo”.
Also, we need 1 more step for spelling.
So the final output is 4.

下面是一种方法,空间复杂度比较高,为O(N)。

class Solution {
public:
    int findRotateSteps(string ring, string key) {
        vector<vector<int> > pos(26, vector<int>());
        for(int i = 0;i < ring.length();i ++)
            pos[ring[i] - 'a'].push_back(i);

        int len = ring.length();
        vector<vector<int> > nums(2, vector<int>(len, 0));
        for(int i = key.length()-1;i > 0;i --) {
            int now = key[i] - 'a';
            int pre = key[i-1] - 'a';
            for(int j = 0;j < pos[pre].size();j ++) {
                int u = pos[pre][j];
                nums[i%2][u] = INT_MAX;
                for(int k = 0;k < pos[now].size();k ++) {
                    int v = pos[now][k];
                    int dis = min(abs(v - u), len - abs(v - u));
                    nums[i%2][u] = min(nums[i%2][u], nums[(i+1)%2][v] + dis);
                }
            }
        }

        nums[0][0] = INT_MAX;
        for(int k = 0;k < pos[key[0] - 'a'].size();k ++) {
            int v = pos[key[0] - 'a'][k];
            int dis = min(v, len - v);
            nums[0][0] = min(nums[0][0], nums[1][v] + dis);
        }

        return nums[0][0] + key.length();
    }
};
如何用Python打造PubMed API客户端:科研文献检索自动化实践 PubMed提供了官方的Entrez E-utilities API接口,支持程序化检索文献,但直接调用API需处理请求重试、参数封装、数据解析等问题。批量获取文献ID及结构化详情(标题、作者、DOI等)内置重试机制,应对网络波动或API限流统一数据输出格式,便于后续分析(如文献计量、AI文本处理)灵活适配不同检索需求(如指定数据库、结果数量)本文实现的PubMed API客户端,通过封装API请求、重试机制、数据解析,让科研文献检索从“手动点击”变为“代码自动化”。 阅读详情

相关推荐

宏杉MacroSan存储开局指导-流程

目录 1. 导入ODSP授权 2.1.版本确认 2.2.存储设备升级U盘、升级光盘制作方法 开局流程如下: 1. 导入ODSP授权 ODSP授权文件随设备附带,拆箱可见 2.1.版本确认 java模式 html模式 2.2.存储设备升级U盘、升级光盘制作方法 准备工具 空白U盘: 1个,容量要求大于等于4GB。仅制作升级U盘时需要。 制作升级U盘步骤 1.将U盘插入PC机USB口,保证PC机能识别到U盘。 2.运行UltraISO,选择菜单中的 文件-&g...

飞天板砖 4294

leetcode 514 Freedom Trail

In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door....

cmy203的博客 297

无需云服务器:飞牛云NAS+Docker搭建本地WordPress站点全球访问

在数字化浪潮中,网站搭建已成为个人和企业在推广自身品牌与业务的首要选择。飞牛云新推出的fnOS,这款集多功能于一身的nas系统,以其卓越的性能和灵活性,成为理想的本地网站部署平台。本文将带你一探如何在飞牛云NAS上通过Docker部署WordPress,并利用cpolar实现全球访问。

LT15171009269的博客 6539

LeetCode 514 自由之路 HERODING的LeetCode之路

视频游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。 给定一个字符串 ring,表示刻在外环上的编码;给定另一个字符串 key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。 最初,ring 的第一个字符与12:00方向对齐。您需要顺时针或逆时针旋转 ring 以使 key 的一个字符在 12:00 方向对齐,然后按下中心按钮,以此逐个拼写完 key 中的所有字符。 旋转 ring 拼出 key 字

HERODING23的博客 352

保姆级教程:如何用Le2i Fall Detection Dataset训练你的跌倒检测模型(附数据集下载)

本文提供了一份详细的实战指南,教你如何使用Le2i Fall Detection Dataset训练跌倒检测模型。内容涵盖数据集的深度解析、预处理与工程化管道搭建、模型选择与训练策略,以及评估优化与部署考量,旨在帮助开发者从零构建高精度、可应用的跌倒检测系统。

assembly8low的博客 1296

IFIX往Mysql数据库同步数据方法

通过IFix数据库向Mysql数据库同步数据

仙人掌 1603

leetCode 514:自由之路

一、题目内容 电子游戏“辐射4”中,任务“通向自由”要求玩家到达名为“Freedom Trail Ring”的金属表盘,并使用表盘拼写特定关键词才能开门。 给定一个字符串ring,表示刻在外环上的编码;给定另一个字符串key,表示需要拼写的关键词。您需要算出能够拼写关键词中所有字符的最少步数。 最初,ring的第一个字符与12:00方向对齐。您需要顺时针或逆时针旋转 ring 以使key的一个字符在 12:00 方向对齐,然后按下中心按钮,以此逐个拼写完key中的所有字符。 旋转ri...

三中两明的博客 371

Obsidian免费同步全攻略:手把手教你用OneDrive+Remotely Save插件跨设备同步笔记

本文提供了一份详细的Obsidian免费同步方案指南,核心是利用OneDrive云存储与Remotely Save插件实现跨设备笔记同步。文章手把手教学,涵盖插件安装、OneDrive授权配置、桌面端与移动端的同步设置,并解答了常见问题,帮助用户在不依赖官方付费服务的情况下,构建稳定、自主的免费同步工作流。

bash7scripter的博客 860

LeetCode第 514 题:自由之路(C++)

514. 自由之路 - 力扣(LeetCode) 一开始感觉是要用贪心,后来发现贪心是错的。 还是dp吧,详解可以看官方题解:自由之路 - 自由之路 - 力扣(LeetCode),还是比较好理解的。 class Solution { public: int dp[110][110];//从前往后拼出key的第i个字符,ring 的第j个字符与12:00方向对齐的最少步数 int findRotateSteps(string ring, string key) { int m

zj 381

Solidworks在Windows 11上安装失败?这里有解决方案

通过上述步骤,你应该能够解决在Windows 11上安装Solidworks时遇到的问题。这个方法通过调整注册表的设置,提高了安装程序的权限,从而有效避免了安装过程中的权限障碍。当然,每个系统的具体情况可能有所不同,如果在尝试上述方法后仍然无法解决问题,建议联系Solidworks的技术支持团队,获取更专业的帮助。希望本文能够对你有所帮助,祝你工作顺利,设计无忧!

nntxthml的博客 4931

044.树莓派4B部署YOLO:从系统烧录到模型运行

《树莓派4B部署YOLOv8实战笔记》摘要:本文详细记录了在ARM架构树莓派4B上部署YOLOv8的全流程。关键点包括:必须使用64位系统并手动编译OpenCV(禁用FFMPEG、开启NEON优化);模型需在x86转换为TFLite格式(分辨率不超过320×320);推理代码使用tflite_runtime替代TensorFlow以减少内存占用;针对USB摄像头卡顿、内存泄漏等典型问题给出了解决方案。

2603_95664987的博客 173

C++ | Leetcode C++题解之第514题自由之路

C++ | Leetcode C++题解之第514题自由之路

Ddddddd_158的博客 535

【嵌入式】AMIC和DMIC(什么是数字麦和模拟麦)

最近在做一个关于MIC的项目,同事不太清楚AMIC和DMIC的区别,又有高手详细解释了其原理,在此整理记录一下,方便后续查阅。

Bigan(安)的博客 8556

Freedom Trail [LeetCode 514]

Freedom Trail [LeetCode 514]

Susie的博客 600

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 514: Freedom Trail 解题与思考

LeetCode 514: Freedom Trail 解题与思考[原题链接]题目描述我们有一个轮盘,轮盘上有一个首尾相接的字符串S,我们有如下操作 1、转轮盘到下一个或者上一个字符,需要一步操作 2、输出当前字符,需要一步操作 假如我们有目标字符串K,求取出K的最小操作步数思路第一感觉就是用DP,因为看上去除去末尾的“上一个字符串”所需要的距离,加上从上一个最小距离,然后求极小值,就是所需答

码农a嘘的博客 548

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode 514. Freedom Trail

一、题目描述 In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to open th...

AzureoSky的博客 509

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[Leetcode] 514. Freedom Trail 解题报告

题目: In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open

魔豆(Magicbean)的博客 554

LeetCode //C - 514. Freedom Trail

【代码】LeetCode //C - 514. Freedom Trail。

Made in Code 1531

leetcode 514 freedom trail

In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door.

qq_28317503的博客 396

[LeetCode 514] Freedom Trail

In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword in order to open the door....

ExcitedZhang的博客 215

[LeetCode]514. Freedom Trail

https://leetcode.com/problems/freedom-trail/#/description 给一个环和一个key,判断环经多少步可以得到key, Input: ring = "godding", key = "gd" Output: 4 Explanation: For the first key character 'g', since

gqk289的专栏 976

Leetcode 514. Freedom Trail

问题描述: In the video game Fallout 4, the quest “Road to Freedom” requires players to reach a metal dial called the “Freedom Trail Ring”, and use the dial to spell a specific keyword in order to

u010370157的博客 1005

leetcode 514. Freedom Trail

In the video game Fallout 4(辐射4), the quest(追寻,探索) "Road to Freedom" requires players to reach a metal dial(表盘,拨号盘) called the "Freedom Trail Ring", and use the dial to spell a specific keyword in ord

淡然坊 913

LeetCode514. Freedom Trail

In the video game Fallout 4, the quest"Road to Freedom"requires players to reach a metal dial called the"Freedom Trail Ring"and use the dial to spell a specific keyword to open the door. Given a stringringthat represents the code engraved on the out...

李歇特冯-兹拜因巴哈的博客 191

[leetcode]514. Freedom Trail

题目链接:https://leetcode.com/problems/freedom-trail/#/description In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and

xiaocong1990的博客 633

2017-09-16 LeetCode_514 Freedom Trail

514. Freedom Trail In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal dial called the "Freedom Trail Ring", and use the dial to spell a specific keyword

qq_33575106的博客 256
上一篇: LeetCode【472】 Concatenated Words
下一篇: LeetCode【576】 Out of Boundary Paths
mattmu
博客等级 码龄10年 1粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值