Posts

leetcode 54 Spiral Matrix – Medium

  54 Spiral Matrix – Medium Problem: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. Thoughts: Go in a Spiral Way, one trip contains four part, go right, go down, go left and go up. Repeat Spiral trip until there is only one column or row left. Solutions: public class Solution { public List<Integer> spiralOrder ( int [][] matrix) { List<Integer> result = new ArrayList<Integer>(); if (matrix == null || matrix.length == 0 ) return result; int m = matrix.length; int n = matrix[ 0 ].length; int x= 0 ; int y= 0 ; while (m> 0 && n> 0 ){ //if one row/column left, no circle can be formed if (m== 1 ){ for ( int i= 0 ; i<n; i++){ result.add(matrix[x][y++]);

leetcode 53. Maximum Subarray

53 Maximum Subarray – Medium Problem: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. Thoughts: If for an array max, max[i] stands for the maximum subarray ending at position i. So that max[i] = Math.max(max[i-1], nums[i]). But all we need is the maximum subarray for the whole array. So that we even don’t need the actual array. Just use a temp variable to keep the previous max value. Solutions: public class Solution { public int maxSubArray ( int [] nums) { int currSum = 0 ; int max = Integer.MIN_VALUE; for ( int i = 0 ; i < nums.length; i ++) { currSum = Math.max(currSum + nums[i], nums[i]); max = Math.max(currSum, max); } return max; } }

leetcode 51 . N-Queens

Image
  The   n-queens   puzzle is the problem of placing   n   queens on an   n x n   chessboard such that no two queens attack each other. Given an integer  n , return  all distinct solutions to the  n-queens puzzle . You may return the answer in  any order . Each solution contains a distinct board configuration of the n-queens' placement, where  'Q'  and  '.'  both indicate a queen and an empty space, respectively. Example 1: Input: n = 4 Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above Example 2: Input: n = 1 Output: [["Q"]]   Constraints: 1 <= n <= 9 public class Solution { public boolean isMatch (String s, String p) { return match(s, p, 0 , 0 ); } private boolean match (String s, String p, int s1, int s2) { if (s1 == s

Raspberry Pi 5 Review: A New Standard for Makers (Updated)

Image
Raspberry Pi 5 Review: A New Standard for Makers (Updated)   T he release date of the Raspberry Pi 5 has finally arrived, and the demand for it is surpassing the available supply. To assist you in acquiring your own Raspberry Pi 5, we have prepared a comprehensive " where to buy " guide. Once you have obtained the Raspberry Pi 5, you will undoubtedly be eager to embark on more ambitious and impressive projects.  After the longest wait between flagship Raspberry Pi models ever (the Raspberry Pi 4 was released in June 2019), the Raspberry Pi 5 is finally here and it's early! In late 2022, Raspberry Pi CEO Eben Upton said that we wouldn’t see the Raspberry Pi 5 in 2023, and that it would be a year for Raspberry Pi stock to improve after a global chip shortage. But it seems that Upton wanted to offer a surprise to the many loyal Raspberry Pi fans. The board is being announced now, and you’ll be able to buy one on October 23. The Raspberry Pi 5 claims to have two to three time