leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 42b1e770bb8cebedeaf32d75ce6cd006b8bb7e3f
parent ade3ae909d5530e632695b3ce840cf1c7a022cc1
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 14 Jul 2023 14:44:00 +0200

Daily Problem

Diffstat:
AProblems/1218.cpp | 14++++++++++++++
MREADME.md | 1+
2 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/Problems/1218.cpp b/Problems/1218.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int longestSubsequence(const vector<int> &arr, int diff) { + unordered_map<int, int> dp; + + int res = 1; + for (int n : arr) { + int before = dp.count(n - diff) ? dp[n - diff] : 0; + res = max(res, dp[n] = before + 1); + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -435,6 +435,7 @@ for solving problems. | 1187 | Hard | [Make Array Strictly Increasing](Problems/1187.cpp) | | 1202 | Medium | [Smallest String With Swaps](Problems/1202.cpp) | | 1209 | Medium | [Remove All Adjacent Duplicates in String II](Problems/1209.cpp) | +| 1218 | Medium | [Longest Arithmetic Subsequence of Given Difference](Problems/1218.cpp) | | 1232 | Easy | [Check If It Is a Straight Line](Problems/1232.cpp) | | 1249 | Medium | [Minimum Remove to Make Valid Parentheses](Problems/1249.cpp) | | 1254 | Medium | [Number of Closed Islands](Problems/1254.cpp) |