mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 03:24:57 +08:00
Refactor Code Style (#4151)
This commit is contained in:
@ -52,7 +52,7 @@ public class BoardPath {
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int bpRS(int curr, int end, int strg[]) {
|
||||
public static int bpRS(int curr, int end, int[] strg) {
|
||||
if (curr == end) {
|
||||
return 1;
|
||||
} else if (curr > end) {
|
||||
|
@ -6,7 +6,7 @@ public class BruteForceKnapsack {
|
||||
// Returns the maximum value that
|
||||
// can be put in a knapsack of
|
||||
// capacity W
|
||||
static int knapSack(int W, int wt[], int val[], int n) {
|
||||
static int knapSack(int W, int[] wt, int[] val, int n) {
|
||||
// Base Case
|
||||
if (n == 0 || W == 0) {
|
||||
return 0;
|
||||
@ -29,9 +29,9 @@ public class BruteForceKnapsack {
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String args[]) {
|
||||
int val[] = new int[] { 60, 100, 120 };
|
||||
int wt[] = new int[] { 10, 20, 30 };
|
||||
public static void main(String[] args) {
|
||||
int[] val = new int[] { 60, 100, 120 };
|
||||
int[] wt = new int[] { 10, 20, 30 };
|
||||
int W = 50;
|
||||
int n = val.length;
|
||||
System.out.println(knapSack(W, wt, val, n));
|
||||
|
@ -23,7 +23,7 @@ public class CatalanNumber {
|
||||
*/
|
||||
static long findNthCatalan(int n) {
|
||||
// Array to store the results of subproblems i.e Catalan numbers from [1...n-1]
|
||||
long catalanArray[] = new long[n + 1];
|
||||
long[] catalanArray = new long[n + 1];
|
||||
|
||||
// Initialising C₀ = 1 and C₁ = 1
|
||||
catalanArray[0] = 1;
|
||||
|
@ -15,8 +15,8 @@ package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
public class CountFriendsPairing {
|
||||
|
||||
public static boolean countFriendsPairing(int n, int a[]) {
|
||||
int dp[] = new int[n + 1];
|
||||
public static boolean countFriendsPairing(int n, int[] a) {
|
||||
int[] dp = new int[n + 1];
|
||||
// array of n+1 size is created
|
||||
dp[0] = 1;
|
||||
// since 1st index position value is fixed so it's marked as 1
|
||||
|
@ -5,9 +5,9 @@ package com.thealgorithms.dynamicprogramming;
|
||||
public class DyanamicProgrammingKnapsack {
|
||||
// Returns the maximum value that can
|
||||
// be put in a knapsack of capacity W
|
||||
static int knapSack(int W, int wt[], int val[], int n) {
|
||||
static int knapSack(int W, int[] wt, int[] val, int n) {
|
||||
int i, w;
|
||||
int K[][] = new int[n + 1][W + 1];
|
||||
int[][] K = new int[n + 1][W + 1];
|
||||
|
||||
// Build table K[][] in bottom up manner
|
||||
for (i = 0; i <= n; i++) {
|
||||
@ -26,9 +26,9 @@ public class DyanamicProgrammingKnapsack {
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String args[]) {
|
||||
int val[] = new int[] { 60, 100, 120 };
|
||||
int wt[] = new int[] { 10, 20, 30 };
|
||||
public static void main(String[] args) {
|
||||
int[] val = new int[] { 60, 100, 120 };
|
||||
int[] wt = new int[] { 10, 20, 30 };
|
||||
int W = 50;
|
||||
int n = val.length;
|
||||
System.out.println(knapSack(W, wt, val, n));
|
||||
|
@ -40,7 +40,7 @@ public class EggDropping {
|
||||
return eggFloor[n][m];
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
int n = 2, m = 4;
|
||||
// result outputs min no. of trials in worst case for n eggs and m floors
|
||||
int result = minTrials(n, m);
|
||||
|
@ -7,7 +7,7 @@ package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
public class KadaneAlgorithm {
|
||||
|
||||
public static boolean max_Sum(int a[], int predicted_answer) {
|
||||
public static boolean max_Sum(int[] a, int predicted_answer) {
|
||||
int sum = a[0], running_sum = 0;
|
||||
for (int k : a) {
|
||||
running_sum = running_sum + k;
|
||||
|
@ -5,13 +5,13 @@ package com.thealgorithms.dynamicprogramming;
|
||||
*/
|
||||
public class Knapsack {
|
||||
|
||||
private static int knapSack(int W, int wt[], int val[], int n)
|
||||
private static int knapSack(int W, int[] wt, int[] val, int n)
|
||||
throws IllegalArgumentException {
|
||||
if (wt == null || val == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
int i, w;
|
||||
int rv[][] = new int[n + 1][W + 1]; // rv means return value
|
||||
int[][] rv = new int[n + 1][W + 1]; // rv means return value
|
||||
|
||||
// Build table rv[][] in bottom up manner
|
||||
for (i = 0; i <= n; i++) {
|
||||
@ -34,9 +34,9 @@ public class Knapsack {
|
||||
}
|
||||
|
||||
// Driver program to test above function
|
||||
public static void main(String args[]) {
|
||||
int val[] = new int[] { 50, 100, 130 };
|
||||
int wt[] = new int[] { 10, 20, 40 };
|
||||
public static void main(String[] args) {
|
||||
int[] val = new int[] { 50, 100, 130 };
|
||||
int[] wt = new int[] { 10, 20, 40 };
|
||||
int W = 50;
|
||||
System.out.println(knapSack(W, wt, val, val.length));
|
||||
}
|
||||
|
@ -26,9 +26,8 @@ public class KnapsackMemoization {
|
||||
|
||||
// Returns the value of maximum profit using recursive approach
|
||||
int solveKnapsackRecursive(int capacity, int[] weights,
|
||||
int[] profits, int numOfItems,
|
||||
int[][] dpTable) {
|
||||
|
||||
int[] profits, int numOfItems,
|
||||
int[][] dpTable) {
|
||||
// Base condition
|
||||
if (numOfItems == 0 || capacity == 0) {
|
||||
return 0;
|
||||
|
@ -13,7 +13,7 @@ package com.thealgorithms.dynamicprogramming;
|
||||
public 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
|
||||
@ -28,7 +28,7 @@ public class LongestAlternatingSubsequence {
|
||||
element
|
||||
|
||||
*/
|
||||
int las[][] = new int[n][2]; // las = LongestAlternatingSubsequence
|
||||
int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
las[i][0] = las[i][1] = 1;
|
||||
@ -61,7 +61,7 @@ public class LongestAlternatingSubsequence {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int arr[] = { 10, 22, 9, 33, 49, 50, 31, 60 };
|
||||
int[] arr = { 10, 22, 9, 33, 49, 50, 31, 60 };
|
||||
int n = arr.length;
|
||||
System.out.println(
|
||||
"Length of Longest " +
|
||||
|
@ -11,7 +11,7 @@ public class LongestIncreasingSubsequence {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
|
||||
int arr[] = new int[n];
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = sc.nextInt();
|
||||
}
|
||||
@ -70,9 +70,9 @@ public class LongestIncreasingSubsequence {
|
||||
* @author Alon Firestein (https://github.com/alonfirestein)
|
||||
*/
|
||||
// A function for finding the length of the LIS algorithm in O(nlogn) complexity.
|
||||
public static int findLISLen(int a[]) {
|
||||
public static int findLISLen(int[] a) {
|
||||
int size = a.length;
|
||||
int arr[] = new int[size];
|
||||
int[] arr = new int[size];
|
||||
arr[0] = a[0];
|
||||
int lis = 1;
|
||||
for (int i = 1; i < size; i++) {
|
||||
|
@ -20,7 +20,7 @@ public class LongestPalindromicSubstring {
|
||||
if (input == null || input.length() == 0) {
|
||||
return input;
|
||||
}
|
||||
boolean arr[][] = new boolean[input.length()][input.length()];
|
||||
boolean[][] arr = new boolean[input.length()][input.length()];
|
||||
int start = 0, end = 0;
|
||||
for (int g = 0; g < input.length(); g++) {
|
||||
for (int i = 0, j = g; j < input.length(); i++, j++) {
|
||||
|
@ -8,9 +8,9 @@ package com.thealgorithms.dynamicprogramming;
|
||||
// minimizes the number of scalar multiplications.
|
||||
public class MatrixChainRecursiveTopDownMemoisation {
|
||||
|
||||
static int Memoized_Matrix_Chain(int p[]) {
|
||||
static int Memoized_Matrix_Chain(int[] p) {
|
||||
int n = p.length;
|
||||
int m[][] = new int[n][n];
|
||||
int[][] m = new int[n][n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
m[i][j] = Integer.MAX_VALUE;
|
||||
@ -19,7 +19,7 @@ public class MatrixChainRecursiveTopDownMemoisation {
|
||||
return Lookup_Chain(m, p, 1, n - 1);
|
||||
}
|
||||
|
||||
static int Lookup_Chain(int m[][], int p[], int i, int j) {
|
||||
static int Lookup_Chain(int[][] m, int[] p, int i, int j) {
|
||||
if (i == j) {
|
||||
m[i][j] = 0;
|
||||
return m[i][j];
|
||||
@ -43,7 +43,7 @@ public class MatrixChainRecursiveTopDownMemoisation {
|
||||
// in this code we are taking the example of 4 matrixes whose orders are 1x2,2x3,3x4,4x5 respectively
|
||||
// output should be Minimum number of multiplications is 38
|
||||
public static void main(String[] args) {
|
||||
int arr[] = { 1, 2, 3, 4, 5 };
|
||||
int[] arr = { 1, 2, 3, 4, 5 };
|
||||
System.out.println(
|
||||
"Minimum number of multiplications is " + Memoized_Matrix_Chain(arr)
|
||||
);
|
||||
|
@ -10,7 +10,7 @@ package com.thealgorithms.dynamicprogramming;
|
||||
public class NewManShanksPrime {
|
||||
|
||||
public static boolean nthManShanksPrime(int n, int expected_answer) {
|
||||
int a[] = new int[n + 1];
|
||||
int[] a = new int[n + 1];
|
||||
// array of n+1 size is initialized
|
||||
a[0] = a[1] = 1;
|
||||
// The 0th and 1st index position values are fixed. They are initialized as 1
|
||||
|
@ -134,7 +134,7 @@ public class RegexMatching {
|
||||
// Method 4: Bottom-Up DP(Tabulation)
|
||||
// Time Complexity=0(N*M) Space Complexity=0(N*M)
|
||||
static boolean regexBU(String src, String pat) {
|
||||
boolean strg[][] = new boolean[src.length() + 1][pat.length() + 1];
|
||||
boolean[][] strg = new boolean[src.length() + 1][pat.length() + 1];
|
||||
strg[src.length()][pat.length()] = true;
|
||||
for (int row = src.length(); row >= 0; row--) {
|
||||
for (int col = pat.length() - 1; col >= 0; col--) {
|
||||
|
@ -8,7 +8,7 @@ package com.thealgorithms.dynamicprogramming;
|
||||
public class RodCutting {
|
||||
|
||||
private static int cutRod(int[] price, int n) {
|
||||
int val[] = new int[n + 1];
|
||||
int[] val = new int[n + 1];
|
||||
val[0] = 0;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
@ -24,7 +24,7 @@ public class RodCutting {
|
||||
}
|
||||
|
||||
// main function to test
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = new int[] { 2, 5, 13, 19, 20 };
|
||||
int result = cutRod(arr, arr.length);
|
||||
System.out.println("Maximum Obtainable Value is " + result);
|
||||
|
@ -45,7 +45,7 @@ class ShortestSuperSequence {
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String args[]) {
|
||||
public static void main(String[] args) {
|
||||
String X = "AGGTAB";
|
||||
String Y = "GXTXAYB";
|
||||
|
||||
|
@ -49,11 +49,11 @@ public class SubsetCount {
|
||||
*/
|
||||
public int getCountSO(int[] arr, int target){
|
||||
int n = arr.length;
|
||||
int prev[]=new int[target+1];
|
||||
int[] prev =new int[target+1];
|
||||
prev[0] =1;
|
||||
if(arr[0]<=target) prev[arr[0]] = 1;
|
||||
for(int ind = 1; ind<n; ind++){
|
||||
int cur[]=new int[target+1];
|
||||
int[] cur =new int[target+1];
|
||||
cur[0]=1;
|
||||
for(int t= 1; t<=target; t++){
|
||||
int notTaken = prev[t];
|
||||
|
@ -31,7 +31,7 @@ public class UniquePaths {
|
||||
|
||||
// The above method runs in O(n) time
|
||||
public static boolean uniquePaths2(int m, int n, int ans) {
|
||||
int dp[][] = new int[m][n];
|
||||
int[][] dp = new int[m][n];
|
||||
for (int i = 0; i < m; i++) {
|
||||
dp[i][0] = 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user