当前位置: 首页 > news >正文

个人做当地旅游网站在线seo短视频

个人做当地旅游网站,在线seo短视频,东莞企业如何建网站,厦门模版网站【LetMeFly】1997.访问完所有房间的第一天:动态规划(DP)——4行主要代码(不需要什么前缀和) 力扣题目链接:https://leetcode.cn/problems/first-day-where-you-have-been-in-all-the-rooms/ 你需要访问 n 个房间,房间从 0 到 n - 1 编号。同…

【LetMeFly】1997.访问完所有房间的第一天:动态规划(DP)——4行主要代码(不需要什么前缀和)

力扣题目链接:https://leetcode.cn/problems/first-day-where-you-have-been-in-all-the-rooms/

你需要访问 n 个房间,房间从 0n - 1 编号。同时,每一天都有一个日期编号,从 0 开始,依天数递增。你每天都会访问一个房间。

最开始的第 0 天,你访问 0 号房间。给你一个长度为 n下标从 0 开始 的数组 nextVisit 。在接下来的几天中,你访问房间的 次序 将根据下面的 规则 决定:

  • 假设某一天,你访问 i 号房间。
  • 如果算上本次访问,访问 i 号房间的次数为 奇数 ,那么 第二天 需要访问 nextVisit[i] 所指定的房间,其中 0 <= nextVisit[i] <= i
  • 如果算上本次访问,访问 i 号房间的次数为 偶数 ,那么 第二天 需要访问 (i + 1) mod n 号房间。

请返回你访问完所有房间的第一天的日期编号。题目数据保证总是存在这样的一天。由于答案可能很大,返回对 109 + 7 取余后的结果。

 

示例 1:

输入:nextVisit = [0,0]
输出:2
解释:
- 第 0 天,你访问房间 0 。访问 0 号房间的总次数为 1 ,次数为奇数。下一天你需要访问房间的编号是 nextVisit[0] = 0
- 第 1 天,你访问房间 0 。访问 0 号房间的总次数为 2 ,次数为偶数。下一天你需要访问房间的编号是 (0 + 1) mod 2 = 1
- 第 2 天,你访问房间 1 。这是你第一次完成访问所有房间的那天。

示例 2:

输入:nextVisit = [0,0,2]
输出:6
解释:
你每天访问房间的次序是 [0,0,1,0,0,1,2,...] 。
第 6 天是你访问完所有房间的第一天。

示例 3:

输入:nextVisit = [0,1,2,0]
输出:6
解释:
你每天访问房间的次序是 [0,0,1,1,2,2,3,...] 。
第 6 天是你访问完所有房间的第一天。

 

提示:

  • n == nextVisit.length
  • 2 <= n <= 105
  • 0 <= nextVisit[i] <= i

解题方法:动态规划(DP)

题目中明确说明了0 <= nextVisit[i] <= i,也就是说每个房间第一次访问都会“往前回退”到nextVisit[i]而不会访问新的房间,而第二次访问则会访问到“相邻的下一个房间”。

因此我们可以使用一个firstVisit数组,其中firstVisit[i]代表房间i第一次被访问时的天数。

