Repository: yennanliu/CS_basics
Pull request #4: lc-java-dev-1-add-time-space-complexity
Author: login: yennanliu
Created: 2025-05-23T10:40:42Z | Merged: 2025-05-23T10:58:13Z



Diff:
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java b/leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
index d065f4d1b..4debcf22e 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
@@ -43,6 +43,7 @@ public class BeautifulArray {
     // V1
     // IDEA : Divide and Conquer
     // https://leetcode.com/problems/beautiful-array/editorial/
+    // time: O(N log N), space: O(N)
     Map<Integer, int[]> memo;
     public int[] beautifulArray_1(int N) {
         memo = new HashMap();
@@ -71,6 +72,7 @@ public int[] f(int N) {
     // V2
     // IDEA : Divide and Conquer
     // https://leetcode.com/problems/beautiful-array/solutions/1373064/java-clear-thinking-process-divide-and-conquer/
+    // time: O(N log N), space: O(N)
     public int[] beautifulArray(int n) {
         int[] ans = new int[n];
         for(int i = 0; i  < n; i++){
@@ -109,6 +111,7 @@ public void recursion(int[] arr, int left, int right){
 
     // V3-1
     // https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
+    // time: O(N log N), space: O(N)
     public int[] beautifulArray_3_1(int n) {
         int[] answer = new int[n];
         if(n == 1) {
@@ -129,6 +132,7 @@ public int[] beautifulArray_3_1(int n) {
 
     // V3-2
     // https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
+    // time: O(N log N), space: O(N)
     public int[] beautifulArray_3_2(int n) {
         ArrayList<Integer> answer=new ArrayList<>();
         answer.add(1);
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java b/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java
index fbb686f76..6c6f29f92 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/BestTimeToBuyAndSellStock.java
@@ -37,6 +37,7 @@ public class BestTimeToBuyAndSellStock {
 
     // V0
     // IDEA: 2 POINTERS
+    // time: O(N), space: O(1)
     /**
      *  IDEA: 2 POINTERS
      *  maintain local min, max, and global max
@@ -116,6 +117,7 @@ public int maxProfit(int[] prices) {
     }
 
     // V0-1
+    // time: O(N), space: O(1)
     public int maxProfit_0_1(int[] prices) {
 
         if (prices.length == 1){
@@ -144,6 +146,7 @@ public int maxProfit_0_1(int[] prices) {
     }
 
     // V0-2
+    // time: O(N), space: O(1)
     public int maxProfit_0_2(int[] prices) {
 
         if (prices.length == 0){
@@ -182,6 +185,7 @@ public int maxProfit_0_2(int[] prices) {
     }
 
     // V0-3
+    // time: O(N), space: O(1)
     public int maxProfit_0_3(int[] prices) {
 
         int minVal = (int) Math.pow(10, 4);
@@ -203,6 +207,7 @@ public int maxProfit_0_3(int[] prices) {
 
     // V1
     // https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735493/java-c-best-ever-explanation-could-possible/
+    // time: O(N), space: O(1)
     public int maxProfit_2(int[] prices) {
         int lsf = Integer.MAX_VALUE;
         int op = 0;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/CarPooling.java b/leetcode_java/src/main/java/LeetCodeJava/Array/CarPooling.java
index 70608eb19..862ba4f95 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/CarPooling.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/CarPooling.java
@@ -39,6 +39,7 @@ public class CarPooling {
 
     // V0
     // IDEA: PREFIX SUM + `prefix interval handling` (improve efficience)
+    // time: O(N + M), space: O(M)
     public boolean carPooling(int[][] trips, int capacity) {
         // edge
         if (trips == null || trips.length == 0) {
@@ -93,6 +94,7 @@ public boolean carPooling(int[][] trips, int capacity) {
 
     // V0-1
     // IDEA: PREFIX SUM
+    // time: O(N + M), space: O(M)
     /**
      *  NOTE !!!
      *
@@ -143,6 +145,7 @@ public boolean carPooling_0_1(int[][] trips, int capacity) {
 
     // V0-2
     // IDEA: PREFIX SUM (gpt)
+    // time: O(N + M), space: O(M)
     public boolean carPooling_0_2(int[][] trips, int capacity) {
         // Edge case: if there are no trips, the vehicle is never needed
         if (trips == null || trips.length == 0) {
@@ -187,6 +190,7 @@ public boolean carPooling_0_2(int[][] trips, int capacity) {
     // V1
     // https://youtu.be/08sn_w4LWEE?feature=shared
     // https://github.com/neetcode-gh/leetcode/blob/main/java%2F1094-car-pooling.java
+    // time: O(N + M), space: O(M)
     public boolean carPooling_1(int[][] trips, int capacity) {
         int[] passChange = new int[1001];
         for (int[] t : trips) {
@@ -207,6 +211,7 @@ public boolean carPooling_1(int[][] trips, int capacity) {
 
     // V2
     // https://leetcode.com/problems/car-pooling/solutions/1669644/well-explained-2-waysjava-cpythonjavascr-djso/
+    // time: O(N log N), space: O(N)
     public boolean carPooling_2(int[][] trips, int capacity) {
         // Because from and to is between 0 and 1000. Idea is to store counts in an array of size 1001.
         int lengthOfTrip[] = new int[1001];
@@ -266,6 +271,7 @@ public boolean carPooling_2(int[][] trips, int capacity) {
 
     // V3
     // https://leetcode.com/problems/car-pooling/solutions/1670309/cjavapython-donot-sort-on-95-faster-imag-da8q/
+    // time: O(N log N), space: O(N)
     public boolean carPooling_3(int[][] trips, int capacity) {
         int in_car = 0;
         int[] increase = new int[1001];
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/FindTheDuplicateNumber.java b/leetcode_java/src/main/java/LeetCodeJava/Array/FindTheDuplicateNumber.java
index fdbd1c978..28e355df5 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/FindTheDuplicateNumber.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/FindTheDuplicateNumber.java
@@ -51,6 +51,7 @@ public class FindTheDuplicateNumber {
 
     // V0
     // IDEA: HASHSET
+    // time: O(N), space: O(N)
     public int findDuplicate(int[] nums) {
         HashSet<Integer> set = new HashSet<>();
         for (int x : nums) {
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/FirstMissingPositive.java b/leetcode_java/src/main/java/LeetCodeJava/Array/FirstMissingPositive.java
index 4f653f0d0..dc901d9d7 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/FirstMissingPositive.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/FirstMissingPositive.java
@@ -46,6 +46,7 @@ public class FirstMissingPositive {
 
     // V0
     // IDEA: HASHMAP + MATH
+    // time: O(N), space: O(N)
     public int firstMissingPositive(int[] nums) {
 
         // edge
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java b/leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java
index aba6b6706..fdb7763cb 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/IncreasingTripletSubsequence.java
@@ -42,6 +42,7 @@
 public class IncreasingTripletSubsequence {
 
     // V0
+    // time: O(N), space: O(1)
     public boolean increasingTriplet(int[] nums) {
 
         // NOTE !!! we init as max integer
@@ -65,6 +66,7 @@ public boolean increasingTriplet(int[] nums) {
 
     // V0-1
     // IDEA: DP (LC 300) (fixed by gpt) (TLE)
+    // time: O(N^2), space: O(N)
     public boolean increasingTriplet_0_1(int[] nums) {
         if (nums == null || nums.length < 3) {
             return false;
@@ -91,6 +93,7 @@ public boolean increasingTriplet_0_1(int[] nums) {
 
     // V1
     // IDEA : LOOP (fixed by gpt)
+    // time: O(N), space: O(1)
     public boolean increasingTriplet_1(int[] nums) {
         if (nums.length < 3) {
             return false;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/KthLargestElementInAnArray.java b/leetcode_java/src/main/java/LeetCodeJava/Array/KthLargestElementInAnArray.java
index c63d8b48d..a99b43ba5 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/KthLargestElementInAnArray.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/KthLargestElementInAnArray.java
@@ -8,6 +8,7 @@ public class KthLargestElementInAnArray {
 
     // V0
     // IDEA : PQ (priority queue)
+    // time: O(N log N), space: O(N)
     public int findKthLargest(int[] nums, int k) {
         if (nums.length == 1){
             if (k == 1){
@@ -34,6 +35,7 @@ public int findKthLargest(int[] nums, int k) {
 
     // V0
     // IDEA : ARRAY + SORTING
+    // time: O(N log N), space: O(N)
     public int findKthLargest_0_1(int[] nums, int k) {
 
         if (nums.length == 1 && k == 1){
@@ -61,6 +63,7 @@ public int findKthLargest_0_1(int[] nums, int k) {
     // V1
     // IDEA : SORT
     // https://leetcode.com/problems/kth-largest-element-in-an-array/editorial/
+    // time: O(N log N), space: O(1)
     public int findKthLargest_2(int[] nums, int k) {
         Arrays.sort(nums);
         // Can't sort int[] in descending order in Java;
@@ -71,6 +74,7 @@ public int findKthLargest_2(int[] nums, int k) {
     // V2
     // IDEA : Min-Heap
     // https://leetcode.com/problems/kth-largest-element-in-an-array/editorial/
+    // time: O(N log K), space: O(K)
     public int findKthLargest_3(int[] nums, int k) {
         PriorityQueue<Integer> heap = new PriorityQueue<>();
         for (int num: nums) {
@@ -86,6 +90,7 @@ public int findKthLargest_3(int[] nums, int k) {
     // V3
     // IDEA : Quickselect
     // https://leetcode.com/problems/kth-largest-element-in-an-array/editorial/
+    // time: O(N) average, O(N^2) worst, space: O(N)
     public int findKthLargest_4(int[] nums, int k) {
         List<Integer> list = new ArrayList<>();
         for (int num: nums) {
@@ -127,6 +132,7 @@ public int quickSelect(List<Integer> nums, int k) {
     // V4
     // IDEA : Counting Sort
     // https://leetcode.com/problems/kth-largest-element-in-an-array/editorial/
+    // time: O(N + R), space: O(R)
     public int findKthLargest_5(int[] nums, int k) {
         int minValue = Integer.MAX_VALUE;
         int maxValue = Integer.MIN_VALUE;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/LongestArithmeticSubsequence.java b/leetcode_java/src/main/java/LeetCodeJava/Array/LongestArithmeticSubsequence.java
index 5dab26636..4a84da98e 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/LongestArithmeticSubsequence.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/LongestArithmeticSubsequence.java
@@ -56,6 +56,7 @@ public class LongestArithmeticSubsequence {
 
     // V2
     // https://leetcode.com/problems/longest-arithmetic-subsequence/solutions/3671662/beats-100-c-java-python-beginner-friendl-c1gm/
+    // time: O(N^2), space: O(N^2)
     public int longestArithSeqLength_2(int[] nums) {
         int n = nums.length;
         if (n <= 2)
@@ -79,6 +80,7 @@ public int longestArithSeqLength_2(int[] nums) {
     // V3
     // https://leetcode.com/problems/longest-arithmetic-subsequence/solutions/595478/java-dp-solution-using-hashmap-on2-with-92d0r/
     // IDEA: DP
+    // time: O(N^2), space: O(N^2)
     public int longestArithSeqLength_3(int[] A) {
         int ansMax = 0, n = A.length, was, diff;
         HashMap<Integer, Integer> tillI[] = new HashMap[n];
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/LongestContinuousIncreasingSubsequence.java b/leetcode_java/src/main/java/LeetCodeJava/Array/LongestContinuousIncreasingSubsequence.java
index 639889906..a1996f8f7 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/LongestContinuousIncreasingSubsequence.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/LongestContinuousIncreasingSubsequence.java
@@ -11,6 +11,7 @@ public class LongestContinuousIncreasingSubsequence {
 
     // V1
     // https://leetcode.com/problems/longest-continuous-increasing-subsequence/solutions/2565636/clean-0ms-java-solution/
+    // time: O(N), space: O(1)
     public int findLengthOfLCIS_1(int[] nums)
     {
         int max =0;
@@ -31,6 +32,7 @@ public int findLengthOfLCIS_1(int[] nums)
 
     // V2
     // https://leetcode.com/problems/longest-continuous-increasing-subsequence/solutions/3426987/solution/
+    // time: O(N), space: O(1)
     public int findLengthOfLCIS_2(int[] nums) {
         int maxCount = 1;
         int currentCount = 1;
@@ -60,6 +62,7 @@ public int findLengthOfLCIS_2(int[] nums) {
 
     // V3
     // https://leetcode.com/problems/longest-continuous-increasing-subsequence/solutions/3118835/simple-java-solution-using-2-pointer-approach-time-beats-100-of-the-solutions/
+    // time: O(N), space: O(1)
     public int findLengthOfLCIS_3(int[] nums)
     {
         int maxCount = 1;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java
index ec19da5da..247231b2e 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java
@@ -7,6 +7,7 @@
 public class MajorityElement {
 
     // V0
+    // time: O(N), space: O(N)
     public int majorityElement(int[] nums) {
 
         if (nums.length == 1){
@@ -35,6 +36,7 @@ public int majorityElement(int[] nums) {
 
     // V1
     // https://leetcode.com/problems/majority-element/solutions/3407133/o-1-java-solution-ternary-operators-in-linear-time-beats-100/
+    // time: O(N), space: O(1)
     public int majorityElement_1(int[] nums) {
         int majority_index = 0;
         int count = 1;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement2.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement2.java
index b846893be..88d02b92d 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement2.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement2.java
@@ -46,6 +46,7 @@ public class MajorityElement2 {
 
     // V0
     // IDEA: HASHMAP
+    // time: O(N), space: O(N)
     public List<Integer> majorityElement(int[] nums) {
         List<Integer> res = new ArrayList<>();
         // edge
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MedianOfTwoSortedArrays.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MedianOfTwoSortedArrays.java
index 30b7e0bd2..0eb1f9e0c 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/MedianOfTwoSortedArrays.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MedianOfTwoSortedArrays.java
@@ -47,6 +47,7 @@ public class MedianOfTwoSortedArrays {
 
     // V0-1
     // IDEA: BRUTE FORCE (sort + even/odd size median number)
+    // time: O((M+N) log(M+N)), space: O(M+N)
     public double findMedianSortedArrays_0_1(int[] nums1, int[] nums2) {
         // edge
         if (nums1.length == 0 && nums2.length == 0) {
@@ -110,6 +111,7 @@ public double getMedian(int[] num) {
     // V1-1
     // https://neetcode.io/problems/median-of-two-sorted-arrays
     // IDEA: BRUTE FORCE
+    // time: O((M+N) log(M+N)), space: O(M+N)
     public double findMedianSortedArrays_1_1(int[] nums1, int[] nums2) {
         int len1 = nums1.length, len2 = nums2.length;
         int[] merged = new int[len1 + len2];
@@ -128,6 +130,7 @@ public double findMedianSortedArrays_1_1(int[] nums1, int[] nums2) {
     // V1-2
     // https://neetcode.io/problems/median-of-two-sorted-arrays
     // IDEA: 2 POINTERS
+    // time: O(M+N), space: O(1)
     public double findMedianSortedArrays_1_2(int[] nums1, int[] nums2) {
         int len1 = nums1.length, len2 = nums2.length;
         int i = 0, j = 0;
@@ -162,6 +165,7 @@ public double findMedianSortedArrays_1_2(int[] nums1, int[] nums2) {
     // V1-3
     // https://neetcode.io/problems/median-of-two-sorted-arrays
     // IDEA: Binary Search
+    // time: O(log(min(M,N))), space: O(log(min(M,N)))
     public double findMedianSortedArrays_1_3(int[] nums1, int[] nums2) {
         int left = (nums1.length + nums2.length + 1) / 2;
         int right = (nums1.length + nums2.length + 2) / 2;
@@ -193,6 +197,7 @@ public int getKth(int[] a, int m, int[] b, int n, int k, int aStart, int bStart)
     // V1-4
     // https://neetcode.io/problems/median-of-two-sorted-arrays
     // IDEA: Binary Search (Optimal)
+    // time: O(log(min(M,N))), space: O(1)
     public double findMedianSortedArrays_1_4(int[] nums1, int[] nums2) {
         int[] A = nums1;
         int[] B = nums2;
@@ -238,6 +243,7 @@ public double findMedianSortedArrays_1_4(int[] nums1, int[] nums2) {
     // https://leetcode.com/problems/median-of-two-sorted-arrays/solutions/6401742/1ms-100-beats-single-loop-beginner-frien-6bqd/
 
     // V4
+    // time: O(log(min(M,N))), space: O(1)
     public double findMedianSortedArrays_4(int[] nums1, int[] nums2) {
         if (nums1.length > nums2.length) {
             return findMedianSortedArrays_4(nums2, nums1); // Ensure nums1 is the smaller array
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java
index 70ab8f26c..5a75993c0 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MergeIntervals.java
@@ -36,6 +36,7 @@ public class MergeIntervals {
 
     // V0
     // IDEA: LC 57 + `interval op`
+    // time: O(N log N), space: O(N)
     public int[][] merge(int[][] intervals) {
 
         if (intervals == null || intervals.length == 0) {
@@ -115,6 +116,7 @@ public int[][] merge(int[][] intervals) {
      */
     // V0-0-1
     // IDEA : ARRAY OP + BOUNDARY OP
+    // time: O(N log N), space: O(N)
     public int[][] merge_0_0_1(int[][] intervals) {
         /**
          *
@@ -155,6 +157,7 @@ public int[][] merge_0_0_1(int[][] intervals) {
 
     // V0-1
     // IDEA: SORTING + BOUNDARY OP
+    // time: O(N log N), space: O(N)
     public int[][] merge_0_1(int[][] intervals) {
         // edge
         if(intervals == null || intervals.length == 0){
@@ -211,6 +214,7 @@ public int compare(int[] o1, int[] o2) {
 
     // V0-2
     // IDEA: SORT + ARRAY OP + BOUNDARY HANDLING
+    // time: O(N log N), space: O(N)
     public int[][] merge_0_2(int[][] intervals) {
 
         if (intervals.length <= 1){
@@ -247,6 +251,7 @@ public int[][] merge_0_2(int[][] intervals) {
 
     // V0-3
     // IDEA: ARRAY OP (GPT)
+    // time: O(N log N), space: O(N)
     public int[][] merge_0_3(int[][] intervals) {
         // Edge case: If intervals is null or empty, return an empty array
         if (intervals == null || intervals.length == 0) {
@@ -288,6 +293,7 @@ public int compare(int[] o1, int[] o2) {
     // V1-1
     // https://neetcode.io/problems/merge-intervals
     // IDEA: SORTING
+    // time: O(N log N), space: O(N)
     public int[][] merge_1_1(int[][] intervals) {
         Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
         List<int[]> output = new ArrayList<>();
@@ -311,6 +317,7 @@ public int[][] merge_1_1(int[][] intervals) {
     // V1-2
     // https://neetcode.io/problems/merge-intervals
     // IDEA:  Sweep Line Algorithm
+    // time: O(N log N), space: O(N)
     public int[][] merge_1_2(int[][] intervals) {
         TreeMap<Integer, Integer> map = new TreeMap<>();
 
@@ -339,6 +346,7 @@ public int[][] merge_1_2(int[][] intervals) {
     // V1-3
     // https://neetcode.io/problems/merge-intervals
     // IDEA: GREEDY
+    // time: O(N log N), space: O(N)
     public int[][] merge_1_3(int[][] intervals) {
         int max = 0;
         for (int i = 0; i < intervals.length; i++) {
@@ -386,6 +394,7 @@ public int[][] merge_1_3(int[][] intervals) {
     // V2
     // IDEA : Sorting
     // https://leetcode.com/problems/merge-intervals/editorial/
+    // time: O(N log N), space: O(N)
     public int[][] merge_2(int[][] intervals) {
         Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
         // NOTE !!! we set res as linkedlist type
@@ -408,6 +417,7 @@ public int[][] merge_2(int[][] intervals) {
     // V3
     // IDEA : Connected Components
     // https://leetcode.com/problems/merge-intervals/editorial/
+    // time: O(N^2), space: O(N)
     private Map<int[], List<int[]>> graph;
     private Map<Integer, List<int[]>> nodesInComp;
     private Set<int[]> visited;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/MinimumSizeSubarraySum.java b/leetcode_java/src/main/java/LeetCodeJava/Array/MinimumSizeSubarraySum.java
index fd09a67ba..31c77e9c0 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/MinimumSizeSubarraySum.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/MinimumSizeSubarraySum.java
@@ -40,6 +40,7 @@ public class MinimumSizeSubarraySum {
 
     // V0
     // IDEA: SLIDING WINDOW (fix by gpt)
+    // time: O(N), space: O(1)
     public int minSubArrayLen(int target, int[] nums) {
         // Check for empty input array
         if (nums == null || nums.length == 0) {
@@ -86,6 +87,7 @@ public int minSubArrayLen(int target, int[] nums) {
     // V1
     // https://leetcode.com/problems/minimum-size-subarray-sum/editorial/
     // IDEA: SLIDING WINDOW
+    // time: O(N), space: O(1)
     public int minSubArrayLen_1(int target, int[] nums) {
         int left = 0, right = 0, sumOfCurrentWindow = 0;
         int res = Integer.MAX_VALUE;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/PlusOne.java b/leetcode_java/src/main/java/LeetCodeJava/Array/PlusOne.java
index 380c255ba..d0bfb1233 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/PlusOne.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/PlusOne.java
@@ -56,6 +56,7 @@ public class PlusOne {
 
     // V0
     // IDEA: reverse loop, `plus one` at iex=len-1, reverse, assign val to array
+    // time: O(N), space: O(N)
     public int[] plusOne(int[] digits) {
         // edge
         if (digits == null || digits.length == 0) {
@@ -97,6 +98,7 @@ public int[] plusOne(int[] digits) {
 
     // V0-1
     // IDEA: MATH ( add per digit, then transform result) (fixed by gpt)
+    // time: O(N), space: O(1)
     public int[] plusOne_0_1(int[] digits) {
         if (digits == null || digits.length == 0) {
             return new int[] { 1 };
@@ -149,6 +151,7 @@ public int[] plusOne_0_1(int[] digits) {
     // V1-1
     // https://neetcode.io/problems/plus-one
     // IDEA:  RECURSION
+    // time: O(N), space: O(N)
     public int[] plusOne_1_1(int[] digits) {
         if (digits.length == 0)
             return new int[]{1};
@@ -170,6 +173,7 @@ public int[] plusOne_1_1(int[] digits) {
     // V1-2
     // https://neetcode.io/problems/plus-one
     // IDEA: ITERATION - I
+    // time: O(N), space: O(1)
     public int[] plusOne_1_2(int[] digits) {
         int one = 1;
         int i = 0;
@@ -197,6 +201,7 @@ public int[] plusOne_1_2(int[] digits) {
     // V1-3
     // https://neetcode.io/problems/plus-one
     // IDEA: ITERATION - II
+    // time: O(N), space: O(1)
     public int[] plusOne_1_3(int[] digits) {
         int n = digits.length;
         for (int i = n - 1; i >= 0; i--) {
@@ -214,6 +219,7 @@ public int[] plusOne_1_3(int[] digits) {
 
     // V2
     // https://leetcode.com/problems/plus-one/editorial/
+    // time: O(N), space: O(1)
     public int[] plusOne_2(int[] digits) {
         int n = digits.length;
 
@@ -239,6 +245,7 @@ public int[] plusOne_2(int[] digits) {
 
     // V3
     // https://leetcode.com/problems/plus-one/solutions/2706861/java-fastest-0ms-runtime-easy-and-elegant-solution/
+    // time: O(N), space: O(1)
     public int[] plusOne_3(int[] digits){
         for (int i = digits.length - 1; i >= 0; i--) {
             if (digits[i] < 9) {
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/ProductOfArrayExceptSelf.java b/leetcode_java/src/main/java/LeetCodeJava/Array/ProductOfArrayExceptSelf.java
index 0cd62643e..2454dc59d 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/ProductOfArrayExceptSelf.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/ProductOfArrayExceptSelf.java
@@ -41,6 +41,7 @@ public class ProductOfArrayExceptSelf {
 
     // VO
     // IDEA : ARRAY PRODUCT
+    // time: O(N), space: O(N)
     public int[] productExceptSelf(int[] nums) {
 
         if (nums.length == 0 || nums.equals(null)){
@@ -82,6 +83,7 @@ private int getProduct(int[] nums) {
 
     // V0-1
     // LC 238
+    // time: O(N), space: O(1)
     public int[] productExceptSelf_0_1(int[] nums) {
 
         // edge
@@ -121,6 +123,7 @@ private int getProductExceptIdx(int[] nums, int idx){
     // V1
     // IDEA : Left and Right product lists
     // https://leetcode.com/problems/product-of-array-except-self/editorial/
+    // time: O(N), space: O(N)
     public int[] productExceptSelf_2(int[] nums) {
 
         // The length of the input array
@@ -171,6 +174,7 @@ public int[] productExceptSelf_2(int[] nums) {
     // V2
     // IDEA : O(1) space approach
     // https://leetcode.com/problems/product-of-array-except-self/editorial/
+    // time: O(N), space: O(1)
     public int[] productExceptSelf_3(int[] nums) {
 
         // The length of the input array
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RegularExpressionMatching.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RegularExpressionMatching.java
index a806c504f..7cfbef108 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/RegularExpressionMatching.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RegularExpressionMatching.java
@@ -51,6 +51,7 @@ public class RegularExpressionMatching {
 
     // V0-1
     // IDEA: 2D DP (gpt)
+    // time: O(M*N), space: O(M*N)
     public boolean isMatch_0_1(String s, String p) {
         int m = s.length();
         int n = p.length();
@@ -95,6 +96,7 @@ public boolean isMatch_0_1(String s, String p) {
     // V1-1
     // https://neetcode.io/problems/regular-expression-matching
     // IDEA: RECURSION
+    // time: O(2^(M+N)), space: O(M+N)
     public boolean isMatch_1_1(String s, String p) {
         int m = s.length(), n = p.length();
         return dfs(0, 0, s, p, m, n);
@@ -120,6 +122,7 @@ private boolean dfs(int i, int j, String s, String p, int m, int n) {
     // V1-2
     // https://neetcode.io/problems/regular-expression-matching
     // IDEA: DP (TOP DOWN)
+    // time: O(M*N), space: O(M*N)
     private Boolean[][] dp;
 
     public boolean isMatch_1_2(String s, String p) {
@@ -151,6 +154,7 @@ private boolean dfs_1_2(int i, int j, String s, String p, int m, int n) {
     // V1-3
     // https://neetcode.io/problems/regular-expression-matching
     // IDEA: DP (BOTTOM UP)
+    // time: O(M*N), space: O(M*N)
     public boolean isMatch_1_3(String s, String p) {
         int m = s.length(), n = p.length();
         boolean[][] dp = new boolean[m + 1][n + 1];
@@ -179,6 +183,7 @@ public boolean isMatch_1_3(String s, String p) {
     // V1-4
     // https://neetcode.io/problems/regular-expression-matching
     // IDEA: DP (SPACE OPTIMIZED)
+    // time: O(M*N), space: O(N)
     public boolean isMatch_1_4(String s, String p) {
         boolean[] dp = new boolean[p.length() + 1];
         dp[p.length()] = true;
@@ -212,6 +217,7 @@ public boolean isMatch_1_4(String s, String p) {
     // V1-5
     // https://neetcode.io/problems/regular-expression-matching
     // IDEA: DP (OPTIMAL)
+    // time: O(M*N), space: O(M*N)
     public boolean isMatch_1_5(String s, String p) {
         boolean[] dp = new boolean[p.length() + 1];
         dp[p.length()] = true;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java
index 851eb8246..1ede88fd5 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveDuplicatesFromSortedArray.java
@@ -59,6 +59,7 @@ public class RemoveDuplicatesFromSortedArray {
 
     // V0
     // IDEA : 2 POINTERS
+    // time: O(N), space: O(1)
     /**
      *  //--------------------------------
      *  Example 1
@@ -142,6 +143,7 @@ public int removeDuplicates(int[] nums) {
 
     // V1
     // https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3416595/c-java-python-javascript-fully-explained-two-pointer/
+    // time: O(N), space: O(1)
     public int removeDuplicates_1(int[] nums) {
         int i = 0;
         for (int j = 1; j < nums.length; j++) {
@@ -155,6 +157,7 @@ public int removeDuplicates_1(int[] nums) {
 
     // V2
     // https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3342081/100-faster-java-2-approaches-step-by-step-explained/
+    // time: O(N), space: O(1)
     public int removeDuplicates_2(int[] nums) {
 
         int newIndex = 1; // Start with index 1 because the first element is already in place
@@ -172,6 +175,7 @@ public int removeDuplicates_2(int[] nums) {
 
     // V2'
     // https://leetcode.com/problems/remove-duplicates-from-sorted-array/solutions/3342081/100-faster-java-2-approaches-step-by-step-explained/
+    // time: O(N), space: O(N)
     public int removeDuplicates_3(int[] nums) {
 
         //Insert all array element in the Set.
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveElement.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveElement.java
index bf833eabd..1005e4675 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveElement.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RemoveElement.java
@@ -61,6 +61,7 @@ class RemoveElement {
 
     // V0
     // IDEA : 2 POINTERS
+    // time: O(N), space: O(1)
     /**
      *  //--------------------
      *  Example 1
@@ -138,6 +139,7 @@ public int removeElement(int[] nums, int val) {
     // V1
     // IDEA : 2 POINTERS
     // https://leetcode.com/problems/remove-element/solutions/3670940/best-100-c-java-python-beginner-friendly/
+    // time: O(N), space: O(1)
     public int removeElement_1(int[] nums, int val) {
         int index = 0;
         for (int i = 0; i < nums.length; i++) {
@@ -152,6 +154,7 @@ public int removeElement_1(int[] nums, int val) {
     // V2
     // IDEA : 2 POINTERS
     // https://leetcode.com/problems/remove-element/solutions/3102906/java-best-solution-o-n-time-complexity/
+    // time: O(N), space: O(1)
     public int removeElement_2(int[] nums, int val) {
         int i = 0;
         for (int j = 0; j < nums.length; j++) {
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RotateArray.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RotateArray.java
index 45a35d33c..31eb2cdad 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/RotateArray.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RotateArray.java
@@ -53,6 +53,7 @@ public class RotateArray {
 
     // V0
     // IDEA: DEQEUE
+    // time: O(N + K), space: O(N)
     public void rotate(int[] nums, int k) {
         // edge
         if (nums == null || nums.length == 0) {
@@ -86,6 +87,7 @@ public void rotate(int[] nums, int k) {
 
     // V0-1
     // IDEA: ARRAY OP (TLE)
+    // time: O(N*K), space: O(N)
     public void rotate_0_1(int[] nums, int k) {
         // edge
         if (nums == null || nums.length == 0) {
@@ -112,6 +114,7 @@ public void rotate_0_1(int[] nums, int k) {
 
     // V1
     // IDEA: rotate ( `%` op)  (gpt)
+    // time: O(N), space: O(1)
     public void rotate_1(int[] nums, int k) {
         // Edge case
         if (nums == null || nums.length == 0) {
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java b/leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java
index 6ba8d788c..af5642db3 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/RotateImage.java
@@ -65,6 +65,7 @@ public class RotateImage {
 
     // V0
     // IDEA : ARRAY OP
+    // time: O(N^2), space: O(1)
     public void rotate(int[][] matrix){
 
         int len = matrix.length;
@@ -146,6 +147,7 @@ private void _reverse(int[] input){
     // V1-1
     // https://neetcode.io/problems/rotate-matrix
     // IDEA: BRUTE FORCE
+    // time: O(N^2), space: O(N^2)
     public void rotate_1_1(int[][] matrix) {
         int n = matrix.length;
         int[][] rotated = new int[n][n];
@@ -166,6 +168,7 @@ public void rotate_1_1(int[][] matrix) {
     // V1-2
     // https://neetcode.io/problems/rotate-matrix
     // IDEA: Rotate By Four Cells
+    // time: O(N^2), space: O(1)
     public void rotate_1_2(int[][] matrix) {
         int l = 0;
         int r = matrix.length - 1;
@@ -198,6 +201,7 @@ public void rotate_1_2(int[][] matrix) {
     // V1-3
     // https://neetcode.io/problems/rotate-matrix
     // IDEA: Reverse And Transpose
+    // time: O(N^2), space: O(1)
     public void rotate_1_3(int[][] matrix) {
         // Reverse the matrix vertically
         reverse(matrix);
@@ -225,6 +229,7 @@ private void reverse(int[][] matrix) {
     // V2-1
     // IDEA : Rotate Groups of Four Cells
     // https://leetcode.com/problems/rotate-image/editorial/
+    // time: O(N^2), space: O(1)
     public void rotate_2_1(int[][] matrix) {
         int n = matrix.length;
         for (int i = 0; i < (n + 1) / 2; i ++) {
@@ -241,6 +246,7 @@ public void rotate_2_1(int[][] matrix) {
     // V2-2
     // IDEA : Reverse on the Diagonal and then Reverse Left to Right
     // https://leetcode.com/problems/rotate-image/editorial/
+    // time: O(N^2), space: O(1)
     public void rotate_2_2(int[][] matrix) {
         transpose(matrix);
         reflect(matrix);
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.java b/leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.java
index 706b04c15..1a50a58a3 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/SetMatrixZeroes.java
@@ -48,6 +48,7 @@ public class SetMatrixZeroes {
     // V0
     // IDEA : ARRAY OP
     // https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/set-matrix-zeroes.py
+    // time: O(M*N), space: O(M*N)
     public void setZeroes(int[][] matrix) {
 
         if (matrix.length == 0 && matrix[0].length == 0){
@@ -89,6 +90,7 @@ public void setZeroes(int[][] matrix) {
     }
 
     // V1
+    // time: O(M*N), space: O(1)
     public void setZeroes_1(int[][] matrix) {
         boolean fr = false,fc = false;
         for(int i = 0; i < matrix.length; i++) {
@@ -121,6 +123,7 @@ public void setZeroes_1(int[][] matrix) {
 
     // V2
     // https://leetcode.com/problems/set-matrix-zeroes/solutions/4800467/using-2-d-boolean-array/
+    // time: O(M*N^2), space: O(M*N)
     public void setZeroes_2(int[][] matrix) {
         int rows = matrix.length;
         int cols = matrix[0].length;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/ShortestWordDistance.java b/leetcode_java/src/main/java/LeetCodeJava/Array/ShortestWordDistance.java
index 387a68da2..df4c97309 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/ShortestWordDistance.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/ShortestWordDistance.java
@@ -8,6 +8,7 @@
 public class ShortestWordDistance {
 
     // V0
+    // time: O(N + M*K), space: O(M+K)
     public int shortestDistance(String[] wordsDict, String word1, String word2) {
 
         List<Integer> list1 =  new ArrayList<>();
@@ -37,6 +38,7 @@ public int shortestDistance(String[] wordsDict, String word1, String word2) {
 
     // V1
     // https://leetcode.com/problems/shortest-word-distance/solutions/66931/ac-java-clean-solution/
+    // time: O(N), space: O(1)
     public int shortestDistance_1(String[] words, String word1, String word2) {
         int p1 = -1, p2 = -1, min = Integer.MAX_VALUE;
 
@@ -56,6 +58,7 @@ public int shortestDistance_1(String[] words, String word1, String word2) {
 
     // V2
     // https://leetcode.com/problems/shortest-word-distance/solutions/3458164/java-easy-to-understand-solution/
+    // time: O(N^2), space: O(1)
     public int shortestDistance_2(String[] wordsDict, String word1, String word2) {
         int minDistance = Integer.MAX_VALUE;
 
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java b/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java
index f6adbdebb..8848e0f3f 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/SpiralMatrix.java
@@ -48,6 +48,7 @@ public class SpiralMatrix {
 
     // V0-1
     // IDEA : array + index op
+    // time: O(M*N), space: O(1)
     public List<Integer> spiralOrder_0_1(int[][] matrix) {
 
         int row = matrix.length;
@@ -90,6 +91,7 @@ public List<Integer> spiralOrder_0_1(int[][] matrix) {
 
     // V1
     // https://leetcode.com/problems/spiral-matrix/solutions/4700215/easy-solution/
+    // time: O(M*N), space: O(1)
     public List<Integer> spiralOrder_1(int[][] m) {
         int l=0,r=m[0].length-1,u=0,d=m.length-1;
         List<Integer> ll=new ArrayList<>();
@@ -124,6 +126,7 @@ public List<Integer> spiralOrder_1(int[][] m) {
 
     // V2
     // https://leetcode.com/problems/spiral-matrix/solutions/3503095/java-runtime-0-ms-beats-100-memory-40-8-mb-beats-46-17/
+    // time: O(M*N), space: O(1)
     public List<Integer> spiralOrder_2(int[][] matrix) {
         int row = matrix.length;
         List<Integer> ans = new ArrayList<>();
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/SummaryRanges.java b/leetcode_java/src/main/java/LeetCodeJava/Array/SummaryRanges.java
index a364d555e..8e8b9f998 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/SummaryRanges.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/SummaryRanges.java
@@ -11,6 +11,7 @@ public class SummaryRanges {
 
     // V1
     // https://leetcode.com/problems/summary-ranges/solutions/1805391/concise-solution-in-0-n-with-approach-explained-in-detail/
+    // time: O(N), space: O(1)
     public List<String> summaryRanges(int[] nums) {
         ArrayList<String> al = new ArrayList<>();
 
@@ -32,6 +33,7 @@ public List<String> summaryRanges(int[] nums) {
 
     // V2
     // https://leetcode.com/problems/summary-ranges/solutions/2813052/java-100-faster-solution/
+    // time: O(N), space: O(1)
     public List<String> summaryRanges_2(int[] nums) {
         List<String> list = new ArrayList<>();
         if (nums.length == 0) return list;
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/ThreeSum.java b/leetcode_java/src/main/java/LeetCodeJava/Array/ThreeSum.java
index ed10f7ca8..9d947a48a 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/ThreeSum.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/ThreeSum.java
@@ -11,6 +11,7 @@ public class ThreeSum {
     // V1
     // IDEA : Two Pointers
     // https://leetcode.com/problems/3sum/editorial/
+    // time: O(N^2), space: O(1)
     public List<List<Integer>> threeSum(int[] nums) {
         Arrays.sort(nums);
         List<List<Integer>> res = new ArrayList<>();
@@ -38,6 +39,7 @@ void twoSumII(int[] nums, int i, List<List<Integer>> res) {
 
     // V2
     // IDEA : 2 POINTERS (GPT)
+    // time: O(N^2), space: O(1)
     public List<List<Integer>> threeSum_2(int[] nums) {
         List<List<Integer>> res = new ArrayList<>();
 
@@ -85,6 +87,7 @@ public List<List<Integer>> threeSum_2(int[] nums) {
     // V3
     // IDEA: HASH SET
     // https://leetcode.com/problems/3sum/editorial/
+    // time: O(N^2), space: O(N)
     public List<List<Integer>> threeSum_3(int[] nums) {
         Arrays.sort(nums);
         List<List<Integer>> res = new ArrayList<>();
@@ -110,6 +113,7 @@ void twoSum(int[] nums, int i, List<List<Integer>> res) {
     // V4
     // IDEA : "No-Sort"
     // https://leetcode.com/problems/3sum/editorial/
+    // time: O(N^2), space: O(N)
     public List<List<Integer>> threeSum_4(int[] nums) {
         Set<List<Integer>> res = new HashSet<>();
         Set<Integer> dups = new HashSet<>();
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/ValidSudoku.java b/leetcode_java/src/main/java/LeetCodeJava/Array/ValidSudoku.java
index 79ea77cf1..3b86de57e 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/ValidSudoku.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/ValidSudoku.java
@@ -68,6 +68,7 @@ public class ValidSudoku {
 
     // V0
     // IDEA: SET, MATRIX OP (fixed by GPT)
+    // time: O(1), space: O(1)
     public boolean isValidSudoku(char[][] board) {
         // edge case: if board is empty, return true (no validation needed)
         if (board.length == 0 || board[0].length == 0) {
@@ -128,6 +129,7 @@ public boolean isValidSudoku(char[][] board) {
     // V0-1
     // IDEA : SET
     // https://github.com/neetcode-gh/leetcode/blob/main/java/0036-valid-sudoku.java
+    // time: O(1), space: O(1)
     public boolean isValidSudoku_0_1(char[][] board) {
         //neetcode solution, slightly modified
 
@@ -202,6 +204,7 @@ public boolean checkBlock(int idxI, int idxJ, char[][] board) {
     // V1
     // IDEA : HASH SET
     // https://leetcode.com/problems/valid-sudoku/editorial/
+    // time: O(1), space: O(1)
     public boolean isValidSudoku_1(char[][] board) {
         int N = 9;
 
@@ -251,6 +254,7 @@ public boolean isValidSudoku_1(char[][] board) {
     // V2
     // IDEA : Array of Fixed Length
     // https://leetcode.com/problems/valid-sudoku/editorial/
+    // time: O(1), space: O(1)
     public boolean isValidSudoku_2(char[][] board) {
         int N = 9;
 
@@ -293,6 +297,7 @@ public boolean isValidSudoku_2(char[][] board) {
     // V3
     // IDEA : Bitmasking
     // https://leetcode.com/problems/valid-sudoku/editorial/
+    // time: O(1), space: O(1)
     public boolean isValidSudoku_3(char[][] board) {
         int N = 9;
 
diff --git a/leetcode_java/src/main/java/LeetCodeJava/Array/maxSubarraySumCircular.java b/leetcode_java/src/main/java/LeetCodeJava/Array/maxSubarraySumCircular.java
index 6502390d5..a3577aab9 100644
--- a/leetcode_java/src/main/java/LeetCodeJava/Array/maxSubarraySumCircular.java
+++ b/leetcode_java/src/main/java/LeetCodeJava/Array/maxSubarraySumCircular.java
@@ -104,6 +104,7 @@ public class maxSubarraySumCircular {
 
     // V0-1
     // IDEA: (GEMINI)
+    // time: O(N), space: O(1)
     /**
      *
      * Explanation of Changes:
@@ -172,6 +173,7 @@ public int maxSubarraySumCircular_0_1(int[] nums) {
 
     // V0-2
     // IDEA: Kadane algo (gpt)
+    // time: O(N), space: O(1)
     public int maxSubarraySumCircular(int[] nums) {
         int totalSum = 0;
         int maxSum = nums[0], curMax = 0;
@@ -196,6 +198,7 @@ public int maxSubarraySumCircular(int[] nums) {
 
     // V0-2
     // IDEA: DEQUEUE + PREFIX SUM (gpt)
+    // time: O(N), space: O(N)
     public int maxSubarraySumCircular_0_2(int[] nums) {
         int n = nums.length;
         int[] prefix = new int[2 * n + 1];
@@ -232,8 +235,7 @@ public int maxSubarraySumCircular_0_2(int[] nums) {
 
 
     // V1
-    // https://www.youtube.com/watch?v=fxT9KjakYPM
-    // https://github.com/neetcode-gh/leetcode/blob/main/java%2F0918-maximum-sum-circular-subarray.java
+    // time: O(N), space: O(1)
     public int maxSubarraySumCircular_1(int[] nums) {
         int curMax = 0, curMin = 0;
         int globMax = nums[0], globMin = nums[0];
@@ -249,6 +251,7 @@ public int maxSubarraySumCircular_1(int[] nums) {
     }
 
     // V2-1
+    // time: O(N), space: O(1)
     // https://leetcode.com/problems/maximum-sum-circular-subarray/editorial/
     // IDEA: Enumerate prefix and suffix sums
     public int maxSubarraySumCircular_2_1(int[] nums) {
@@ -280,6 +283,7 @@ public int maxSubarraySumCircular_2_1(int[] nums) {
     }
 
     // V2-2
+    // time: O(N), space: O(1)
     // https://leetcode.com/problems/maximum-sum-circular-subarray/editorial/
     // IDEA: Calculate the "Minimum Subarray"
     public int maxSubarraySumCircular_2_2(int[] nums) {


Reviews:
user: login: gemini-code-assist[bot]
body: Hello @yennanliu, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

## Summary of Changes
Hello team, gemini-code-assist here to provide a summary of this pull request. Based on the title and the changes, this PR aims to enhance the documentation of the `BeautifulArray.java` LeetCode solution by explicitly adding comments detailing the time and space complexity for each implemented approach (V1, V2, V3-1, and V3-2). This helps future readers and contributors quickly understand the performance characteristics of these solutions.

### Highlights
* **Documentation Improvement**: Adds comments to document the time and space complexity of existing solutions for the Beautiful Array problem.
* **Complexity Analysis**: Specifies O(N log N) time complexity and O(N) space complexity for the various divide and conquer approaches implemented.

### Changelog
* **leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java**
  * Added time and space complexity comments for V1 (Divide and Conquer) solution.
  * Added time and space complexity comments for V2 (Divide and Conquer) solution.
  * Added time and space complexity comments for V3-1 solution.
  * Added time and space complexity comments for V3-2 solution.


<details>

<summary><b>Using Gemini Code Assist</b></summary>
<br>

The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.

<b>Invoking Gemini</b>

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands.

Feature | Command | Description
--- | --- | ---
Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments.
Help | `/gemini help` | Displays a list of available commands.

<b>Customization</b>

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).

<b>Limitations & Feedback</b>

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments to provide feedback.

</details>

<b>You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the [Gemini Code Assist IDE Extension](https://cloud.google.com/products/gemini/code-assist).</b>


[^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/review-github-code). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).

---

> Code runs fast or slow,
> Complexity helps us know,
> O(N log N) is the way to go.


state: COMMENTED
author_association: NONE
submitted_at: 2025-05-23T10:40:55Z
user: login: gemini-code-assist[bot]
body: ## Code Review
This pull request aims to add time and space complexity analysis to various solutions for the Beautiful Array problem. This is a valuable addition for understanding the performance characteristics of each approach. However, several of the stated time complexities appear to be incorrect. I've detailed these in the comments below.

### Summary of Findings
* **Incorrect Time Complexity for V2 (`beautifulArray`)**: The `beautifulArray` method (V2) has a time complexity of `O(N^2)` due to `ArrayList.remove(0)` operations in a loop, not `O(N log N)` as commented.
* **Incorrect Recursive Call and Time Complexity for V3-1 (`beautifulArray_3_1`)**: The `beautifulArray_3_1` method incorrectly calls the `O(N^2)` V2 `beautifulArray` method for its recursive steps, making its actual time complexity `O(N^2)`. If it were to call itself, it would be `O(N log N)`. The comment needs to reflect the actual implementation or the implementation needs to be fixed.
* **Incorrect Time Complexity for V3-2 (`beautifulArray_3_2`)**: The `beautifulArray_3_2` method, which iteratively builds the solution, has a time complexity of `O(N)`, not `O(N log N)` as commented.
* **Correctness of V1 Complexity**: The time and space complexity `O(N log N)` and `O(N)` respectively for `beautifulArray_1` (V1) appear to be correct.

### Merge Readiness
The pull request introduces important complexity analyses. However, three out of the four added time complexity comments are inaccurate. Given the nature of these inaccuracies (including a critical issue with unintended recursive calls in V3-1), I recommend that these changes be addressed before merging. I am not authorized to approve pull requests, so please ensure further review and approval after corrections are made.
state: CHANGES_REQUESTED
author_association: NONE
submitted_at: 2025-05-23T10:44:14Z

Review comments:
diff_hunk: @@ -109,6 +111,7 @@ public void recursion(int[] arr, int left, int right){
 
     // V3-1
     // https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
+    // time: O(N log N), space: O(N)
path: leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
user: login: gemini-code-assist[bot]
body: ![critical](https://img.shields.io/badge/priority-critical-red)

There are two concerns regarding this method `beautifulArray_3_1` and its complexity analysis:

1.  **Recursive Calls**: This method currently calls `beautifulArray(...)` on lines 121 and 122. This `beautifulArray` method is the V2 implementation (defined at line 76), which has a time complexity of `O(N^2)` (as noted in another comment).
    If `beautifulArray_3_1` calls an `O(N^2)` function for its subproblems, its own time complexity will also be `O(N^2)`. Specifically, `T(N) = 2 * O((N/2)^2) + O(N) = O(N^2)`.

2.  **Stated Complexity**: The comment states `time: O(N log N)`. This would be correct if `beautifulArray_3_1` were to call *itself* recursively (i.e., `beautifulArray_3_1(n/2)` and `beautifulArray_3_1((n+1)/2)`). However, with the current recursive calls to V2, the time complexity is `O(N^2)`.

Could you please clarify the intended behavior? 
- If this method is meant to call the V2 `beautifulArray`, the time complexity in the comment should be updated to `O(N^2)`.
- If this method is meant to be a self-contained `O(N log N)` solution, the recursive calls on lines 121 and 122 should be changed to `beautifulArray_3_1(...)`.

The space complexity `O(N)` is correct under both scenarios.

```suggestion
    // time: O(N^2) (currently, due to calls to V2) or O(N log N) (if recursively calls itself), space: O(N)
```
created_at: 2025-05-23T10:44:13Z
author_association: NONE
diff_hunk: @@ -71,6 +72,7 @@ public int[] f(int N) {
     // V2
     // IDEA : Divide and Conquer
     // https://leetcode.com/problems/beautiful-array/solutions/1373064/java-clear-thinking-process-divide-and-conquer/
+    // time: O(N log N), space: O(N)
path: leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
user: login: gemini-code-assist[bot]
body: ![high](https://img.shields.io/badge/priority-high-orange)

The time complexity for this `beautifulArray` (V2) method seems to be `O(N^2)` rather than `O(N log N)`. 

The `recursion` helper method (lines 85-110) involves these steps for a sub-array of size `M`:
1. Partitioning elements into `ArrayList<Integer> l` and `r`: This takes `O(M)` time (lines 94-100).
2. Merging elements back into `arr`: This loop (lines 102-107) iterates `M` times. Inside the loop, `l.remove(0)` or `r.remove(0)` on an `ArrayList` takes `O(M)` time in the worst case (as it requires shifting subsequent elements).
   This makes the merging step `O(M^2)`.

The recurrence relation for the time complexity of `recursion` is `T(M) = 2 * T(M/2) + O(M^2)`. According to the Master Theorem, this solves to `T(M) = O(M^2)`.
Since the initial call is with `M=n`, the overall time complexity is `O(N^2)`.

The space complexity `O(N)` is correct.

```suggestion
    // time: O(N^2), space: O(N)
```
created_at: 2025-05-23T10:44:13Z
author_association: NONE
diff_hunk: @@ -129,6 +132,7 @@ public int[] beautifulArray_3_1(int n) {
 
     // V3-2
     // https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
+    // time: O(N log N), space: O(N)
path: leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java
user: login: gemini-code-assist[bot]
body: ![high](https://img.shields.io/badge/priority-high-orange)

The time complexity for this `beautifulArray_3_2` method appears to be `O(N)` rather than `O(N log N)`.

Here's the reasoning:
- The `while(answer.size()<n)` loop (line 139) iterates. In each iteration, the size of the `answer` ArrayList roughly doubles (elements `2*x-1` and `2*x` are added, filtered by `<=n`). This means the loop runs `O(log N)` times if the size doubles consistently.
- Inside the `while` loop, two `for` loops (lines 141, 145) iterate over the current `answer` list. If `answer.size()` is `k`, this part takes `O(k)` time. The `temp.add(...)` operations are amortized `O(1)`.
- The sum of `k` over the iterations where `k` goes `1, approx_2, approx_4, ..., approx_N/2` before the final iteration that reaches size `N` or more, is `O(1+2+4+...+N/2) = O(N)`.
- The final step `answer.stream().mapToInt(i -> i).toArray()` (line 152) takes `O(N)` time to convert the ArrayList of size `N` to an `int[]`.

Therefore, the total time complexity is dominated by these linear operations, resulting in `O(N)`.

The space complexity `O(N)` is correct.

```suggestion
    // time: O(N), space: O(N)
```
created_at: 2025-05-23T10:44:13Z
author_association: NONE

PR comments:
user: login: yennanliu
created_at: 2025-05-23T10:47:15Z
body: /gemini review

review whether the new added time, space complexity are correct
author_association: OWNER
user: login: gemini-code-assist[bot]
created_at: 2025-05-23T10:48:13Z
body: Hello @yennanliu,

Thanks for submitting this pull request and for asking me to review the time and space complexities you've added. I've gone through the changes and reviewed the complexity annotations for each method.

Overall, the complexities are correctly identified for many of the methods. However, I found a few instances where the annotations might need slight adjustments or clarification based on the specific implementation or standard complexity definitions.

Here's a breakdown by file:

**BeautifulArray.java**

*   `beautifulArray_1` (V1, recursive with memoization): The time complexity `O(N log N)` and space complexity `O(N)` are generally considered correct for this approach, where the space is dominated by the memoization table storing results for subproblems up to size N. A tighter bound for the memoization space might be O(N log N) if we sum the sizes of all stored arrays, but O(N) is often acceptable if considering the number of distinct subproblems.
*   `beautifulArray` (V2, iterative): The time complexity is actually `O(N)` because in each step, you process the current list of numbers to generate the next list, and the total number of operations across all steps sums up to O(N). The space complexity is `O(N)` for storing the list of numbers, which grows up to size N. The annotation `time: O(N log N), space: O(N)` seems incorrect for time.
*   `beautifulArray_3_1` (V3-1, recursive): The time complexity `O(N log N)` and space complexity `O(N)` are correct, similar to V1.
*   `beautifulArray_3_2` (V3-2, iterative): Similar to V2, the time complexity should be `O(N)`, not `O(N log N)`. The space complexity `O(N)` is correct.

**BestTimeToBuyAndSellStock.java**

*   All added complexities (`time: O(N), space: O(1)`) are correct for the linear time, constant space approaches implemented.

**FirstMissingPositive.java**

*   `firstMissingPositive` (V0): The time complexity `O(N)` and space complexity `O(N)` (due to the HashMap) are correct.

**LongestArithmeticSubsequence.java**

*   `longestArithSeqLength_2` (V2, DP): The time complexity `O(N^2)` and space complexity `O(N^2)` (due to the map storing differences for each index) are correct.
*   `longestArithSeqLength_3` (V3, DP): The time complexity `O(N^2)` and space complexity `O(N^2)` (due to the array of HashMaps) are correct.

**MajorityElement.java**

*   `majorityElement` (V0, HashMap): The time complexity `O(N)` and space complexity `O(N)` (due to the HashMap) are correct.
*   `majorityElement_1` (V1, Boyer-Moore): The time complexity `O(N)` and space complexity `O(1)` are correct.

**MergeIntervals.java**

*   `merge` (V0), `merge_0_0_1` (V0-0-1), `merge_0_1` (V0-1), `merge_0_2` (V0-2), `merge_0_3` (V0-3), `merge_1_1` (V1-1), `merge_2` (V2): The time complexity `O(N log N)` (dominated by sorting) and space complexity `O(N)` (for the output list) are correct for these sorting-based approaches.
*   `merge_1_2` (V1-2, Sweep Line): The time complexity `O(N log N)` (due to TreeMap operations) and space complexity `O(N)` (for the TreeMap) are correct.
*   `merge_1_3` (V1-3, Greedy): The code snippet provided for this method seems to implement a different logic than the standard greedy merge intervals approach after sorting. Based on the code, the time complexity appears to be O(N + max_end_value) and space O(max_end_value), where `max_end_value` can be much larger than N. If the intention was the standard greedy approach after sorting, the annotation `O(N log N)` time and `O(N)` space would be correct, but it doesn't seem to match the code snippet.
*   `merge_3` (V3, Connected Components): The time complexity `O(N^2)` is correct due to checking all pairs for overlaps. However, the space complexity for storing the graph (adjacency list/map) can be up to `O(N^2)` in the worst case (a dense graph where every interval overlaps with every other interval). The annotation `space: O(N)` seems incorrect.

**PlusOne.java**

*   `plusOne` (V0, List conversion) and `plusOne_1_1` (V1-1, Recursion): The time complexity `O(N)` and space complexity `O(N)` (for the list or recursion stack) are correct.
*   `plusOne_0_1` (V0-1), `plusOne_1_2` (V1-2), `plusOne_1_3` (V1-3), `plusOne_2` (V2), `plusOne_3` (V3): These iterative approaches modify the array in place unless a new array of size N+1 is needed (when all digits are 9). In the worst case, they require creating a new array of size N+1, resulting in `O(N)` auxiliary space. The annotation `space: O(1)` seems incorrect for the auxiliary space in the worst case.

**ProductOfArrayExceptSelf.java**

*   `productExceptSelf` (V0, naive with division): The time complexity `O(N)` and space complexity `O(N)` (including the output array) are correct.
*   `productExceptSelf_0_1` (V0-1, O(1) space), `productExceptSelf_3` (V2, O(1) space): The time complexity `O(N)` and auxiliary space complexity `O(1)` (excluding the output array) are correct.
*   `productExceptSelf_2` (V1, Left/Right arrays): The time complexity `O(N)` and space complexity `O(N)` (for the left, right, and output arrays) are correct.

**RotateImage.java**

*   All added complexities (`time: O(N^2), space: O(1)` for in-place rotations and `time: O(N^2), space: O(N^2)` for the brute force approach) are correct.

**ThreeSum.java**

*   `threeSum` (V1, Two Pointers after sort) and `threeSum_2` (V2, Two Pointers after sort): The time complexity `O(N^2)` (dominated by the nested loops after sorting) and auxiliary space complexity `O(1)` (excluding the output list and sorting space) are correct.
*   `threeSum_3` (V3, Hash Set) and `threeSum_4` (V4, "No-Sort" with Hash Sets): The time complexity `O(N^2)` and space complexity `O(N)` (due to the Hash Sets) are correct.

**ValidSudoku.java**

*   All added complexities (`time: O(1), space: O(1)`) are correct because the size of the Sudoku board is fixed at 9x9, making the operations constant time and space relative to the input size (which is effectively constant).


In summary, most of the complexities are accurate. The main areas for potential review are the time complexity for the iterative `BeautifulArray` implementations (V2 and V3-2), the space complexity for the iterative `PlusOne` implementations (V0-1, V1-2, V1-3, V2, V3) when considering auxiliary space in the worst case, the space complexity for the graph-based `MergeIntervals` (V3), and the complexity annotation for `MergeIntervals` V1-3 based on the provided code snippet.

Let me know if you'd like me to elaborate on any of these points or if you have further questions!

author_association: NONE