Skip to content

Latest commit

 

History

History
34 lines (29 loc) · 1.01 KB

Question_1940.md

File metadata and controls

34 lines (29 loc) · 1.01 KB

LeetCode Records - Question 1940 Longest Common Subsequence Between Sorted Arrays

Attempt 1: Use an int[] to store the current searching index of each array

class Solution {
    public List<Integer> longestCommonSubsequence(int[][] arrays) {
        int[] leftIndices = new int[arrays.length];
        List<Integer> ans = new ArrayList<>();

        for (int num : arrays[0]) {
            boolean allFound = true;
            for (int i = 1; i < arrays.length; i++) {
                while (leftIndices[i] < arrays[i].length && arrays[i][leftIndices[i]] < num) {
                    leftIndices[i]++;
                }
                if (leftIndices[i] >= arrays[i].length || arrays[i][leftIndices[i]] != num) {
                    allFound = false;
                    break;
                }
            }

            if (allFound) {
                ans.add(num);
            }
        }

        return ans;
    }
}
  • Runtime: 3 ms (Beats: 94.00%)
  • Memory: 45.32 MB (Beats: 88.00%)