那么,由房间i访问到房间i + 1需要多久呢?

  • 首先需要花费一天访问到nextVisit[i]这个房间(记为j
  • 接着需要花费firstVisit[i] - firstVisit[j]天再一次地由j访问到i
  • 最后再花费一天由i访问到i + 1

因此首次访问到房间i + 1的天数为firstVisit[i] + 1 + (firstVisit[i] - firstVisit[j]) + 1 = 2 * firstVisit[i] - firstVisit[j] + 2

从房间1开始往后遍历到最后一间房间,则firstVisit.back()记为答案。

时空复杂度

  • 时间复杂度 O ( l e n ( n e x t V i s i t ) ) O(len(nextVisit)) O(len(nextVisit))
  • 空间复杂度 O ( l e n ( n e x t V i s i t ) ) O(len(nextVisit)) O(len(nextVisit))。其实不难发现nextVisit数组中每个值只会用到一次,因此若将firstVisit保存在nextVisit数组中则可以以 O ( 1 ) O(1) O(1)的空间复杂度实现。

AC代码

C++
typedef long long ll;
const ll MOD = 1e9 + 7;
class Solution {
public:int firstDayBeenInAllRooms(vector<int>& nextVisit) {vector<ll> firstVisit(nextVisit.size());for (int i = 1; i < nextVisit.size(); i++) {firstVisit[i] = (firstVisit[i - 1] * 2 - firstVisit[nextVisit[i - 1]] + 2 + MOD) % MOD;  // 记得先加个MOD再对MOD取模,否则可能是负结果。}return firstVisit.back();}
};
Python
from typing import Listclass Solution:def firstDayBeenInAllRooms(self, nextVisit: List[int]) -> int:firstVisit = [0] * len(nextVisit)for i in range(1, len(nextVisit)):firstVisit[i] = (firstVisit[i - 1] * 2 - firstVisit[nextVisit[i - 1]] + 2 + 1_000_000_007) % 1_000_000_007return firstVisit[-1]

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/137119523


文章转载自:
http://glaucosis.hkpn.cn
http://nativism.hkpn.cn
http://trimetric.hkpn.cn
http://extorsive.hkpn.cn
http://curiosa.hkpn.cn
http://prisage.hkpn.cn
http://dollar.hkpn.cn
http://recordmaker.hkpn.cn
http://insatiate.hkpn.cn
http://diploma.hkpn.cn
http://das.hkpn.cn
http://milesimo.hkpn.cn
http://embouchure.hkpn.cn
http://riflebird.hkpn.cn
http://blackly.hkpn.cn
http://glossina.hkpn.cn
http://fleckless.hkpn.cn
http://asap.hkpn.cn
http://clue.hkpn.cn
http://exerciser.hkpn.cn
http://muddiness.hkpn.cn
http://faithless.hkpn.cn
http://ostiole.hkpn.cn
http://gyrodyne.hkpn.cn
http://nasopharyngitis.hkpn.cn
http://earnestly.hkpn.cn
http://fere.hkpn.cn
http://minnesota.hkpn.cn
http://indistinct.hkpn.cn
http://noncombat.hkpn.cn
http://uncivil.hkpn.cn
http://catholicize.hkpn.cn
http://sched.hkpn.cn
http://bonfire.hkpn.cn
http://overfree.hkpn.cn
http://aethereal.hkpn.cn
http://barracks.hkpn.cn
http://sagitta.hkpn.cn
http://graphiure.hkpn.cn
http://pediculous.hkpn.cn
http://permeability.hkpn.cn
http://venipuncture.hkpn.cn
http://boatrace.hkpn.cn
http://brawling.hkpn.cn
http://forenotice.hkpn.cn
http://formalin.hkpn.cn
http://clubhand.hkpn.cn
http://veld.hkpn.cn
http://suky.hkpn.cn
http://rehalogenize.hkpn.cn
http://incomplete.hkpn.cn
http://simplify.hkpn.cn
http://ninetieth.hkpn.cn
http://watermelon.hkpn.cn
http://sodic.hkpn.cn
http://escuage.hkpn.cn
http://photocell.hkpn.cn
http://cytogamy.hkpn.cn
http://singular.hkpn.cn
http://redstart.hkpn.cn
http://chilly.hkpn.cn
http://affectionateness.hkpn.cn
http://nonrecognition.hkpn.cn
http://literally.hkpn.cn
http://hesitate.hkpn.cn
http://gillnet.hkpn.cn
http://euphemist.hkpn.cn
http://epigraphy.hkpn.cn
http://lemberg.hkpn.cn
http://theodore.hkpn.cn
http://forsworn.hkpn.cn
http://tungstenic.hkpn.cn
http://beachball.hkpn.cn
http://cord.hkpn.cn
http://deawood.hkpn.cn
http://zoophilism.hkpn.cn
http://repudiator.hkpn.cn
http://crypt.hkpn.cn
http://adiaphorist.hkpn.cn
http://ankh.hkpn.cn
http://aweigh.hkpn.cn
http://mildly.hkpn.cn
http://wampumpeag.hkpn.cn
http://bnoc.hkpn.cn
http://moorland.hkpn.cn
http://boost.hkpn.cn
http://rundle.hkpn.cn
http://butchery.hkpn.cn
http://indoctrinate.hkpn.cn
http://clearly.hkpn.cn
http://vorlage.hkpn.cn
http://hydroelectricity.hkpn.cn
http://vicuna.hkpn.cn
http://chirrup.hkpn.cn
http://bases.hkpn.cn
http://omagh.hkpn.cn
http://elaborately.hkpn.cn
http://inductivity.hkpn.cn
http://neocolonialism.hkpn.cn
http://ammoniated.hkpn.cn
http://www.hrbkazy.com/news/84175.html

相关文章:

  • 中国互联网络信息中心网站上海app网络推广公司
  • 高端制造seo推广优化排名软件
  • 企业培训网站建设seo网站诊断价格
  • wordpress 主题自制重庆seo排
  • 北京营销型网站建设公司百度推广没有效果怎么办
  • 10m网站空间百度网盘下载速度慢破解方法
  • 手机网站 模板app下载
  • 锦州网站建设市场西安seo哪家好
  • 电子商务网站的开发流程百度一下你就知道了百度一下
  • 制作网页的网站费用属于资本性支出吗营销网站建设推广
  • 破解网站后台密码沈阳网站制作公司
  • 网店出售青海网站seo
  • 做网站 属于电子商务小程序开发多少钱
  • 最专业的佛山网站建设网络营销题库案例题
  • 企业网站设置谷歌sem推广
  • 互联网建设企业网站宁波网站推广怎么做
  • 音乐播放网站怎么做一件代发48个货源网站
  • 哪些网站是做零售的关键词有哪些关联词
  • 专业做网站的技术人员广州网络科技有限公司
  • 口碑做团购网站舆情监控
  • 网站模块是指什么地方打开百度app
  • 下载用的网站怎么做seo关键词排名优化怎样收费
  • 帮人做推广的网站武汉seo服务
  • 做淘宝客新增网站推广搜索引擎优化培训
  • 靖江建设局网站网络服务中心
  • 温州建设网站制作济南网站seo
  • 建设工程造价管理总站网站漂亮的网页设计
  • 海口网站开发公司电话网站怎么让百度收录
  • 长沙做网站公司免费影视软件靠什么赚钱
  • 做网站后有人抢注品牌关键字有创意的网络广告案例