Skip to content

Commit 9317ab4

Browse files
authored
Added tasks 2012, 2013, 2014.
1 parent a4fdb09 commit 9317ab4

File tree

9 files changed

+343
-0
lines changed

9 files changed

+343
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2001_2100.s2012_sum_of_beauty_in_the_array;
2+
3+
// #Medium #Array #2022_05_24_Time_10_ms_(44.69%)_Space_98.1_MB_(18.43%)
4+
5+
public class Solution {
6+
public int sumOfBeauties(int[] nums) {
7+
int[] maxArr = new int[nums.length];
8+
maxArr[0] = nums[0];
9+
for (int i = 1; i < nums.length - 1; i++) {
10+
maxArr[i] = Math.max(maxArr[i - 1], nums[i]);
11+
}
12+
int[] minArr = new int[nums.length];
13+
minArr[nums.length - 1] = nums[nums.length - 1];
14+
for (int i = nums.length - 2; i >= 0; i--) {
15+
minArr[i] = Math.min(minArr[i + 1], nums[i]);
16+
}
17+
18+
int sum = 0;
19+
for (int i = 1; i < nums.length - 1; i++) {
20+
if (nums[i] > maxArr[i - 1] && nums[i] < minArr[i + 1]) {
21+
sum += 2;
22+
} else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) {
23+
sum += 1;
24+
}
25+
}
26+
27+
return sum;
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2012\. Sum of Beauty in the Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`. For each index `i` (`1 <= i <= nums.length - 2`) the **beauty** of `nums[i]` equals:
6+
7+
* `2`, if `nums[j] < nums[i] < nums[k]`, for **all** `0 <= j < i` and for **all** `i < k <= nums.length - 1`.
8+
* `1`, if `nums[i - 1] < nums[i] < nums[i + 1]`, and the previous condition is not satisfied.
9+
* `0`, if none of the previous conditions holds.
10+
11+
Return _the **sum of beauty** of all_ `nums[i]` _where_ `1 <= i <= nums.length - 2`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3]
16+
17+
**Output:** 2
18+
19+
**Explanation:** For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 2.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,4,6,4]
24+
25+
**Output:** 1
26+
27+
**Explanation:** For each index i in the range 1 <= i <= 2:
28+
29+
- The beauty of nums[1] equals 1.
30+
31+
- The beauty of nums[2] equals 0.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [3,2,1]
36+
37+
**Output:** 0
38+
39+
**Explanation:** For each index i in the range 1 <= i <= 1: - The beauty of nums[1] equals 0.
40+
41+
**Constraints:**
42+
43+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
44+
* <code>1 <= nums[i] <= 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 g2001_2100.s2013_detect_squares;
2+
3+
// #Medium #Array #Hash_Table #Design #Counting
4+
// #2022_05_24_Time_67_ms_(88.46%)_Space_46.1_MB_(96.15%)
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class DetectSquares {
10+
private static final int MUL = 1002;
11+
private final Map<Integer, int[]> map;
12+
13+
public DetectSquares() {
14+
this.map = new HashMap<>();
15+
}
16+
17+
public void add(int[] point) {
18+
int x = point[0];
19+
int y = point[1];
20+
int hash = x * MUL + y;
21+
if (map.containsKey(hash)) {
22+
map.get(hash)[2]++;
23+
} else {
24+
map.put(hash, new int[] {x, y, 1});
25+
}
26+
}
27+
28+
public int count(int[] point) {
29+
int ans = 0;
30+
int x = point[0];
31+
int y = point[1];
32+
for (Map.Entry<Integer, int[]> entry : map.entrySet()) {
33+
int[] diap = entry.getValue();
34+
int x1 = diap[0];
35+
int y1 = diap[1];
36+
int num = diap[2];
37+
if (Math.abs(x - x1) != Math.abs(y - y1) || x == x1 || y == y1) {
38+
continue;
39+
} else {
40+
int p1hash = x * MUL + y1;
41+
int p2hash = x1 * MUL + y;
42+
if (map.containsKey(p1hash) && map.containsKey(p2hash)) {
43+
ans += map.get(p1hash)[2] * map.get(p2hash)[2] * num;
44+
}
45+
}
46+
}
47+
return ans;
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2013\. Detect Squares
2+
3+
Medium
4+
5+
You are given a stream of points on the X-Y plane. Design an algorithm that:
6+
7+
* **Adds** new points from the stream into a data structure. **Duplicate** points are allowed and should be treated as different points.
8+
* Given a query point, **counts** the number of ways to choose three points from the data structure such that the three points and the query point form an **axis-aligned square** with **positive area**.
9+
10+
An **axis-aligned square** is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and y-axis.
11+
12+
Implement the `DetectSquares` class:
13+
14+
* `DetectSquares()` Initializes the object with an empty data structure.
15+
* `void add(int[] point)` Adds a new point `point = [x, y]` to the data structure.
16+
* `int count(int[] point)` Counts the number of ways to form **axis-aligned squares** with point `point = [x, y]` as described above.
17+
18+
**Example 1:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/09/01/image.png)
21+
22+
**Input** ["DetectSquares", "add", "add", "add", "count", "count", "add", "count"] [[], [[3, 10]], [[11, 2]], [[3, 2]], [[11, 10]], [[14, 8]], [[11, 2]], [[11, 10]]]
23+
24+
**Output:** [null, null, null, null, 1, 0, null, 2]
25+
26+
**Explanation:**
27+
28+
DetectSquares detectSquares = new DetectSquares();
29+
30+
detectSquares.add([3, 10]);
31+
32+
detectSquares.add([11, 2]);
33+
34+
detectSquares.add([3, 2]);
35+
36+
detectSquares.count([11, 10]); // return 1. You can choose:
37+
// - The first, second, and third points
38+
39+
detectSquares.count([14, 8]); // return 0. The query point cannot form a square with any points in the data structure.
40+
41+
detectSquares.add([11, 2]); // Adding duplicate points is allowed.
42+
43+
detectSquares.count([11, 10]); // return 2. You can choose: // - The first, second, and third points // - The first, third, and fourth points
44+
45+
**Constraints:**
46+
47+
* `point.length == 2`
48+
* `0 <= x, y <= 1000`
49+
* At most `3000` calls **in total** will be made to `add` and `count`.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g2001_2100.s2014_longest_subsequence_repeated_k_times;
2+
3+
// #Hard #String #Greedy #Backtracking #Counting #Enumeration
4+
// #2022_05_24_Time_169_ms_(98.59%)_Space_48.8_MB_(57.75%)
5+
6+
import java.util.ArrayList;
7+
8+
public class Solution {
9+
public String longestSubsequenceRepeatedK(String s, int k) {
10+
char[] ca = s.toCharArray();
11+
12+
char[] freq = new char[26];
13+
for (char value : ca) {
14+
++freq[value - 'a'];
15+
}
16+
17+
ArrayList<String>[] cand = new ArrayList[8];
18+
cand[1] = new ArrayList<>();
19+
String ans = "";
20+
for (int i = 0; i < 26; i++) {
21+
if (freq[i] >= k) {
22+
ans = "" + (char) ('a' + i);
23+
cand[1].add(ans);
24+
}
25+
}
26+
for (int i = 2; i < 8; i++) {
27+
cand[i] = new ArrayList<>();
28+
for (String prev : cand[i - 1]) {
29+
for (String c : cand[1]) {
30+
String next = prev + c;
31+
if (isSubsequenceRepeatedK(ca, next, k)) {
32+
ans = next;
33+
cand[i].add(ans);
34+
}
35+
}
36+
}
37+
}
38+
return ans;
39+
}
40+
41+
private boolean isSubsequenceRepeatedK(char[] ca, String t, int k) {
42+
char[] ta = t.toCharArray();
43+
int n = ca.length;
44+
int m = ta.length;
45+
int i = 0;
46+
while (k-- > 0) {
47+
int j = 0;
48+
while (i < n && j < m) {
49+
if (ca[i] == ta[j]) {
50+
j++;
51+
}
52+
i++;
53+
}
54+
if (j != m) {
55+
return false;
56+
}
57+
}
58+
return true;
59+
}
60+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2014\. Longest Subsequence Repeated k Times
2+
3+
Hard
4+
5+
You are given a string `s` of length `n`, and an integer `k`. You are tasked to find the **longest subsequence repeated** `k` times in string `s`.
6+
7+
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.
8+
9+
A subsequence `seq` is **repeated** `k` times in the string `s` if `seq * k` is a subsequence of `s`, where `seq * k` represents a string constructed by concatenating `seq` `k` times.
10+
11+
* For example, `"bba"` is repeated `2` times in the string `"bababcba"`, because the string `"bbabba"`, constructed by concatenating `"bba"` `2` times, is a subsequence of the string `"**b**a**bab**c**ba**"`.
12+
13+
Return _the **longest subsequence repeated**_ `k` _times in string_ `s`_. If multiple such subsequences are found, return the **lexicographically largest** one. If there is no such subsequence, return an **empty** string_.
14+
15+
**Example 1:**
16+
17+
![example 1](https://assets.leetcode.com/uploads/2021/08/30/longest-subsequence-repeat-k-times.png)
18+
19+
**Input:** s = "letsleetcode", k = 2
20+
21+
**Output:** "let"
22+
23+
**Explanation:** There are two longest subsequences repeated 2 times: "let" and "ete". "let" is the lexicographically largest one.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "bb", k = 2
28+
29+
**Output:** "b"
30+
31+
**Explanation:** The longest subsequence repeated 2 times is "b".
32+
33+
**Example 3:**
34+
35+
**Input:** s = "ab", k = 2
36+
37+
**Output:** ""
38+
39+
**Explanation:** There is no subsequence repeated 2 times. Empty string is returned.
40+
41+
**Constraints:**
42+
43+
* `n == s.length`
44+
* `2 <= n, k <= 2000`
45+
* `2 <= n < k * 8`
46+
* `s` consists of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2012_sum_of_beauty_in_the_array;
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 sumOfBeauties() {
11+
assertThat(new Solution().sumOfBeauties(new int[] {1, 2, 3}), equalTo(2));
12+
}
13+
14+
@Test
15+
void sumOfBeauties2() {
16+
assertThat(new Solution().sumOfBeauties(new int[] {2, 4, 6, 4}), equalTo(1));
17+
}
18+
19+
@Test
20+
void sumOfBeauties3() {
21+
assertThat(new Solution().sumOfBeauties(new int[] {3, 2, 1}), equalTo(0));
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2001_2100.s2013_detect_squares;
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 DetectSquaresTest {
9+
@Test
10+
void detectSquaresTest() {
11+
DetectSquares detectSquares = new DetectSquares();
12+
detectSquares.add(new int[] {3, 10});
13+
detectSquares.add(new int[] {11, 2});
14+
detectSquares.add(new int[] {3, 2});
15+
assertThat(detectSquares.count(new int[] {11, 10}), equalTo(1));
16+
assertThat(detectSquares.count(new int[] {14, 8}), equalTo(0));
17+
detectSquares.add(new int[] {11, 2});
18+
assertThat(detectSquares.count(new int[] {11, 10}), equalTo(2));
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2014_longest_subsequence_repeated_k_times;
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 longestSubsequenceRepeatedK() {
11+
assertThat(new Solution().longestSubsequenceRepeatedK("letsleetcode", 2), equalTo("let"));
12+
}
13+
14+
@Test
15+
void longestSubsequenceRepeatedK2() {
16+
assertThat(new Solution().longestSubsequenceRepeatedK("bb", 2), equalTo("b"));
17+
}
18+
19+
@Test
20+
void longestSubsequenceRepeatedK3() {
21+
assertThat(new Solution().longestSubsequenceRepeatedK("ab", 2), equalTo(""));
22+
}
23+
}

0 commit comments

Comments
 (0)