diff --git a/ARR1_238_Product_of_Array_Except_Self.java b/ARR1_238_Product_of_Array_Except_Self.java new file mode 100644 index 00000000..2694863c --- /dev/null +++ b/ARR1_238_Product_of_Array_Except_Self.java @@ -0,0 +1,98 @@ +/**** Method 1 ****/ +//Time Complexity: O(n^2) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +// Brute Force: Start a loop and we will have two inner loops which find the product of idx = 0 to idx = i-1 and other idx = i+1 and idx = n-1, later store the product of both inner loops values in answer array. + +/**** Method 2 ****/ +//Time Complexity: O(n) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +// In this solution use two arrays prefix and suffix to store the product,initiate pro = 1 and starting with prefix array, we traverse the array from 0 to n-1 and store the product in the prefix array and then do pro*= arr[i]. Do the same with suffix array but we start from end. At the end we create a new result array which has the product of both pre[i]*suf[i] at it's ith value. + +/**** Method 3 ****/ +//Time Complexity: O(n) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +// Same as above method but we reduce the space here, rather than using two arrays, prefix and suffix, we only use one array which is prefix array and fill it first, later when traversing from n-1 to 0, we do the product arr[i] with pre[i] and store it in pre[i], later pro*=arr[i], at the end return prefix array. + +public class ARR1_238_Product_of_Array_Except_Self { + + public static void main(String[] args) {} + + /**** Method 1 ****/ + public int[] productExceptSelf(int[] nums) { + int arr[] = new int[nums.length]; + + for (int i = 0; i < nums.length; i++) { + int pro = 1; + + int idx = 0; + while (idx < i) { + pro *= nums[idx]; + idx++; + } + + idx = i + 1; + + while (idx < nums.length) { + pro *= nums[idx]; + idx++; + } + + arr[i] = pro; + } + + return arr; + } + + /**** Method 2 ****/ + public int[] productExceptSelf1(int[] nums) { + int ans[] = new int[nums.length]; + int pre[] = new int[nums.length]; + int suf[] = new int[nums.length]; + + int pro = 1; + for (int i = 0; i < nums.length; i++) { + pre[i] = pro; + pro *= nums[i]; + } + + pro = 1; + for (int i = nums.length - 1; i >= 0; i--) { + suf[i] = pro; + pro *= nums[i]; + } + + for (int i = 0; i < nums.length; i++) { + ans[i] = pre[i] * suf[i]; + } + + return ans; + } + + /**** Method 3 ****/ + public int[] productExceptSelf2(int[] nums) { + int ans[] = new int[nums.length]; + + int pro = 1; + for (int i = 0; i < nums.length; i++) { + ans[i] = pro; + pro *= nums[i]; + } + + pro = 1; + for (int i = nums.length - 1; i >= 0; i--) { + ans[i] = pro * ans[i]; + pro *= nums[i]; + } + + return ans; + } +} diff --git a/ARR1_498_Diagonal_Traverse.java b/ARR1_498_Diagonal_Traverse.java new file mode 100644 index 00000000..f71bf211 --- /dev/null +++ b/ARR1_498_Diagonal_Traverse.java @@ -0,0 +1,98 @@ +/**** Method 1 ****/ +//Time Complexity: O(n^2) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +// Brute Force: Start a loop and maintain a direction to find which way we can traverse, dir = 1, when we dir = 1, we do i-- and j++ until it reaches a wall(index out of bound) then change the dir = -1 and traverse i++ and j--, and add each of those value to array and return it. + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ARR1_498_Diagonal_Traverse { + + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + + int[] result = new int[m * n]; + int r = 0, c = 0; + + int dir = 1; + + for (int i = 0; i < m * n; i++) { + result[i] = mat[r][c]; + + if (dir == 1) { + if (c == n - 1) { + r++; + dir = -1; + } else if (r == 0) { + c++; + dir = -1; + } else { + r--; + c++; + } + } else { + if (r == m - 1) { + c++; + dir = 1; + } else if (c == 0) { + r++; + dir = 1; + } else { + r++; + c--; + } + } + } + + return result; + } + + public int[] findDiagonalOrder1(int[][] mat) { + int val = mat.length * mat[0].length; + + int[] ans = new int[val]; + int idx = 0; + + List list; + + Map> map = new HashMap<>(); + + for (int i = 0; i < mat.length; i++) { + for (int j = 0; j < mat[0].length; j++) { + int num = i + j; + + if (map.containsKey(num)) { + list = map.get(num); + } else { + list = new ArrayList<>(); + } + + list.add(mat[i][j]); + map.put(num, list); + } + } + + for (int i = 0; i < map.size(); i++) { + list = map.get(i); + if (i % 2 == 0) { + for (int j = list.size() - 1; j >= 0; j--) { + ans[idx] = list.get(j); + idx++; + } + } else { + for (int j = 0; j < list.size(); j++) { + ans[idx] = list.get(j); + idx++; + } + } + } + + return ans; + } +} diff --git a/ARR1_54_Spiral_Matrix.java b/ARR1_54_Spiral_Matrix.java new file mode 100644 index 00000000..84c18a30 --- /dev/null +++ b/ARR1_54_Spiral_Matrix.java @@ -0,0 +1,53 @@ +/**** Method 1 ****/ +//Time Complexity: O(n) +//Space Complexity: O(n) + +//Successfully submitted in LeetCode + +// Start with i= 0, j=0 and when reach any of the wall i.e last index of col or row, we change the direction. + +import java.util.ArrayList; +import java.util.List; + +public class ARR1_54_Spiral_Matrix { + + public List spiralOrder(int[][] arr) { + int minr = 0; + int minc = 0; + int maxr = arr.length - 1; + int maxc = arr[0].length - 1; + + List ans = new ArrayList<>(); + + while (minr <= maxr && minc <= maxc) { + //top wall + for (int j = minc; j <= maxc; j++) { + ans.add(arr[minr][j]); + } + minr++; + + //right wall + for (int i = minr; i <= maxr; i++) { + ans.add(arr[i][maxc]); + } + maxc--; + + //bottom wall + if (minr <= maxr) { + for (int j = maxc; j >= minc; j--) { + ans.add(arr[maxr][j]); + } + maxr--; + } + + //left wall + if (minc <= maxc) { + for (int i = maxr; i >= minr; i--) { + ans.add(arr[i][minc]); + } + minc++; + } + } + return ans; + } +}