Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of the Best combination of Tourism by LeetCode

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to solve the problem of the best tourism combination with LeetCode. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

one

Topic description

Given a positive integer array A _ (I) A [I] represents the score of the first tourist attraction, and the distance between the two scenic spots I and j is j-I. A pair of scenic spots (I

< j)组成的观光组合的得分为(A[i] + A[j] + i - j),即景点的评分之和减去它们两者之间的距离。返回一组观光景点中能取得的最高分。如:输入[8,1,5,2,6],返回11(i=0,j=2)。 2 题解 思路:数组变换这道题可以用遍历、双指针、动态规划的方法求解,但看网友的分享后,发现一个最为奇妙的方法:A[i] + A[j] + i - j = (A[i] + i) + ( A[j] - j ),因此对于A[j] - j ,只需找到最大的A[i] + i即可,此处i int: if len(A)ans: ans = A[i]-i+max_value if A[i]+i>

Max_value: max_value = A [I] + i return ans, this is the end of the article on "how to solve the problem of the best tourism combination in LeetCode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 213

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report