Git Product home page Git Product logo

java-notes's Introduction

Java Notes Solving Problems

  1. Solve the Longest Palindromic Substring problem using the dynamic programming approach.
class Solution {
  public String longestPalindrome(String s) {
    if (s == null || s.length() < 1) {
      return "";
    }

    int start = 0, end = 0;

    for (int i = 0; i < s.length(); i++) {
      int len1 = expandAroundCenter(s, i, i);
      int len2 = expandAroundCenter(s, i, i + 1);
      int len = Math.max(len1, len2);

      if (len > end - start) {
        start = i - (len - 1) / 2;
        end = i + len / 2;
      }
    }

    return s.substring(start, end + 1);
  }

  private int expandAroundCenter(String s, int left, int right) {
      while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
        left--;
        right++;
      }

      return right - left - 1;
  }
}

// Test this solution:
public class Main {
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    String s1 = "babad";
    System.out.println(solution.longestPalindrome(s1));  // Output: "bab" or "aba"

    // Example 2
    String s2 = "cbbd";
    System.out.println(solution.longestPalindrome(s2));  // Output: "bb"
  }
}
  1. Solve the "Best Time to Buy and Sell Stock" problem using a simple approach that keeps track of the minimum stock price and calculates the maximum profit.
class Solution {
  public int maxProfit(int[] prices) {
    if (prices == null || prices.length == 0) {
      return 0;
    }

    int minPrice = prices[0];
    int maxProfit = 0;

    for (int i = 1; i < prices.length; i++) {
      if (prices[i] < minPrice) {
        minPrice = prices[i];
      } else if (prices[i] - minPrice > maxProfit) {
        maxProfit = prices[i] - minPrice;
      }
    }

    return maxProfit;
  }
}

Test this solution with the following test cases:

public class Main {
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    int[] prices1 = {7, 1, 5, 3, 6, 4};
    System.out.println(solution.maxProfit(prices1));  // Output: 5

    // Example 2
    int[] prices3 = {7, 6, 4, 3, 1};
    System.out.println(solution.maxProfit(prices3));  // Output: 0
  }
}
  1. Solve the "Best Time to Buy and Sell Stock II" problem using a simple approach that calculates the maximum profit by adding the difference between the consecutive elements of the array if the second element is larger than the first one.
class Solution {
  public int maxProfit(int[] prices) {
    if (prices == null || prices.length <= 1) {
      return 0;
    }

    int maxProfit = 0;

    for (int i = 1; i < prices.length; i++) {
      int profit = prices[i] - prices[i - 1];
      if (profit > 0) {
        maxProfit += profit;
      }
    }

    return maxProfit;
  }
}

Test this solution with the following test cases:

public class Main {
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    int[] prices1 = {7, 1, 5, 3, 6, 4};
    System.out.println(solution.maxProfit(prices1));  // Output: 7

    // Example 2
    int[] prices2 = {1, 2, 3, 4, 5};
    System.out.println(solution.maxProfit(prices2));  // Output: 4

    // Example 3
    int[] prices3 = {7, 6, 4, 3, 1};
    System.out.println(solution.maxProfit(prices3));  // Output: 0
  }
}
  1. Solve the "Best Time to Buy and Sell Stock III" problem using the dynamic programming approach.
class Solution {
  public int maxProfit(int[] prices) {
    if (prices == null || prices.length == 0) {
      return 0;
    }

    int n = prices.length;
    int[] left = new int[n];
    int[] right = new int[n];

    int min = prices[0];

    for (int i = 1; i < n; i++) {
      left[i] = Math.max(left[i - 1], prices[i] - min);
      min = Math.min(min, prices[i]);
    }

    int max = prices[n - 1];

    for (int i = n - 2; i >= 0; i--) {
      right[i] = Math.max(right[i + 1], max - prices[i]);
      max = Math.max(max, prices[i]);
    }

    int maxProfit = 0;

    for (int i = 0; i < n; i++) {
      maxProfit = Math.max(maxProfit, left[i] + right[i]);
    }

    return maxProfit;
  }
}

Test this solution with the following test cases:

public class Main {
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    int[] prices1 = {3, 3, 5, 0, 0, 3, 1, 4};
    System.out.println(solution.maxProfit(prices1));  // Output: 6

    // Example 2
    int[] prices2 = {1, 2, 3, 4, 5};
    System.out.println(solution.maxProfit(prices2));  // Output: 4

    // Example 3
    int[] prices3 = {7, 6, 4, 3, 1};
    System.out.println(solution.maxProfit(prices3));  // Output: 0
  }
}
  1. Solve the "Best Time to Buy and Sell Stock IV" problem using the dynamic programming approach.
class Solution {
  public int maxProfit(int k, int[] prices) {
    if (prices == null || prices.length == 0) {
      return 0;
    }

    int n = prices.length;

    if (k >= n / 2) {
      int maxProfit = 0;

      for (int i = 1; i < n; i++) {
        if (prices[i] > prices[i - 1]) {
          maxProfit += prices[i] - prices[i - 1];
        }
      }

      return maxProfit;
    }

    int[][] dp = new int[k + 1][n];

    for (int i = 1; i <= k; i++) {
      int maxDiff = -prices[0];

      for (int j = 1; j < n; j++) {
        dp[i][j] = Math.max(dp[i][j - 1], prices[j] + maxDiff);
        maxDiff = Math.max(maxDiff, dp[i - 1][j] - prices[j]);
      }
    }

    return dp[k][n - 1];
  }
}

Test this solution with the following test cases:

public class Main {
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1
    int k1 = 2;
    int[] prices1 = {2, 4, 1};
    System.out.println(solution.maxProfit(k1, prices1));  // Output: 2

    // Example 2
    int k2 = 2;
    int[] prices2 = {3, 2, 6, 5, 0, 3};
    System.out.println(solution.maxProfit(k2, prices2));  // Output: 7
  }
}

java-notes's People

Contributors

rodcyb3dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.