Skip to content

Commit 09f3a38

Browse files
committed
Updated tags
1 parent 2414e78 commit 09f3a38

File tree

8 files changed

+88
-87
lines changed

8 files changed

+88
-87
lines changed

src/main/java/g3301_3400/s3330_find_the_original_typed_string_i/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3330_find_the_original_typed_string_i;
22

3-
// #Easy #2024_10_27_Time_1_ms_(100.00%)_Space_42_MB_(100.00%)
3+
// #Easy #String #2024_10_29_Time_1_ms_(96.13%)_Space_42_MB_(72.46%)
44

55
public class Solution {
66
public int possibleStringCount(String word) {

src/main/java/g3301_3400/s3331_find_subtree_sizes_after_changes/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3331_find_subtree_sizes_after_changes;
22

3-
// #Medium #2024_10_27_Time_129_ms_(100.00%)_Space_58.4_MB_(100.00%)
3+
// #Medium #Array #String #Hash_Table #Tree #Depth_First_Search
4+
// #2024_10_29_Time_166_ms_(52.73%)_Space_86.3_MB_(8.86%)
45

56
import java.util.ArrayList;
67
import java.util.HashMap;
Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
package g3301_3400.s3332_maximum_points_tourist_can_earn;
22

3-
// #Medium #2024_10_27_Time_305_ms_(100.00%)_Space_55.1_MB_(100.00%)
3+
// #Medium #Array #Dynamic_Programming #Matrix #2024_10_29_Time_53_ms_(100.00%)_Space_55_MB_(78.55%)
44

55
public class Solution {
6-
int days;
7-
int cities;
8-
Integer[][] dp;
9-
10-
private int f(int day, int city, int[][] stayScore, int[][] travelScore) {
11-
if (day == days) {
12-
return 0;
13-
}
14-
if (dp[day][city] != null) {
15-
return dp[day][city];
16-
}
17-
int maxScore = 0;
18-
for (int desCity = 0; desCity < cities; desCity++) {
19-
int score;
20-
if (desCity == city) {
21-
score = stayScore[day][city];
22-
} else {
23-
score = travelScore[city][desCity];
6+
public int maxScore(int n, int k, int[][] stayScores, int[][] travelScores) {
7+
// dp[day][city]
8+
int[][] dp = new int[k + 1][n];
9+
int result = 0;
10+
for (int day = k - 1; day >= 0; day--) {
11+
for (int city = 0; city < n; city++) {
12+
int stayScore = stayScores[day][city] + dp[day + 1][city];
13+
int travelScore = 0;
14+
for (int nextCity = 0; nextCity < n; nextCity++) {
15+
int nextScore = travelScores[city][nextCity] + dp[day + 1][nextCity];
16+
travelScore = Math.max(nextScore, travelScore);
17+
}
18+
dp[day][city] = Math.max(stayScore, travelScore);
19+
result = Math.max(dp[day][city], result);
2420
}
25-
maxScore = Math.max(maxScore, score + f(day + 1, desCity, stayScore, travelScore));
26-
}
27-
dp[day][city] = maxScore;
28-
return dp[day][city];
29-
}
30-
31-
public int maxScore(int n, int k, int[][] stayScore, int[][] travelScore) {
32-
days = k;
33-
cities = n;
34-
int maxScore = 0;
35-
dp = new Integer[days + 1][cities + 1];
36-
for (int city = 0; city < cities; city++) {
37-
maxScore = Math.max(maxScore, f(0, city, stayScore, travelScore));
3821
}
39-
return maxScore;
22+
return result;
4023
}
4124
}

src/main/java/g3301_3400/s3333_find_the_original_typed_string_ii/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3333_find_the_original_typed_string_ii;
22

3-
// #Hard #2024_10_27_Time_89_ms_(50.00%)_Space_58.4_MB_(100.00%)
3+
// #Hard #String #Dynamic_Programming #Prefix_Sum
4+
// #2024_10_29_Time_89_ms_(90.20%)_Space_55.6_MB_(40.38%)
45

56
import java.util.ArrayList;
67
import java.util.List;

src/main/java/g3301_3400/s3334_find_the_maximum_factor_score_of_array/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3301_3400.s3334_find_the_maximum_factor_score_of_array;
22

3-
// #Medium #2024_10_27_Time_4_ms_(100.00%)_Space_43.9_MB_(100.00%)
3+
// #Medium #Array #Math #Number_Theory #2024_10_29_Time_5_ms_(95.93%)_Space_43.4_MB_(40.07%)
44

55
public class Solution {
66
public long maxScore(int[] nums) {

src/main/java/g3301_3400/s3335_total_characters_in_string_after_transformations_i/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3335_total_characters_in_string_after_transformations_i;
22

3-
// #Medium #2024_10_27_Time_77_ms_(50.00%)_Space_45.4_MB_(100.00%)
3+
// #Medium #String #Hash_Table #Dynamic_Programming #Math #Counting
4+
// #2024_10_29_Time_77_ms_(77.83%)_Space_45.7_MB_(37.40%)
45

56
import java.util.LinkedList;
67

src/main/java/g3301_3400/s3336_find_the_number_of_subsequences_with_equal_gcd/Solution.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package g3301_3400.s3336_find_the_number_of_subsequences_with_equal_gcd;
22

3-
// #Hard #2024_10_27_Time_370_ms_(100.00%)_Space_125.3_MB_(100.00%)
3+
// #Hard #Array #Dynamic_Programming #Math #Number_Theory
4+
// #2024_10_29_Time_408_ms_(50.28%)_Space_114.9_MB_(56.91%)
45

56
import java.util.Arrays;
67

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,87 @@
11
package g3301_3400.s3337_total_characters_in_string_after_transformations_ii;
22

3-
// #Hard #2024_10_27_Time_340_ms_(100.00%)_Space_45.1_MB_(100.00%)
3+
// #Hard #String #Hash_Table #Dynamic_Programming #Math #Counting
4+
// #2024_10_29_Time_67_ms_(99.31%)_Space_45.4_MB_(45.83%)
45

56
import java.util.List;
67

78
public class Solution {
8-
private static final int MOD = 1_000_000_007;
9-
private static final int ALPHABET_SIZE = 26;
9+
public static final int MOD = 1000000007;
10+
public static final long M2 = (long) MOD * MOD;
11+
public static final long BIG = 8L * M2;
1012

1113
public int lengthAfterTransformations(String s, int t, List<Integer> nums) {
12-
// Initialize transformation matrix M
13-
int[][] matrix = new int[ALPHABET_SIZE][ALPHABET_SIZE];
14-
for (int i = 0; i < ALPHABET_SIZE; i++) {
15-
int transforms = nums.get(i);
16-
for (int j = 0; j < transforms; j++) {
17-
matrix[i][(i + j + 1) % ALPHABET_SIZE]++;
14+
int[][] m = new int[26][26];
15+
for (int i = 0; i < 26; i++) {
16+
for (int j = 1; j <= nums.get(i); j++) {
17+
m[(i + j) % 26][i]++;
1818
}
1919
}
20-
// Initialize count array based on string `s`
21-
int[] count = new int[ALPHABET_SIZE];
22-
for (char ch : s.toCharArray()) {
23-
count[ch - 'a']++;
20+
int[] v = new int[26];
21+
for (char c : s.toCharArray()) {
22+
v[c - 'a']++;
2423
}
25-
// Apply matrix exponentiation to get M^t
26-
int[][] matrixT = power(matrix, t);
27-
// Calculate final character counts after t transformations
28-
int[] finalCount = new int[ALPHABET_SIZE];
29-
for (int i = 0; i < ALPHABET_SIZE; i++) {
30-
for (int j = 0; j < ALPHABET_SIZE; j++) {
31-
finalCount[j] =
32-
(int) ((finalCount[j] + ((long) matrixT[i][j] * count[i]) % MOD) % MOD);
24+
v = pow(m, v, t);
25+
long ans = 0;
26+
for (int x : v) {
27+
ans += x;
28+
}
29+
return (int) (ans % MOD);
30+
}
31+
32+
// A^e*v
33+
private int[] pow(int[][] a, int[] v, long e) {
34+
for (int i = 0; i < v.length; i++) {
35+
if (v[i] >= MOD) {
36+
v[i] %= MOD;
3337
}
3438
}
35-
// Calculate total length
36-
int totalLength = 0;
37-
for (int cnt : finalCount) {
38-
totalLength = (totalLength + cnt) % MOD;
39+
int[][] mul = a;
40+
for (; e > 0; e >>>= 1) {
41+
if ((e & 1) == 1) {
42+
v = mul(mul, v);
43+
}
44+
mul = p2(mul);
3945
}
40-
return totalLength;
46+
return v;
4147
}
4248

43-
// Matrix multiplication function
44-
private int[][] multiply(int[][] a, int[][] b) {
45-
int[][] matrixC = new int[ALPHABET_SIZE][ALPHABET_SIZE];
46-
for (int i = 0; i < ALPHABET_SIZE; i++) {
47-
for (int j = 0; j < ALPHABET_SIZE; j++) {
48-
for (int k = 0; k < ALPHABET_SIZE; k++) {
49-
matrixC[i][j] =
50-
(int) ((matrixC[i][j] + ((long) a[i][k] * b[k][j]) % MOD) % MOD);
49+
// int matrix*int vector
50+
private int[] mul(int[][] a, int[] v) {
51+
int m = a.length;
52+
int n = v.length;
53+
int[] w = new int[m];
54+
for (int i = 0; i < m; i++) {
55+
long sum = 0;
56+
for (int k = 0; k < n; k++) {
57+
sum += (long) a[i][k] * v[k];
58+
if (sum >= BIG) {
59+
sum -= BIG;
5160
}
5261
}
62+
w[i] = (int) (sum % MOD);
5363
}
54-
return matrixC;
64+
return w;
5565
}
5666

57-
// Matrix exponentiation function
58-
private int[][] power(int[][] matrix, int exp) {
59-
int[][] result = new int[ALPHABET_SIZE][ALPHABET_SIZE];
60-
for (int i = 0; i < ALPHABET_SIZE; i++) {
61-
// Identity matrix
62-
result[i][i] = 1;
63-
}
64-
while (exp > 0) {
65-
if (exp % 2 == 1) {
66-
result = multiply(result, matrix);
67+
// int matrix^2 (be careful about negative value)
68+
private int[][] p2(int[][] a) {
69+
int n = a.length;
70+
int[][] c = new int[n][n];
71+
for (int i = 0; i < n; i++) {
72+
long[] sum = new long[n];
73+
for (int k = 0; k < n; k++) {
74+
for (int j = 0; j < n; j++) {
75+
sum[j] += (long) a[i][k] * a[k][j];
76+
if (sum[j] >= BIG) {
77+
sum[j] -= BIG;
78+
}
79+
}
80+
}
81+
for (int j = 0; j < n; j++) {
82+
c[i][j] = (int) (sum[j] % MOD);
6783
}
68-
matrix = multiply(matrix, matrix);
69-
exp /= 2;
7084
}
71-
return result;
85+
return c;
7286
}
7387
}

0 commit comments

Comments
 (0)