mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 03:24:57 +08:00
style: enable MethodName
in CheckStyle (#5182)
enabled: MethodName in CheckStyle
This commit is contained in:

committed by
GitHub

parent
ea4dc15a24
commit
295e7436b1
@ -10,7 +10,7 @@ public final class KadaneAlgorithm {
|
||||
private KadaneAlgorithm() {
|
||||
}
|
||||
|
||||
public static boolean max_Sum(int[] a, int predicted_answer) {
|
||||
public static boolean maxSum(int[] a, int predicted_answer) {
|
||||
int sum = a[0];
|
||||
int running_sum = 0;
|
||||
for (int k : a) {
|
||||
|
@ -16,7 +16,7 @@ public final class LongestAlternatingSubsequence {
|
||||
}
|
||||
|
||||
/* Function to return longest alternating subsequence length*/
|
||||
static int AlternatingLength(int[] arr, int n) {
|
||||
static int alternatingLength(int[] arr, int n) {
|
||||
/*
|
||||
|
||||
las[i][0] = Length of the longest
|
||||
@ -68,6 +68,6 @@ public final class LongestAlternatingSubsequence {
|
||||
int[] arr = {10, 22, 9, 33, 49, 50, 31, 60};
|
||||
int n = arr.length;
|
||||
System.out.println("Length of Longest "
|
||||
+ "alternating subsequence is " + AlternatingLength(arr, n));
|
||||
+ "alternating subsequence is " + alternatingLength(arr, n));
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ public final class LongestIncreasingSubsequence {
|
||||
return r;
|
||||
}
|
||||
|
||||
public static int LIS(int[] array) {
|
||||
public static int lis(int[] array) {
|
||||
int N = array.length;
|
||||
if (N == 0) {
|
||||
return 0;
|
||||
|
@ -12,14 +12,14 @@ public final class LongestPalindromicSubsequence {
|
||||
String a = "BBABCBCAB";
|
||||
String b = "BABCBAB";
|
||||
|
||||
String aLPS = LPS(a);
|
||||
String bLPS = LPS(b);
|
||||
String aLPS = lps(a);
|
||||
String bLPS = lps(b);
|
||||
|
||||
System.out.println(a + " => " + aLPS);
|
||||
System.out.println(b + " => " + bLPS);
|
||||
}
|
||||
|
||||
public static String LPS(String original) throws IllegalArgumentException {
|
||||
public static String lps(String original) throws IllegalArgumentException {
|
||||
StringBuilder reverse = new StringBuilder(original);
|
||||
reverse = reverse.reverse();
|
||||
return recursiveLPS(original, reverse.toString());
|
||||
|
@ -11,14 +11,14 @@ public final class LongestPalindromicSubstring {
|
||||
String a = "babad";
|
||||
String b = "cbbd";
|
||||
|
||||
String aLPS = LPS(a);
|
||||
String bLPS = LPS(b);
|
||||
String aLPS = lps(a);
|
||||
String bLPS = lps(b);
|
||||
|
||||
System.out.println(a + " => " + aLPS);
|
||||
System.out.println(b + " => " + bLPS);
|
||||
}
|
||||
|
||||
private static String LPS(String input) {
|
||||
private static String lps(String input) {
|
||||
if (input == null || input.length() == 0) {
|
||||
return input;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
private MatrixChainRecursiveTopDownMemoisation() {
|
||||
}
|
||||
|
||||
static int Memoized_Matrix_Chain(int[] p) {
|
||||
static int memoizedMatrixChain(int[] p) {
|
||||
int n = p.length;
|
||||
int[][] m = new int[n][n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -18,10 +18,10 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
m[i][j] = Integer.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
return Lookup_Chain(m, p, 1, n - 1);
|
||||
return lookupChain(m, p, 1, n - 1);
|
||||
}
|
||||
|
||||
static int Lookup_Chain(int[][] m, int[] p, int i, int j) {
|
||||
static int lookupChain(int[][] m, int[] p, int i, int j) {
|
||||
if (i == j) {
|
||||
m[i][j] = 0;
|
||||
return m[i][j];
|
||||
@ -30,7 +30,7 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
return m[i][j];
|
||||
} else {
|
||||
for (int k = i; k < j; k++) {
|
||||
int q = Lookup_Chain(m, p, i, k) + Lookup_Chain(m, p, k + 1, j) + (p[i - 1] * p[k] * p[j]);
|
||||
int q = lookupChain(m, p, i, k) + lookupChain(m, p, k + 1, j) + (p[i - 1] * p[k] * p[j]);
|
||||
if (q < m[i][j]) {
|
||||
m[i][j] = q;
|
||||
}
|
||||
@ -43,6 +43,6 @@ public final class MatrixChainRecursiveTopDownMemoisation {
|
||||
// respectively output should be Minimum number of multiplications is 38
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {1, 2, 3, 4, 5};
|
||||
System.out.println("Minimum number of multiplications is " + Memoized_Matrix_Chain(arr));
|
||||
System.out.println("Minimum number of multiplications is " + memoizedMatrixChain(arr));
|
||||
}
|
||||
}
|
||||
|
@ -16,22 +16,22 @@ public final class WineProblem {
|
||||
|
||||
// Method 1: Using Recursion
|
||||
// Time Complexity=0(2^N) Space Complexity=Recursion extra space
|
||||
public static int WPRecursion(int[] arr, int si, int ei) {
|
||||
public static int wpRecursion(int[] arr, int si, int ei) {
|
||||
int n = arr.length;
|
||||
int year = (n - (ei - si + 1)) + 1;
|
||||
if (si == ei) {
|
||||
return arr[si] * year;
|
||||
}
|
||||
|
||||
int start = WPRecursion(arr, si + 1, ei) + arr[si] * year;
|
||||
int end = WPRecursion(arr, si, ei - 1) + arr[ei] * year;
|
||||
int start = wpRecursion(arr, si + 1, ei) + arr[si] * year;
|
||||
int end = wpRecursion(arr, si, ei - 1) + arr[ei] * year;
|
||||
|
||||
return Math.max(start, end);
|
||||
}
|
||||
|
||||
// Method 2: Top-Down DP(Memoization)
|
||||
// Time Complexity=0(N*N) Space Complexity=0(N*N)+Recursion extra space
|
||||
public static int WPTD(int[] arr, int si, int ei, int[][] strg) {
|
||||
public static int wptd(int[] arr, int si, int ei, int[][] strg) {
|
||||
int n = arr.length;
|
||||
int year = (n - (ei - si + 1)) + 1;
|
||||
if (si == ei) {
|
||||
@ -41,8 +41,8 @@ public final class WineProblem {
|
||||
if (strg[si][ei] != 0) {
|
||||
return strg[si][ei];
|
||||
}
|
||||
int start = WPTD(arr, si + 1, ei, strg) + arr[si] * year;
|
||||
int end = WPTD(arr, si, ei - 1, strg) + arr[ei] * year;
|
||||
int start = wptd(arr, si + 1, ei, strg) + arr[si] * year;
|
||||
int end = wptd(arr, si, ei - 1, strg) + arr[ei] * year;
|
||||
|
||||
int ans = Math.max(start, end);
|
||||
|
||||
@ -53,7 +53,7 @@ public final class WineProblem {
|
||||
|
||||
// Method 3: Bottom-Up DP(Tabulation)
|
||||
// Time Complexity=0(N*N/2)->0(N*N) Space Complexity=0(N*N)
|
||||
public static int WPBU(int[] arr) {
|
||||
public static int wpbu(int[] arr) {
|
||||
int n = arr.length;
|
||||
int[][] strg = new int[n][n];
|
||||
|
||||
@ -76,9 +76,9 @@ public final class WineProblem {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {2, 3, 5, 1, 4};
|
||||
System.out.println("Method 1: " + WPRecursion(arr, 0, arr.length - 1));
|
||||
System.out.println("Method 2: " + WPTD(arr, 0, arr.length - 1, new int[arr.length][arr.length]));
|
||||
System.out.println("Method 3: " + WPBU(arr));
|
||||
System.out.println("Method 1: " + wpRecursion(arr, 0, arr.length - 1));
|
||||
System.out.println("Method 2: " + wptd(arr, 0, arr.length - 1, new int[arr.length][arr.length]));
|
||||
System.out.println("Method 3: " + wpbu(arr));
|
||||
}
|
||||
}
|
||||
// Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/
|
||||
|
Reference in New Issue
Block a user