{"id":5715,"date":"2019-10-05T21:30:42","date_gmt":"2019-10-06T04:30:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5715"},"modified":"2019-10-08T23:17:15","modified_gmt":"2019-10-09T06:17:15","slug":"leetcode-1218-longest-arithmetic-subsequence-of-given-difference","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1218-longest-arithmetic-subsequence-of-given-difference\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1218. Longest Arithmetic Subsequence of Given Difference"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1218. Longest Arithmetic Subsequence of Given Difference - \u5237\u9898\u627e\u5de5\u4f5c EP273\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/1THU4Aa9akQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an integer array&nbsp;<code>arr<\/code>&nbsp;and an integer&nbsp;<code>difference<\/code>, return the length of the longest subsequence in&nbsp;<code>arr<\/code>&nbsp;which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals&nbsp;<code>difference<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,3,4], difference = 1\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The longest arithmetic subsequence is [1,2,3,4].<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,3,5,7], difference = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>The longest arithmetic subsequence is any single element.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,5,7,8,5,3,4,2,1], difference = -2\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The longest arithmetic subsequence is [7,5,3,1].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= arr.length &lt;= 10^5<\/code><\/li><li><code>-10^4 &lt;= arr[i], difference &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1218-ep273.png\" alt=\"\" class=\"wp-image-5729\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1218-ep273.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1218-ep273-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/10\/1218-ep273-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i] := max length of sequence ends with x<br>dp[x] = max(0, dp[x &#8211; diff]) + 1<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int longestSubsequence(vector<int>& arr, int d) {\n    unordered_map<int, int> dp;\n    int ans = 0;\n    for (int x : arr)      \n      ans = max(ans, dp[x] = dp[x - d] + 1);    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;arr&nbsp;and an integer&nbsp;difference, return the length of the longest subsequence in&nbsp;arr&nbsp;which is an arithmetic sequence such that the difference between adjacent elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,82,229],"class_list":["post-5715","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hashtable","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5715"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5715\/revisions"}],"predecessor-version":[{"id":5733,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5715\/revisions\/5733"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}