LeetCode -Toeplitz Matrix

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Given a matrix, check if it is toeplitz. (Assume the matrix is not empty) Java Solution public boolean isToeplitzMatrix(int[][] matrix) { int m=matrix.length; int n=matrix[0].length; for(int i=0; i<m; i++){ for(int j=0; j<n; j++){ if(i+1<m && j+1<n && matrix[i][j]!=matrix[i+1][j+1]){ return false; … Read more

LeetCode – Rotated Digits (Java)

X is a good number if after rotating EACH digit individually by 180 degrees, we get a valid number that is different from X. A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number.

Read more

LeetCode – Count of Smaller Numbers After Self (Java)

You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Input: [5,2,6,1] Output: [2,1,1,0] Java Solution 1 public List<Integer> countSmaller(int[] nums) { List<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> sorted … Read more

LeetCode – Minimum Increment to Make Array Unique (Java)

Java Solution 1 public int minIncrementForUnique(int[] A) { Arrays.sort(A);   int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } }   return count; }public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; } Actually we do not need to increment … Read more