View on GitHub

SCRATCH PAD

No polish, just pulse

Striver’s SDE Sheet Notes (Days 1-9)


#### Day 1: Arrays

1. Set Matrix Zeroes

  public void setZeroes(int[][] matrix) {
      int rows = matrix.length, cols = matrix[0].length;
      boolean firstRowZero = false, firstColZero = false;

      // Check if the first row or column should be zeroed
      for (int i = 0; i < rows; i++) if (matrix[i][0] == 0) firstColZero = true;
      for (int j = 0; j < cols; j++) if (matrix[0][j] == 0) firstRowZero = true;

      // Mark zeroes in the first row/column
      for (int i = 1; i < rows; i++)
          for (int j = 1; j < cols; j++)
              if (matrix[i][j] == 0) {
                  matrix[i][0] = 0;
                  matrix[0][j] = 0;
              }

      // Zero out rows/columns based on markers
      for (int i = 1; i < rows; i++)
          for (int j = 1; j < cols; j++)
              if (matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0;

      // Handle the first row and column separately
      if (firstRowZero) for (int j = 0; j < cols; j++) matrix[0][j] = 0;
      if (firstColZero) for (int i = 0; i < rows; i++) matrix[i][0] = 0;
  }
  

2. Pascal’s Triangle

  public List<List<Integer>> generate(int numRows) {
      List<List<Integer>> triangle = new ArrayList<>();

      for (int i = 0; i < numRows; i++) {
          List<Integer> row = new ArrayList<>();
          row.add(1);  // First element of each row
          for (int j = 1; j < i; j++) {
              row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
          }
          if (i > 0) row.add(1);  // Last element
          triangle.add(row);
      }

      return triangle;
  }
  

3. Next Permutation

  public void nextPermutation(int[] nums) {
      int n = nums.length, i = n - 2;

      // Find the first decreasing element
      while (i >= 0 && nums[i] >= nums[i + 1]) i--;

      if (i >= 0) {
          // Find the next larger element to swap
          int j = n - 1;
          while (nums[j] <= nums[i]) j--;
          int temp = nums[i];
          nums[i] = nums[j];
          nums[j] = temp;
      }

      // Reverse the remaining elements
      reverse(nums, i + 1, n - 1);
  }

  private void reverse(int[] nums, int start, int end) {
      while (start < end) {
          int temp = nums[start];
          nums[start] = nums[end];
          nums[end] = temp;
          start++;
          end--;
      }
  }
  

4. Kadane’s Algorithm

  public int maxSubArray(int[] nums) {
      int currentSum = 0, maxSum = nums[0];

      for (int num : nums) {
          currentSum = Math.max(num, currentSum + num);
          maxSum = Math.max(maxSum, currentSum);
      }

      return maxSum;
  }
  

5. Sort Colors (Dutch National Flag Algorithm)

  public void sortColors(int[] nums) {
      int low = 0, mid = 0, high = nums.length - 1;

      while (mid <= high) {
          if (nums[mid] == 0) {
              int temp = nums[low];
              nums[low] = nums[mid];
              nums[mid] = temp;
              low++; mid++;
          } else if (nums[mid] == 1) {
              mid++;
          } else {
              int temp = nums[mid];
              nums[mid] = nums[high];
              nums[high] = temp;
              high--;
          }
      }
  }
  

#### Day 1: Arrays

6. Best Time to Buy and Sell Stock

  public int maxProfit(int[] prices) {
      int minPrice = Integer.MAX_VALUE;
      int maxProfit = 0;

      for (int price : prices) {
          if (price < minPrice) {
              minPrice = price;
          } else if (price - minPrice > maxProfit) {
              maxProfit = price - minPrice;
          }
      }

      return maxProfit;
  }