Skip to content

Commit c1bc7aa

Browse files
committed
Added tasks 3502-3505
1 parent c2a1554 commit c1bc7aa

File tree

12 files changed

+564
-0
lines changed

12 files changed

+564
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g3501_3600.s3502_minimum_cost_to_reach_every_position;
2+
3+
// #Easy #2025_03_30_Time_1_ms_(97.67%)_Space_44.71_MB_(95.61%)
4+
5+
public class Solution {
6+
public int[] minCosts(int[] cost) {
7+
int min = cost[0];
8+
int[] ans = new int[cost.length];
9+
ans[0] = min;
10+
for (int i = 1; i < cost.length; i++) {
11+
min = Math.min(min, cost[i]);
12+
ans[i] = min;
13+
}
14+
return ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3502\. Minimum Cost to Reach Every Position
2+
3+
Easy
4+
5+
You are given an integer array `cost` of size `n`. You are currently at position `n` (at the end of the line) in a line of `n + 1` people (numbered from 0 to `n`).
6+
7+
You wish to move forward in the line, but each person in front of you charges a specific amount to **swap** places. The cost to swap with person `i` is given by `cost[i]`.
8+
9+
You are allowed to swap places with people as follows:
10+
11+
* If they are in front of you, you **must** pay them `cost[i]` to swap with them.
12+
* If they are behind you, they can swap with you for free.
13+
14+
Return an array `answer` of size `n`, where `answer[i]` is the **minimum** total cost to reach each position `i` in the line.
15+
16+
**Example 1:**
17+
18+
**Input:** cost = [5,3,4,1,3,2]
19+
20+
**Output:** [5,3,3,1,1,1]
21+
22+
**Explanation:**
23+
24+
We can get to each position in the following way:
25+
26+
* `i = 0`. We can swap with person 0 for a cost of 5.
27+
* `i = 1`. We can swap with person 1 for a cost of 3.
28+
* `i = 2`. We can swap with person 1 for a cost of 3, then swap with person 2 for free.
29+
* `i = 3`. We can swap with person 3 for a cost of 1.
30+
* `i = 4`. We can swap with person 3 for a cost of 1, then swap with person 4 for free.
31+
* `i = 5`. We can swap with person 3 for a cost of 1, then swap with person 5 for free.
32+
33+
**Example 2:**
34+
35+
**Input:** cost = [1,2,4,6,7]
36+
37+
**Output:** [1,1,1,1,1]
38+
39+
**Explanation:**
40+
41+
We can swap with person 0 for a cost of 1, then we will be able to reach any position `i` for free.
42+
43+
**Constraints:**
44+
45+
* `1 <= n == cost.length <= 100`
46+
* `1 <= cost[i] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3501_3600.s3503_longest_palindrome_after_substring_concatenation_i;
2+
3+
// #Medium #2025_03_30_Time_389_ms_(56.95%)_Space_45.18_MB_(96.47%)
4+
5+
public class Solution {
6+
public int longestPalindrome(String s, String t) {
7+
int result = 0;
8+
for (int i = 0; i <= s.length(); i++) {
9+
for (int j = i; j <= s.length(); j++) {
10+
String subStrS = s.substring(i, j);
11+
for (int k = 0; k <= t.length(); k++) {
12+
for (int l = k; l <= t.length(); l++) {
13+
String subStrT = t.substring(k, l);
14+
String combineStr = subStrS + subStrT;
15+
if (isPalindrome(combineStr)) {
16+
result = Math.max(result, combineStr.length());
17+
}
18+
}
19+
}
20+
}
21+
}
22+
return result;
23+
}
24+
25+
private boolean isPalindrome(String input) {
26+
int left = 0;
27+
int right = input.length() - 1;
28+
while (left < right) {
29+
if (input.charAt(left) != input.charAt(right)) {
30+
return false;
31+
}
32+
left++;
33+
right--;
34+
}
35+
return true;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3503\. Longest Palindrome After Substring Concatenation I
2+
3+
Medium
4+
5+
You are given two strings, `s` and `t`.
6+
7+
You can create a new string by selecting a **substring** from `s` (possibly empty) and a substring from `t` (possibly empty), then concatenating them **in order**.
8+
9+
Return the length of the **longest** palindrome that can be formed this way.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "a", t = "a"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
Concatenating `"a"` from `s` and `"a"` from `t` results in `"aa"`, which is a palindrome of length 2.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abc", t = "def"
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
Since all characters are different, the longest palindrome is any single character, so the answer is 1.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "b", t = "aaaa"
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
Selecting "`aaaa`" from `t` is the longest palindrome, so the answer is 4.
40+
41+
**Example 4:**
42+
43+
**Input:** s = "abcde", t = "ecdba"
44+
45+
**Output:** 5
46+
47+
**Explanation:**
48+
49+
Concatenating `"abc"` from `s` and `"ba"` from `t` results in `"abcba"`, which is a palindrome of length 5.
50+
51+
**Constraints:**
52+
53+
* `1 <= s.length, t.length <= 30`
54+
* `s` and `t` consist of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package g3501_3600.s3504_longest_palindrome_after_substring_concatenation_ii;
2+
3+
// #Hard #2025_03_30_Time_21_ms_(100.00%)_Space_56.36_MB_(57.78%)
4+
5+
public class Solution {
6+
private int[] sPa;
7+
private int[] tPa;
8+
private char[] ss;
9+
private char[] tt;
10+
11+
public int longestPalindrome(String s, String t) {
12+
final int sLen = s.length();
13+
final int tLen = t.length();
14+
ss = s.toCharArray();
15+
tt = t.toCharArray();
16+
int[][] palindrome = new int[sLen][tLen + 1];
17+
sPa = new int[sLen];
18+
tPa = new int[tLen];
19+
int maxLen = 1;
20+
for (int j = 0; j < tLen; j++) {
21+
if (ss[0] == tt[j]) {
22+
palindrome[0][j] = 2;
23+
sPa[0] = 2;
24+
tPa[j] = 2;
25+
maxLen = 2;
26+
}
27+
}
28+
for (int i = 1; i < sLen; i++) {
29+
for (int j = 0; j < tLen; j++) {
30+
if (ss[i] == tt[j]) {
31+
palindrome[i][j] = 2 + palindrome[i - 1][j + 1];
32+
sPa[i] = Math.max(sPa[i], palindrome[i][j]);
33+
tPa[j] = Math.max(tPa[j], palindrome[i][j]);
34+
maxLen = Math.max(maxLen, palindrome[i][j]);
35+
}
36+
}
37+
}
38+
for (int i = 0; i < sLen - 1; i++) {
39+
int len = maxS(i, i + 1);
40+
maxLen = Math.max(maxLen, len);
41+
}
42+
for (int i = 1; i < sLen; i++) {
43+
int len = maxS(i - 1, i + 1) + 1;
44+
maxLen = Math.max(maxLen, len);
45+
}
46+
for (int j = 0; j < tLen - 1; j++) {
47+
int len = maxT(j, j + 1);
48+
maxLen = Math.max(maxLen, len);
49+
}
50+
for (int j = 0; j < tLen - 1; j++) {
51+
int len = maxT(j - 1, j + 1) + 1;
52+
maxLen = Math.max(maxLen, len);
53+
}
54+
return maxLen;
55+
}
56+
57+
private int maxS(int left, int right) {
58+
int len = 0;
59+
while (left >= 0 && right < ss.length && ss[left] == ss[right]) {
60+
len += 2;
61+
left--;
62+
right++;
63+
}
64+
if (left >= 0) {
65+
len += sPa[left];
66+
}
67+
return len;
68+
}
69+
70+
private int maxT(int left, int right) {
71+
int len = 0;
72+
while (left >= 0 && right < tt.length && tt[left] == tt[right]) {
73+
len += 2;
74+
left--;
75+
right++;
76+
}
77+
if (right < tt.length) {
78+
len += tPa[right];
79+
}
80+
return len;
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3504\. Longest Palindrome After Substring Concatenation II
2+
3+
Hard
4+
5+
You are given two strings, `s` and `t`.
6+
7+
You can create a new string by selecting a **substring** from `s` (possibly empty) and a substring from `t` (possibly empty), then concatenating them **in order**.
8+
9+
Return the length of the **longest** palindrome that can be formed this way.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "a", t = "a"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
Concatenating `"a"` from `s` and `"a"` from `t` results in `"aa"`, which is a palindrome of length 2.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abc", t = "def"
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
Since all characters are different, the longest palindrome is any single character, so the answer is 1.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "b", t = "aaaa"
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
Selecting "`aaaa`" from `t` is the longest palindrome, so the answer is 4.
40+
41+
**Example 4:**
42+
43+
**Input:** s = "abcde", t = "ecdba"
44+
45+
**Output:** 5
46+
47+
**Explanation:**
48+
49+
Concatenating `"abc"` from `s` and `"ba"` from `t` results in `"abcba"`, which is a palindrome of length 5.
50+
51+
**Constraints:**
52+
53+
* `1 <= s.length, t.length <= 1000`
54+
* `s` and `t` consist of lowercase English letters.

0 commit comments

Comments
 (0)