網頁

2025年5月15日 星期四

C++ Py3 Greedy 2 pointer速解(含1行解)Leetcode 2900 Longest Unequal Adjacent ...


C++ Py3 Greedy 2 pointer速解2900  Longest Unequal Adjacent Groups Subsequence I
C++ 會採用 2 -pointers主要的原因,就是不要額外的陣列,達到真正的O(1)空間解。
[Py3一行解請進]
---------
The main reason why C++ uses 2-pointers is to avoid extra arrays and achieve a true O(1) space solution.
class Solution:
    def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
        return [words[0]]+[ words[i+1] for i, (x, y) in enumerate(zip(groups, groups[1:])) if x!=y]

沒有留言:

張貼留言

HTML 編輯器