[Leetcode] 2900. Longest Unequal Adjacent Groups Subsequence I

whitehousechef·2025년 5월 15일

https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/description/?envType=daily-question&envId=2025-05-15

initial

an ultra easy question to cehck for alternating values of groups value (0->1->0 or 1->0 and so on). I thought of checking for index of 0 and 1 to calc seaprately and take the bigger longer value but actually the first value that is in the list is guaranteed to be the longest.

notice if we have 0,1,0,1,0 the first value (0) is our starting point, not the 1 at index =1.

sol

class Solution {
    public List<String> getLongestSubsequence(String[] words, int[] groups) {
        int check=-1;
        List<String> ans =new ArrayList<>();
        for (int i =0; i<words.length;i++){
            if(groups[i]!=check){
                check=groups[i];
                ans.add(words[i]);
            }
        }
        // String[] realAns= new String[ans.size()];
        // for(int i =0; i<ans.size();i++){
        //     realAns[i]=ans.get(i);
        // }
        return ans;
    }
}

py

n time
n space

0개의 댓글