Skip to content

← leetcode

Longest Unequal Adjacent Groups Subsequence I

· Easy

LeetCode problem 2900

You can basically just iterate through both words and groups, keeping track of the previous binary integer. While you are iterating, if previous != groups[i], you can just push words[i] to the end of ans and update previous.

code

class Solution {
public:
    vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
        vector<string> ans;
        int previous = -1;
        for (int i = 0; i < groups.size(); ++i)
        {
            if (previous != groups[i]) {
                ans.push_back(words[i]);
                previous = groups[i];
            }
        }
        return ans;
    }
};

complexity

  • Time: O(n) where n is the size of words and groups
  • Space: O(n)