Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d26fe8

Browse files
Added tasks 2930-2934
1 parent bf047de commit 9d26fe8

File tree

15 files changed

+618
-0
lines changed

15 files changed

+618
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2901_3000.s2930_number_of_strings_which_can_be_rearranged_to_contain_substring;
2+
3+
// #Medium #Dynamic_Programming #Math #Combinatorics
4+
// #2024_01_02_Time_1_ms_(80.56%)_Space_40.8_MB_(66.67%)
5+
6+
public class Solution {
7+
private long pow(long x, long n, long mod) {
8+
long result = 1;
9+
long p = x % mod;
10+
while (n != 0) {
11+
if ((n & 1) != 0) {
12+
result = (result * p) % mod;
13+
}
14+
p = (p * p) % mod;
15+
n >>= 1;
16+
}
17+
return result;
18+
}
19+
20+
public int stringCount(int n) {
21+
long mod = (int) 1e9 + 7L;
22+
return (int)
23+
(((+pow(26, n, mod)
24+
- (n + 75) * pow(25, n - 1L, mod)
25+
+ (2 * n + 72) * pow(24, n - 1L, mod)
26+
- (n + 23) * pow(23, n - 1L, mod))
27+
% mod
28+
+ mod)
29+
% mod);
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2930\. Number of Strings Which Can Be Rearranged to Contain Substring
2+
3+
Medium
4+
5+
You are given an integer `n`.
6+
7+
A string `s` is called **good** if it contains only lowercase English characters **and** it is possible to rearrange the characters of `s` such that the new string contains `"leet"` as a **substring**.
8+
9+
For example:
10+
11+
* The string `"lteer"` is good because we can rearrange it to form `"leetr"` .
12+
* `"letl"` is not good because we cannot rearrange it to contain `"leet"` as a substring.
13+
14+
Return _the **total** number of good strings of length_ `n`.
15+
16+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
A **substring** is a contiguous sequence of characters within a string.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 4
23+
24+
**Output:** 12
25+
26+
**Explanation:** The 12 strings which can be rearranged to have "leet" as a substring are: "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
27+
28+
**Example 2:**
29+
30+
**Input:** n = 10
31+
32+
**Output:** 83943898
33+
34+
**Explanation:** The number of strings with length 10 which can be rearranged to have "leet" as a substring is 526083947580. Hence the answer is 526083947580 % (10<sup>9</sup> + 7) = 83943898.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n <= 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 g2901_3000.s2931_maximum_spending_after_buying_items;
2+
3+
// #Hard #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue
4+
// #2024_01_02_Time_18_ms_(79.00%)_Space_55.4_MB_(78.52%)
5+
6+
public class Solution {
7+
private static class Node {
8+
int val = -1;
9+
Node next = null;
10+
11+
public Node(int val) {
12+
this.val = val;
13+
}
14+
15+
public Node() {}
16+
}
17+
18+
public long maxSpending(int[][] values) {
19+
int m = values.length;
20+
int n = values[0].length;
21+
Node head = new Node();
22+
Node node = head;
23+
for (int j = n - 1; j >= 0; j--) {
24+
node.next = new Node(values[0][j]);
25+
node = node.next;
26+
}
27+
for (int i = 1; i < m; i++) {
28+
node = head;
29+
for (int j = n - 1; j >= 0; j--) {
30+
while (node.next != null && node.next.val <= values[i][j]) {
31+
node = node.next;
32+
}
33+
Node next = node.next;
34+
node.next = new Node(values[i][j]);
35+
node = node.next;
36+
node.next = next;
37+
}
38+
}
39+
long res = 0;
40+
long day = 1;
41+
node = head.next;
42+
while (node != null) {
43+
res += day * node.val;
44+
node = node.next;
45+
day++;
46+
}
47+
return res;
48+
}
49+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2931\. Maximum Spending After Buying Items
2+
3+
Hard
4+
5+
You are given a **0-indexed** `m * n` integer matrix `values`, representing the values of `m * n` different items in `m` different shops. Each shop has `n` items where the <code>j<sup>th</sup></code> item in the <code>i<sup>th</sup></code> shop has a value of `values[i][j]`. Additionally, the items in the <code>i<sup>th</sup></code> shop are sorted in non-increasing order of value. That is, `values[i][j] >= values[i][j + 1]` for all `0 <= j < n - 1`.
6+
7+
On each day, you would like to buy a single item from one of the shops. Specifically, On the <code>d<sup>th</sup></code> day you can:
8+
9+
* Pick any shop `i`.
10+
* Buy the rightmost available item `j` for the price of `values[i][j] * d`. That is, find the greatest index `j` such that item `j` was never bought before, and buy it for the price of `values[i][j] * d`.
11+
12+
**Note** that all items are pairwise different. For example, if you have bought item `0` from shop `1`, you can still buy item `0` from any other shop.
13+
14+
Return _the **maximum amount of money that can be spent** on buying all_ `m * n` _products_.
15+
16+
**Example 1:**
17+
18+
**Input:** values = [[8,5,2],[6,4,1],[9,7,3]]
19+
20+
**Output:** 285
21+
22+
**Explanation:** On the first day, we buy product 2 from shop 1 for a price of values[1][2] \* 1 = 1.
23+
24+
On the second day, we buy product 2 from shop 0 for a price of values[0][2] \* 2 = 4.
25+
26+
On the third day, we buy product 2 from shop 2 for a price of values[2][2] \* 3 = 9.
27+
28+
On the fourth day, we buy product 1 from shop 1 for a price of values[1][1] \* 4 = 16.
29+
30+
On the fifth day, we buy product 1 from shop 0 for a price of values[0][1] \* 5 = 25.
31+
32+
On the sixth day, we buy product 0 from shop 1 for a price of values[1][0] \* 6 = 36.
33+
34+
On the seventh day, we buy product 1 from shop 2 for a price of values[2][1] \* 7 = 49.
35+
36+
On the eighth day, we buy product 0 from shop 0 for a price of values[0][0] \* 8 = 64.
37+
38+
On the ninth day, we buy product 0 from shop 2 for a price of values[2][0] \* 9 = 81.
39+
40+
Hence, our total spending is equal to 285.
41+
42+
It can be shown that 285 is the maximum amount of money that can be spent buying all m \* n products.
43+
44+
**Example 2:**
45+
46+
**Input:** values = [[10,8,6,4,2],[9,7,5,3,2]]
47+
48+
**Output:** 386
49+
50+
**Explanation:** On the first day, we buy product 4 from shop 0 for a price of values[0][4] \* 1 = 2.
51+
52+
On the second day, we buy product 4 from shop 1 for a price of values[1][4] \* 2 = 4.
53+
54+
On the third day, we buy product 3 from shop 1 for a price of values[1][3] \* 3 = 9.
55+
56+
On the fourth day, we buy product 3 from shop 0 for a price of values[0][3] \* 4 = 16.
57+
58+
On the fifth day, we buy product 2 from shop 1 for a price of values[1][2] \* 5 = 25.
59+
60+
On the sixth day, we buy product 2 from shop 0 for a price of values[0][2] \* 6 = 36.
61+
62+
On the seventh day, we buy product 1 from shop 1 for a price of values[1][1] \* 7 = 49.
63+
64+
On the eighth day, we buy product 1 from shop 0 for a price of values[0][1] \* 8 = 64
65+
66+
On the ninth day, we buy product 0 from shop 1 for a price of values[1][0] \* 9 = 81.
67+
68+
On the tenth day, we buy product 0 from shop 0 for a price of values[0][0] \* 10 = 100.
69+
70+
Hence, our total spending is equal to 386.
71+
72+
It can be shown that 386 is the maximum amount of money that can be spent buying all m \* n products.
73+
74+
**Constraints:**
75+
76+
* `1 <= m == values.length <= 10`
77+
* <code>1 <= n == values[i].length <= 10<sup>4</sup></code>
78+
* <code>1 <= values[i][j] <= 10<sup>6</sup></code>
79+
* `values[i]` are sorted in non-increasing order.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2901_3000.s2932_maximum_strong_pair_xor_i;
2+
3+
// #Easy #Array #Hash_Table #Bit_Manipulation #Sliding_Window #Trie
4+
// #2024_01_02_Time_2_ms_(98.64%)_Space_43.1_MB_(59.18%)
5+
6+
public class Solution {
7+
public int maximumStrongPairXor(int[] nums) {
8+
int max = 0;
9+
int pair = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
for (int j = i; j < nums.length; j++) {
12+
if (Math.abs(nums[i] - nums[j]) <= Math.min(nums[i], nums[j])) {
13+
pair = nums[i] ^ nums[j];
14+
max = Math.max(max, pair);
15+
}
16+
}
17+
}
18+
return max;
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2932\. Maximum Strong Pair XOR I
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. A pair of integers `x` and `y` is called a **strong** pair if it satisfies the condition:
6+
7+
* `|x - y| <= min(x, y)`
8+
9+
You need to select two integers from `nums` such that they form a strong pair and their bitwise `XOR` is the **maximum** among all strong pairs in the array.
10+
11+
Return _the **maximum**_ `XOR` _value out of all possible strong pairs in the array_ `nums`.
12+
13+
**Note** that you can pick the same integer twice to form a pair.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3,4,5]
18+
19+
**Output:** 7
20+
21+
**Explanation:** There are 11 strong pairs in the array `nums`: (1, 1), (1, 2), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (3, 5), (4, 4), (4, 5) and (5, 5). The maximum XOR possible from these pairs is 3 XOR 4 = 7.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [10,100]
26+
27+
**Output:** 0
28+
29+
**Explanation:** There are 2 strong pairs in the array `nums`: (10, 10) and (100, 100). The maximum XOR possible from these pairs is 10 XOR 10 = 0 since the pair (100, 100) also gives 100 XOR 100 = 0.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [5,6,25,30]
34+
35+
**Output:** 7
36+
37+
**Explanation:** There are 6 strong pairs in the array `nums`: (5, 5), (5, 6), (6, 6), (25, 25), (25, 30) and (30, 30). The maximum XOR possible from these pairs is 25 XOR 30 = 7 since the only other non-zero XOR value is 5 XOR 6 = 3.
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 50`
42+
* `1 <= nums[i] <= 100`
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g2901_3000.s2933_high_access_employees;
2+
3+
// #Medium #Array #String #Hash_Table #Sorting #2024_01_02_Time_9_ms_(87.94%)_Space_45.6_MB_(5.79%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class Solution {
12+
private boolean isPossible(int a, int b) {
13+
int hb = b / 100;
14+
int ha = a / 100;
15+
int mind = b % 100;
16+
int mina = a % 100;
17+
if (hb == 23 && ha == 0) {
18+
return false;
19+
}
20+
if (hb - ha > 1) {
21+
return false;
22+
}
23+
if (hb - ha == 1) {
24+
mind += 60;
25+
}
26+
return mind - mina < 60;
27+
}
28+
29+
private boolean isHighAccess(List<Integer> list) {
30+
if (list.size() < 3) {
31+
return false;
32+
}
33+
int i = 0;
34+
int j = 1;
35+
int k = 2;
36+
while (k < list.size()) {
37+
int a = list.get(i++);
38+
int b = list.get(j++);
39+
int c = list.get(k++);
40+
if (isPossible(a, c) && isPossible(b, c) && isPossible(a, b)) {
41+
return true;
42+
}
43+
}
44+
return false;
45+
}
46+
47+
private int stringToInt(String str) {
48+
int i = 1000;
49+
int val = 0;
50+
for (char ch : str.toCharArray()) {
51+
int n = ch - '0';
52+
val += i * n;
53+
i = i / 10;
54+
}
55+
return val;
56+
}
57+
58+
public List<String> findHighAccessEmployees(List<List<String>> accessTimes) {
59+
HashMap<String, List<Integer>> map = new HashMap<>();
60+
for (List<String> list : accessTimes) {
61+
List<Integer> temp = map.getOrDefault(list.get(0), new ArrayList<>());
62+
int val = stringToInt(list.get(1));
63+
temp.add(val);
64+
map.put(list.get(0), temp);
65+
}
66+
List<String> ans = new ArrayList<>();
67+
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
68+
List<Integer> temp = entry.getValue();
69+
Collections.sort(temp);
70+
if (isHighAccess(temp)) {
71+
ans.add(entry.getKey());
72+
}
73+
}
74+
return ans;
75+
}
76+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2933\. High-Access Employees
2+
3+
Medium
4+
5+
You are given a 2D **0-indexed** array of strings, `access_times`, with size `n`. For each `i` where `0 <= i <= n - 1`, `access_times[i][0]` represents the name of an employee, and `access_times[i][1]` represents the access time of that employee. All entries in `access_times` are within the same day.
6+
7+
The access time is represented as **four digits** using a **24-hour** time format, for example, `"0800"` or `"2250"`.
8+
9+
An employee is said to be **high-access** if he has accessed the system **three or more** times within a **one-hour period**.
10+
11+
Times with exactly one hour of difference are **not** considered part of the same one-hour period. For example, `"0815"` and `"0915"` are not part of the same one-hour period.
12+
13+
Access times at the start and end of the day are **not** counted within the same one-hour period. For example, `"0005"` and `"2350"` are not part of the same one-hour period.
14+
15+
Return _a list that contains the names of **high-access** employees with any order you want._
16+
17+
**Example 1:**
18+
19+
**Input:** access\_times = [["a","0549"],["b","0457"],["a","0532"],["a","0621"],["b","0540"]]
20+
21+
**Output:** ["a"]
22+
23+
**Explanation:** "a" has three access times in the one-hour period of [05:32, 06:31] which are 05:32, 05:49, and 06:21. But "b" does not have more than two access times at all. So the answer is ["a"].
24+
25+
**Example 2:**
26+
27+
**Input:** access\_times = [["d","0002"],["c","0808"],["c","0829"],["e","0215"],["d","1508"],["d","1444"],["d","1410"],["c","0809"]]
28+
29+
**Output:** ["c","d"]
30+
31+
**Explanation:** "c" has three access times in the one-hour period of [08:08, 09:07] which are 08:08, 08:09, and 08:29. "d" has also three access times in the one-hour period of [14:10, 15:09] which are 14:10, 14:44, and 15:08. However, "e" has just one access time, so it can not be in the answer and the final answer is ["c","d"].
32+
33+
**Example 3:**
34+
35+
**Input:** access\_times = [["cd","1025"],["ab","1025"],["cd","1046"],["cd","1055"],["ab","1124"],["ab","1120"]]
36+
37+
**Output:** ["ab","cd"]
38+
39+
**Explanation:** "ab" has three access times in the one-hour period of [10:25, 11:24] which are 10:25, 11:20, and 11:24. "cd" has also three access times in the one-hour period of [10:25, 11:24] which are 10:25, 10:46, and 10:55. So the answer is ["ab","cd"].
40+
41+
**Constraints:**
42+
43+
* `1 <= access_times.length <= 100`
44+
* `access_times[i].length == 2`
45+
* `1 <= access_times[i][0].length <= 10`
46+
* `access_times[i][0]` consists only of English small letters.
47+
* `access_times[i][1].length == 4`
48+
* `access_times[i][1]` is in 24-hour time format.
49+
* `access_times[i][1]` consists only of `'0'` to `'9'`.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /