1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最长定差子序列 [longest-arithmetic-subsequence-of-given-difference].html
2022-03-29 12:43:11 +08:00

38 lines
1.2 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个整数数组 <code>arr</code> 和一个整数 <code>difference</code>,请你找出并返回 <code>arr</code> 中最长等差子序列的长度,该子序列中相邻元素之间的差等于 <code>difference</code></p>
<p><strong>子序列</strong> 是指在不改变其余元素顺序的情况下,通过删除一些元素或不删除任何元素而从 <code>arr</code> 派生出来的序列。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>arr = [1,2,3,4], difference = 1
<strong>输出:</strong>4
<strong>解释:</strong>最长的等差子序列是 [1,2,3,4]。</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>arr = [1,3,5,7], difference = 1
<strong>输出:</strong>1
<strong>解释:</strong>最长的等差子序列是任意单个元素。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>arr = [1,5,7,8,5,3,4,2,1], difference = -2
<strong>输出:</strong>4
<strong>解释:</strong>最长的等差子序列是 [7,5,3,1]。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= arr[i], difference <= 10<sup>4</sup></code></li>
</ul>