Skip to content

Commit 8b52bff

Browse files
authored
Added tasks 2427, 2428, 2429, 2430.
1 parent fa41064 commit 8b52bff

File tree

12 files changed

+388
-0
lines changed

12 files changed

+388
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g2401_2500.s2427_number_of_common_factors;
2+
3+
// #Easy #Math #Enumeration #Number_Theory #2022_12_07_Time_1_ms_(81.93%)_Space_38.7_MB_(98.17%)
4+
5+
public class Solution {
6+
public int commonFactors(int a, int b) {
7+
int ans = 0;
8+
for (int i = 1; i <= Math.min(a, b); i++) {
9+
if (a % i == 0 && b % i == 0) {
10+
ans++;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2427\. Number of Common Factors
2+
3+
Easy
4+
5+
Given two positive integers `a` and `b`, return _the number of **common** factors of_ `a` _and_ `b`.
6+
7+
An integer `x` is a **common factor** of `a` and `b` if `x` divides both `a` and `b`.
8+
9+
**Example 1:**
10+
11+
**Input:** a = 12, b = 6
12+
13+
**Output:** 4
14+
15+
**Explanation:** The common factors of 12 and 6 are 1, 2, 3, 6.
16+
17+
**Example 2:**
18+
19+
**Input:** a = 25, b = 30
20+
21+
**Output:** 2
22+
23+
**Explanation:** The common factors of 25 and 30 are 1, 5.
24+
25+
**Constraints:**
26+
27+
* `1 <= a, b <= 1000`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2401_2500.s2428_maximum_sum_of_an_hourglass;
2+
3+
// #Medium #Array #Matrix #Prefix_Sum #2022_12_07_Time_4_ms_(93.51%)_Space_44.1_MB_(86.28%)
4+
5+
public class Solution {
6+
public int maxSum(int[][] grid) {
7+
int m = grid.length;
8+
int n = grid[0].length;
9+
int res = 0;
10+
for (int i = 0; i < m; i++) {
11+
for (int j = 0; j < n; j++) {
12+
if (isHourGlass(i, j, m, n)) {
13+
res = Math.max(res, calculate(i, j, grid));
14+
} else {
15+
// If we cannot form an hour glass from the current row anymore, just move to
16+
// the next one
17+
break;
18+
}
19+
}
20+
}
21+
return res;
22+
}
23+
24+
// Check if an hour glass can be formed from the current position
25+
private boolean isHourGlass(int r, int c, int m, int n) {
26+
return r + 2 < m && c + 2 < n;
27+
}
28+
29+
// Once we know an hourglass can be formed, just traverse the value
30+
private int calculate(int r, int c, int[][] grid) {
31+
int sum = 0;
32+
// Traverse the bottom and the top row of the hour glass at the same time
33+
for (int i = c; i <= c + 2; i++) {
34+
sum += grid[r][i];
35+
sum += grid[r + 2][i];
36+
}
37+
// Add the middle vlaue
38+
sum += grid[r + 1][c + 1];
39+
return sum;
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2428\. Maximum Sum of an Hourglass
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `grid`.
6+
7+
We define an **hourglass** as a part of the matrix with the following form:
8+
9+
![](https://assets.leetcode.com/uploads/2022/08/21/img.jpg)
10+
11+
Return _the **maximum** sum of the elements of an hourglass_.
12+
13+
**Note** that an hourglass cannot be rotated and must be entirely contained within the matrix.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/08/21/1.jpg)
18+
19+
**Input:** grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
20+
21+
**Output:** 30
22+
23+
**Explanation:** The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2022/08/21/2.jpg)
28+
29+
**Input:** grid = [[1,2,3],[4,5,6],[7,8,9]]
30+
31+
**Output:** 35
32+
33+
**Explanation:** There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.
34+
35+
**Constraints:**
36+
37+
* `m == grid.length`
38+
* `n == grid[i].length`
39+
* `3 <= m, n <= 150`
40+
* <code>0 <= grid[i][j] <= 10<sup>6</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2401_2500.s2429_minimize_xor;
2+
3+
// #Medium #Greedy #Bit_Manipulation #2022_12_07_Time_0_ms_(100.00%)_Space_39.6_MB_(87.47%)
4+
5+
public class Solution {
6+
public int minimizeXor(int num1, int num2) {
7+
int bits = Integer.bitCount(num2);
8+
int result = 0;
9+
for (int i = 30; i >= 0 && bits > 0; --i) {
10+
if ((1 << i & num1) != 0) {
11+
--bits;
12+
result = result ^ (1 << i);
13+
}
14+
}
15+
for (int i = 0; i <= 30 && bits > 0; ++i) {
16+
if ((1 << i & num1) == 0) {
17+
--bits;
18+
result = result ^ (1 << i);
19+
}
20+
}
21+
return result;
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2429\. Minimize XOR
2+
3+
Medium
4+
5+
Given two positive integers `num1` and `num2`, find the positive integer `x` such that:
6+
7+
* `x` has the same number of set bits as `num2`, and
8+
* The value `x XOR num1` is **minimal**.
9+
10+
Note that `XOR` is the bitwise XOR operation.
11+
12+
Return _the integer_ `x`. The test cases are generated such that `x` is **uniquely determined**.
13+
14+
The number of **set bits** of an integer is the number of `1`'s in its binary representation.
15+
16+
**Example 1:**
17+
18+
**Input:** num1 = 3, num2 = 5
19+
20+
**Output:** 3
21+
22+
**Explanation:** The binary representations of num1 and num2 are 0011 and 0101, respectively. The integer **3** has the same number of set bits as num2, and the value `3 XOR 3 = 0` is minimal.
23+
24+
**Example 2:**
25+
26+
**Input:** num1 = 1, num2 = 12
27+
28+
**Output:** 3
29+
30+
**Explanation:** The binary representations of num1 and num2 are 0001 and 1100, respectively. The integer **3** has the same number of set bits as num2, and the value `3 XOR 1 = 2` is minimal.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= num1, num2 <= 10<sup>9</sup></code>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g2401_2500.s2430_maximum_deletions_on_a_string;
2+
3+
// #Hard #String #Dynamic_Programming #Hash_Function #String_Matching #Rolling_Hash
4+
// #2022_12_07_Time_159_ms_(93.39%)_Space_42.7_MB_(79.28%)
5+
6+
import java.util.HashMap;
7+
8+
public class Solution {
9+
private String s;
10+
private int[] hash;
11+
private int[] pows;
12+
private HashMap<Integer, Integer> visited;
13+
14+
public int deleteString(String s) {
15+
this.s = s;
16+
if (isBad()) {
17+
return s.length();
18+
}
19+
visited = new HashMap<>();
20+
fill();
21+
22+
return helper(0, 1, 0, 1);
23+
}
24+
25+
private int helper(int a, int b, int id1, int id2) {
26+
int mask = (id1 << 12) + id2;
27+
int ans = 1;
28+
if (visited.containsKey(mask)) {
29+
return visited.get(mask);
30+
}
31+
for (; b < s.length(); a++, id2++, b += 2) {
32+
if ((hash[a + 1] - hash[id1]) * pows[id2] == (hash[b + 1] - hash[id2]) * pows[id1]) {
33+
if (id2 + 1 == s.length()) {
34+
ans = Math.max(ans, 2);
35+
} else {
36+
ans = Math.max(ans, 1 + helper(id2, id2 + 1, id2, id2 + 1));
37+
}
38+
}
39+
}
40+
visited.put(mask, ans);
41+
return ans;
42+
}
43+
44+
private void fill() {
45+
int n = s.length();
46+
hash = new int[n + 1];
47+
pows = new int[n];
48+
pows[0] = 1;
49+
hash[1] = s.charAt(0);
50+
for (int i = 1; i != n; i++) {
51+
int temp = pows[i] = pows[i - 1] * 1000000007;
52+
hash[i + 1] = s.charAt(i) * temp + hash[i];
53+
}
54+
}
55+
56+
private boolean isBad() {
57+
int i = 1;
58+
while (i < s.length()) {
59+
if (s.charAt(0) != s.charAt(i++)) {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2430\. Maximum Deletions on a String
2+
3+
Hard
4+
5+
You are given a string `s` consisting of only lowercase English letters. In one operation, you can:
6+
7+
* Delete **the entire string** `s`, or
8+
* Delete the **first** `i` letters of `s` if the first `i` letters of `s` are **equal** to the following `i` letters in `s`, for any `i` in the range `1 <= i <= s.length / 2`.
9+
10+
For example, if `s = "ababc"`, then in one operation, you could delete the first two letters of `s` to get `"abc"`, since the first two letters of `s` and the following two letters of `s` are both equal to `"ab"`.
11+
12+
Return _the **maximum** number of operations needed to delete all of_ `s`.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "abcabcdabc"
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
22+
23+
- Delete all the letters.
24+
25+
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
26+
27+
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "aaabaab"
32+
33+
**Output:** 4
34+
35+
**Explanation:**
36+
-
37+
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
38+
39+
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
40+
41+
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
42+
43+
- Delete all the letters.
44+
45+
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.
46+
47+
**Example 3:**
48+
49+
**Input:** s = "aaaaa"
50+
51+
**Output:** 5
52+
53+
**Explanation:** In each operation, we can delete the first letter of s.
54+
55+
**Constraints:**
56+
57+
* `1 <= s.length <= 4000`
58+
* `s` consists only of lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2401_2500.s2427_number_of_common_factors;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void commonFactors() {
11+
assertThat(new Solution().commonFactors(12, 6), equalTo(4));
12+
}
13+
14+
@Test
15+
void commonFactors2() {
16+
assertThat(new Solution().commonFactors(25, 30), equalTo(2));
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2401_2500.s2428_maximum_sum_of_an_hourglass;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxSum() {
11+
assertThat(
12+
new Solution()
13+
.maxSum(
14+
new int[][] {
15+
{6, 2, 1, 3}, {4, 2, 1, 5}, {9, 2, 8, 7}, {4, 1, 2, 9}
16+
}),
17+
equalTo(30));
18+
}
19+
20+
@Test
21+
void maxSum2() {
22+
assertThat(
23+
new Solution().maxSum(new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}), equalTo(35));
24+
}
25+
}

0 commit comments

Comments
 (0)