Skip to content

Commit b2986d0

Browse files
authored
Added tasks 2484, 2485, 2486, 2487, 2488
1 parent 5708432 commit b2986d0

File tree

16 files changed

+471
-0
lines changed

16 files changed

+471
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,11 @@ implementation 'com.github.javadev:leetcode-in-java:1.17'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2488 |[Count Subarrays With Median K](src/main/java/g2401_2500/s2488_count_subarrays_with_median_k/Solution.java)| Hard | Array, Hash_Table, Prefix_Sum | 24 | 72.25
1852+
| 2487 |[Remove Nodes From Linked List](src/main/java/g2401_2500/s2487_remove_nodes_from_linked_list/Solution.java)| Medium | Stack, Linked_List, Monotonic_Stack, Recursion | 5 | 99.79
1853+
| 2486 |[Append Characters to String to Make Subsequence](src/main/java/g2401_2500/s2486_append_characters_to_string_to_make_subsequence/Solution.java)| Medium | String, Greedy, Two_Pointers | 2 | 99.89
1854+
| 2485 |[Find the Pivot Integer](src/main/java/g2401_2500/s2485_find_the_pivot_integer/Solution.java)| Easy | Math, Prefix_Sum | 1 | 95.67
1855+
| 2484 |[Count Palindromic Subsequences](src/main/java/g2401_2500/s2484_count_palindromic_subsequences/Solution.java)| Hard | String, Dynamic_Programming | 93 | 76.16
18511856
| 2483 |[Minimum Penalty for a Shop](src/main/java/g2401_2500/s2483_minimum_penalty_for_a_shop/Solution.java)| Medium | String, Prefix_Sum | 17 | 69.62
18521857
| 2482 |[Difference Between Ones and Zeros in Row and Column](src/main/java/g2401_2500/s2482_difference_between_ones_and_zeros_in_row_and_column/Solution.java)| Medium | Array, Matrix, Simulation | 9 | 94.05
18531858
| 2481 |[Minimum Cuts to Divide a Circle](src/main/java/g2401_2500/s2481_minimum_cuts_to_divide_a_circle/Solution.java)| Easy | Math, Geometry | 0 | 100.00
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2401_2500.s2484_count_palindromic_subsequences;
2+
3+
// #Hard #String #Dynamic_Programming #2023_01_26_Time_93_ms_(76.16%)_Space_40.2_MB_(99.74%)
4+
5+
public class Solution {
6+
public int countPalindromes(String s) {
7+
int n = s.length();
8+
long ans = 0;
9+
int mod = (int) 1e9 + 7;
10+
int[] count = new int[10];
11+
for (int i = 0; i < n; i++) {
12+
long m = 0;
13+
for (int j = n - 1; j > i; j--) {
14+
if (s.charAt(i) == s.charAt(j)) {
15+
ans += (m * (j - i - 1));
16+
ans = ans % mod;
17+
}
18+
m += count[s.charAt(j) - '0'];
19+
}
20+
count[s.charAt(i) - '0']++;
21+
}
22+
return (int) ans;
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2484\. Count Palindromic Subsequences
2+
3+
Hard
4+
5+
Given a string of digits `s`, return _the number of **palindromic subsequences** of_ `s` _having length_ `5`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
**Note:**
8+
9+
* A string is **palindromic** if it reads the same forward and backward.
10+
* A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "103301"
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are 6 possible subsequences of length 5: "10330","10331","10301","10301","13301","03301". Two of them (both equal to "10301") are palindromic.
19+
20+
**Example 2:**
21+
22+
**Input:** s = "0000000"
23+
24+
**Output:** 21
25+
26+
**Explanation:** All 21 subsequences are "00000", which is palindromic.
27+
28+
**Example 3:**
29+
30+
**Input:** s = "9999900000"
31+
32+
**Output:** 2
33+
34+
**Explanation:** The only two palindromic subsequences are "99999" and "00000".
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 10<sup>4</sup></code>
39+
* `s` consists of digits.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2401_2500.s2485_find_the_pivot_integer;
2+
3+
// #Easy #Math #Prefix_Sum #2023_01_26_Time_1_ms_(95.67%)_Space_39.2_MB_(91.00%)
4+
5+
public class Solution {
6+
public int pivotInteger(int n) {
7+
if (n == 0 || n == 1) {
8+
return n;
9+
}
10+
int sum = 0;
11+
for (int i = 1; i <= n; i++) {
12+
sum += i;
13+
}
14+
int ad = 0;
15+
for (int i = 1; i <= n; i++) {
16+
ad += i - 1;
17+
sum -= i;
18+
if (sum == ad) {
19+
return i;
20+
}
21+
}
22+
return -1;
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2485\. Find the Pivot Integer
2+
3+
Easy
4+
5+
Given a positive integer `n`, find the **pivot integer** `x` such that:
6+
7+
* The sum of all elements between `1` and `x` inclusively equals the sum of all elements between `x` and `n` inclusively.
8+
9+
Return _the pivot integer_ `x`. If no such integer exists, return `-1`. It is guaranteed that there will be at most one pivot index for the given input.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 8
14+
15+
**Output:** 6
16+
17+
**Explanation:** 6 is the pivot integer since: 1 + 2 + 3 + 4 + 5 + 6 = 6 + 7 + 8 = 21.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 1
22+
23+
**Output:** 1
24+
25+
**Explanation:** 1 is the pivot integer since: 1 = 1.
26+
27+
**Example 3:**
28+
29+
**Input:** n = 4
30+
31+
**Output:** -1
32+
33+
**Explanation:** It can be proved that no such integer exist.
34+
35+
**Constraints:**
36+
37+
* `1 <= n <= 1000`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2401_2500.s2486_append_characters_to_string_to_make_subsequence;
2+
3+
// #Medium #String #Greedy #Two_Pointers #2023_01_26_Time_2_ms_(99.89%)_Space_49.5_MB_(39.30%)
4+
5+
public class Solution {
6+
public int appendCharacters(String s, String t) {
7+
int lengthOfT = t.length();
8+
int indexOfT = 0;
9+
int indexOfS = 0;
10+
int position;
11+
if (s.contains(t)) {
12+
return 0;
13+
}
14+
while (indexOfT < lengthOfT) {
15+
position = s.indexOf(t.charAt(indexOfT), indexOfS);
16+
if (position < 0) {
17+
return lengthOfT - indexOfT;
18+
}
19+
indexOfS = position;
20+
indexOfT++;
21+
indexOfS++;
22+
}
23+
return lengthOfT - (indexOfT);
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2486\. Append Characters to String to Make Subsequence
2+
3+
Medium
4+
5+
You are given two strings `s` and `t` consisting of only lowercase English letters.
6+
7+
Return _the minimum number of characters that need to be appended to the end of_ `s` _so that_ `t` _becomes a **subsequence** of_ `s`.
8+
9+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "coaching", t = "coding"
14+
15+
**Output:** 4
16+
17+
**Explanation:** Append the characters "ding" to the end of s so that s = "coachingding". Now, t is a subsequence of s ("<ins>**co**</ins>aching<ins>**ding**</ins>"). It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "abcde", t = "a"
22+
23+
**Output:** 0
24+
25+
**Explanation:** t is already a subsequence of s ("<ins>**a**</ins>bcde").
26+
27+
**Example 3:**
28+
29+
**Input:** s = "z", t = "abcde"
30+
31+
**Output:** 5
32+
33+
**Explanation:** Append the characters "abcde" to the end of s so that s = "zabcde". Now, t is a subsequence of s ("z<ins>**abcde**</ins>"). It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= s.length, t.length <= 10<sup>5</sup></code>
38+
* `s` and `t` consist only of lowercase English letters.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g2401_2500.s2487_remove_nodes_from_linked_list;
2+
3+
// #Medium #Stack #Linked_List #Monotonic_Stack #Recursion
4+
// #2023_01_26_Time_5_ms_(99.79%)_Space_67_MB_(74.44%)
5+
6+
import com_github_leetcode.ListNode;
7+
8+
public class Solution {
9+
public ListNode removeNodes(ListNode head) {
10+
head = reverse(head);
11+
if (head == null) {
12+
return null;
13+
}
14+
int max = head.val;
15+
ListNode temp = head;
16+
ListNode temp1 = head.next;
17+
while (temp1 != null) {
18+
if (temp1.val >= max) {
19+
max = temp1.val;
20+
temp.next = temp1;
21+
temp = temp.next;
22+
}
23+
temp1 = temp1.next;
24+
}
25+
temp.next = null;
26+
return reverse(head);
27+
}
28+
29+
private ListNode reverse(ListNode head) {
30+
if (head == null || head.next == null) {
31+
return head;
32+
}
33+
ListNode prev = null;
34+
ListNode curr = head;
35+
ListNode next;
36+
while (curr != null) {
37+
next = curr.next;
38+
curr.next = prev;
39+
prev = curr;
40+
curr = next;
41+
}
42+
return prev;
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2487\. Remove Nodes From Linked List
2+
3+
Medium
4+
5+
You are given the `head` of a linked list.
6+
7+
Remove every node which has a node with a **strictly greater** value anywhere to the right side of it.
8+
9+
Return _the_ `head` _of the modified linked list._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2022/10/02/drawio.png)
14+
15+
**Input:** head = [5,2,13,3,8]
16+
17+
**Output:** [13,8]
18+
19+
**Explanation:** The nodes that should be removed are 5, 2 and 3.
20+
21+
- Node 13 is to the right of node 5.
22+
- Node 13 is to the right of node 2.
23+
- Node 8 is to the right of node 3.
24+
25+
**Example 2:**
26+
27+
**Input:** head = [1,1,1,1]
28+
29+
**Output:** [1,1,1,1]
30+
31+
**Explanation:** Every node has value 1, so no nodes are removed.
32+
33+
**Constraints:**
34+
35+
* The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.
36+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g2401_2500.s2488_count_subarrays_with_median_k;
2+
3+
// #Hard #Array #Hash_Table #Prefix_Sum #2023_01_26_Time_24_ms_(72.25%)_Space_51_MB_(99.24%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int countSubarrays(int[] nums, int k) {
10+
int idx;
11+
int n = nums.length;
12+
int ans = 0;
13+
for (idx = 0; idx < n; idx++) {
14+
if (nums[idx] == k) {
15+
break;
16+
}
17+
}
18+
int[][] arr = new int[n - idx][2];
19+
int j = 1;
20+
for (int i = idx + 1; i < n; i++) {
21+
if (nums[i] < k) {
22+
arr[j][0] = arr[j - 1][0] + 1;
23+
arr[j][1] = arr[j - 1][1];
24+
} else {
25+
arr[j][1] = arr[j - 1][1] + 1;
26+
arr[j][0] = arr[j - 1][0];
27+
}
28+
j++;
29+
}
30+
Map<Integer, Integer> map = new HashMap<>();
31+
for (int[] ints : arr) {
32+
int d2 = ints[1] - ints[0];
33+
map.put(d2, map.getOrDefault(d2, 0) + 1);
34+
}
35+
int s1 = 0;
36+
int g1 = 0;
37+
for (int i = idx; i >= 0; i--) {
38+
if (nums[i] < k) {
39+
s1++;
40+
} else if (nums[i] > k) {
41+
g1++;
42+
}
43+
int d1 = g1 - s1;
44+
int cur = map.getOrDefault(-d1, 0) + map.getOrDefault(1 - d1, 0);
45+
ans += cur;
46+
}
47+
return ans;
48+
}
49+
}

0 commit comments

Comments
 (0)