Skip to content
Prev Previous commit
Next Next commit
Fixed sonar
  • Loading branch information
javadev committed Mar 6, 2025
commit c8912c45f9f093b8680dd9ceaf6c31c0c96252aa
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private int solve(int[] nums, int i, int last) {
int res = Math.max(nums[i], nums[i + 1]) + solve(nums, i + 2, last);
res = Math.min(res, Math.max(nums[i], nums[last]) + solve(nums, i + 2, i + 1));
res = Math.min(res, Math.max(nums[i + 1], nums[last]) + solve(nums, i + 2, i));
return dp[i][last] = res;
dp[i][last] = res;
return res;
}
}
12 changes: 6 additions & 6 deletions src/main/java/g3401_3500/s3470_permutations_iv/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class Solution {
// Define a large constant value to cap calculations and prevent overflow
private static final long CAP = 1000000000000001L;
// 3D DP array to store precomputed results for dynamic programming
private static final long[][][] DP = new long[105][105][3];
private final long[][][] dp = new long[105][105][3];

// Initialize DP array with -1 (indicating uncomputed states)
static {
for (long[][] longs : DP) {
{
for (long[][] longs : dp) {
for (long[] aLong : longs) {
Arrays.fill(aLong, -1);
}
Expand All @@ -26,8 +26,8 @@ private long rec(int o, int e, int req) {
if (o == 0 && e == 0) {
return 1;
}
if (DP[o][e][req] != -1) {
return DP[o][e][req];
if (dp[o][e][req] != -1) {
return dp[o][e][req];
}
long count = 0;
if (req == 2) {
Expand All @@ -46,7 +46,7 @@ private long rec(int o, int e, int req) {
count = multiplyCapped(e, rec(o, e - 1, 0));
}
}
DP[o][e][req] = count;
dp[o][e][req] = count;
return count;
}

Expand Down