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.
[codes on Leetcode]https://leetcode.com/problems/longest-unequal-adjacent-groups-subsequence-i/solutions/6745078/greedy-2-pointers-1-liner-all-beats-100/
[2-pointer/sliding window playlist]https://www.youtube.com/watch?v=pi1XpQU_Yxo&list=PLYRlUBnWnd5KrCfs7qCFb-Bvn7d
- 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]
沒有留言:
張貼留言