Movatterモバイル変換


[0]ホーム

URL:


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

Commit66334e6

Browse files
authored
Added tasks 2707-2711
1 parentd0d71a3 commit66334e6

File tree

15 files changed

+520
-0
lines changed

15 files changed

+520
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packageg2701_2800.s2707_extra_characters_in_a_string;
2+
3+
// #Medium #Array #String #Hash_Table #Dynamic_Programming #Trie
4+
// #2023_09_15_Time_37_ms_(74.40%)_Space_44.2_MB_(74.60%)
5+
6+
importjava.util.Arrays;
7+
8+
publicclassSolution {
9+
publicintminExtraChar(Strings,String[]dictionary) {
10+
returntabulationApproach(s,dictionary);
11+
}
12+
13+
privateinttabulationApproach(Strings,String[]dictionary) {
14+
intm =s.length();
15+
int[]dp =newint[m +1];
16+
for (inti =m -1;i >=0;i--) {
17+
dp[i] =dp[i +1] +1;
18+
intfinalI =i;
19+
Arrays.stream(dictionary)
20+
.filter(word ->s.startsWith(word,finalI))
21+
.mapToInt(String::length)
22+
.map(n ->dp[finalI +n])
23+
.forEach(prev ->dp[finalI] =Math.min(dp[finalI],prev));
24+
}
25+
returndp[0];
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2707\. Extra Characters in a String
2+
3+
Medium
4+
5+
You are given a**0-indexed** string`s` and a dictionary of words`dictionary`. You have to break`s` into one or more**non-overlapping** substrings such that each substring is present in`dictionary`. There may be some**extra characters** in`s` which are not present in any of the substrings.
6+
7+
Return_the**minimum** number of extra characters left over if you break up_`s`_optimally._
8+
9+
**Example 1:**
10+
11+
**Input:** s = "leetscode", dictionary =["leet","code","leetcode"]
12+
13+
**Output:** 1
14+
15+
**Explanation:** We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "sayhelloworld", dictionary =["hello","world"]
20+
21+
**Output:** 3
22+
23+
**Explanation:** We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
24+
25+
**Constraints:**
26+
27+
*`1 <= s.length <= 50`
28+
*`1 <= dictionary.length <= 50`
29+
*`1 <= dictionary[i].length <= 50`
30+
*`dictionary[i]` and`s` consists of only lowercase English letters
31+
*`dictionary` contains distinct words
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packageg2701_2800.s2708_maximum_strength_of_a_group;
2+
3+
// #Medium #Array #Sorting #Greedy #Backtracking
4+
// #2023_09_15_Time_2_ms_(85.08%)_Space_43.1_MB_(57.14%)
5+
6+
importjava.util.ArrayList;
7+
importjava.util.List;
8+
9+
publicclassSolution {
10+
publiclongmaxStrength(int[]nums) {
11+
List<Integer>filtered =newArrayList<>();
12+
longproduct =1L;
13+
booleanhasZero =false;
14+
for (intnum :nums) {
15+
if (num ==0) {
16+
hasZero =true;
17+
continue;
18+
}
19+
filtered.add(num);
20+
product *=num;
21+
}
22+
if (filtered.isEmpty()) {
23+
return0;
24+
}
25+
if (filtered.size() ==1 &&filtered.get(0) <=0) {
26+
returnhasZero ?0 :filtered.get(0);
27+
}
28+
longresult =product;
29+
for (intnum :nums) {
30+
if (num ==0) {
31+
continue;
32+
}
33+
result =Math.max(result,product /num);
34+
}
35+
returnresult;
36+
}
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2708\. Maximum Strength of a Group
2+
3+
Medium
4+
5+
You are given a**0-indexed** integer array`nums` representing the score of students in an exam. The teacher would like to form one**non-empty** group of students with maximal**strength**, where the strength of a group of students of indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> is defined as <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code>.
6+
7+
Return_the maximum strength of a group the teacher can create_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums =[3,-1,-5,2,5,-9]
12+
13+
**Output:** 1350
14+
15+
**Explanation:** One way to form a group of maximal strength is to group the students at indices[0,2,3,4,5]. Their strength is 3\* (-5)\* 2\* 5\* (-9) = 1350, which we can show is optimal.
16+
17+
**Example 2:**
18+
19+
**Input:** nums =[-4,-5,-4]
20+
21+
**Output:** 20
22+
23+
**Explanation:** Group the students at indices[0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
24+
25+
**Constraints:**
26+
27+
*`1 <= nums.length <= 13`
28+
*`-9 <= nums[i] <= 9`
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
packageg2701_2800.s2709_greatest_common_divisor_traversal;
2+
3+
// #Hard #Array #Math #Union_Find #Number_Theory
4+
// #2023_09_15_Time_244_ms_(64.71%)_Space_55.5_MB_(91.18%)
5+
6+
importjava.util.HashMap;
7+
importjava.util.Map;
8+
9+
publicclassSolution {
10+
privateMap<Integer,Integer>map =null;
11+
privateint[]set;
12+
13+
privateintfindParent(intu) {
14+
if (u ==set[u]) {
15+
returnu;
16+
}
17+
set[u] =findParent(set[u]);
18+
returnset[u];
19+
}
20+
21+
privatevoidunion(inta,intb) {
22+
intp1 =findParent(a);
23+
intp2 =findParent(b);
24+
if (p1 !=p2) {
25+
set[b] =p1;
26+
}
27+
set[p2] =p1;
28+
}
29+
30+
privatevoidsolve(intn,intindex) {
31+
if (n %2 ==0) {
32+
intx =map.getOrDefault(2, -1);
33+
if (x != -1) {
34+
union(x,index);
35+
}
36+
while (n %2 ==0) {
37+
n /=2;
38+
}
39+
map.put(2,index);
40+
}
41+
intsqrt = (int)Math.sqrt(n);
42+
for (inti =3;i <=sqrt;i++) {
43+
if (n %i ==0) {
44+
intx =map.getOrDefault(i, -1);
45+
if (x != -1) {
46+
union(x,index);
47+
}
48+
while (n %i ==0) {
49+
n /=i;
50+
}
51+
map.put(i,index);
52+
}
53+
}
54+
if (n >2) {
55+
intx =map.getOrDefault(n, -1);
56+
if (x != -1) {
57+
union(x,index);
58+
}
59+
map.put(n,index);
60+
}
61+
}
62+
63+
publicbooleancanTraverseAllPairs(int[]nums) {
64+
set =newint[nums.length];
65+
map =newHashMap<>();
66+
for (inti =0;i <nums.length;i++) {
67+
set[i] =i;
68+
}
69+
for (inti =0;i <nums.length;i++) {
70+
solve(nums[i],i);
71+
}
72+
intp =findParent(0);
73+
for (inti =0;i <nums.length;i++) {
74+
if (p !=findParent(i)) {
75+
returnfalse;
76+
}
77+
}
78+
returntrue;
79+
}
80+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2709\. Greatest Common Divisor Traversal
2+
3+
Hard
4+
5+
You are given a**0-indexed** integer array`nums`, and you are allowed to**traverse** between its indices. You can traverse between index`i` and index`j`,`i != j`, if and only if`gcd(nums[i], nums[j]) > 1`, where`gcd` is the**greatest common divisor**.
6+
7+
Your task is to determine if for**every pair** of indices`i` and`j` in nums, where`i < j`, there exists a**sequence of traversals** that can take us from`i` to`j`.
8+
9+
Return`true`_if it is possible to traverse between all such pairs of indices,__or_`false`_otherwise._
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[2,3,6]
14+
15+
**Output:** true
16+
17+
**Explanation:** In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2). To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1. To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[3,9,5]
22+
23+
**Output:** false
24+
25+
**Explanation:** No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
26+
27+
**Example 3:**
28+
29+
**Input:** nums =[4,3,12,8]
30+
31+
**Output:** true
32+
33+
**Explanation:** There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
packageg2701_2800.s2710_remove_trailing_zeros_from_a_string;
2+
3+
// #Easy #String #2023_09_15_Time_1_ms_(100.00%)_Space_43.5_MB_(80.00%)
4+
5+
publicclassSolution {
6+
publicStringremoveTrailingZeros(Stringnum) {
7+
intendIndex =num.length() -1;
8+
while (endIndex >=0 &&num.charAt(endIndex) =='0') {
9+
endIndex--;
10+
}
11+
returnnum.substring(0,endIndex +1);
12+
}
13+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2710\. Remove Trailing Zeros From a String
2+
3+
Easy
4+
5+
Given a**positive** integer`num` represented as a string, return_the integer_`num`_without trailing zeros as a string_.
6+
7+
**Example 1:**
8+
9+
**Input:** num = "51230100"
10+
11+
**Output:** "512301"
12+
13+
**Explanation:** Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301".
14+
15+
**Example 2:**
16+
17+
**Input:** num = "123"
18+
19+
**Output:** "123"
20+
21+
**Explanation:** Integer "123" has no trailing zeros, we return integer "123".
22+
23+
**Constraints:**
24+
25+
*`1 <= num.length <= 1000`
26+
*`num` consists of only digits.
27+
*`num` doesn't have any leading zeros.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
packageg2701_2800.s2711_difference_of_number_of_distinct_values_on_diagonals;
2+
3+
// #Medium #Array #Hash_Table #Matrix #2023_09_15_Time_7_ms_(90.98%)_Space_44.3_MB_(93.44%)
4+
5+
importjava.util.HashSet;
6+
importjava.util.Set;
7+
8+
publicclassSolution {
9+
publicint[][]differenceOfDistinctValues(int[][]grid) {
10+
intm =grid.length;
11+
intn =grid[0].length;
12+
int[][]arrTopLeft =newint[m][n];
13+
int[][]arrBotRight =newint[m][n];
14+
15+
for (inti =m -1;i >=0;i--) {
16+
intc =0;
17+
intr =i;
18+
Set<Integer>set =newHashSet<>();
19+
while (cellExists(r,c,grid)) {
20+
arrTopLeft[r][c] =set.size();
21+
set.add(grid[r++][c++]);
22+
}
23+
}
24+
25+
for (inti =1;i <n;i++) {
26+
intr =0;
27+
intc =i;
28+
Set<Integer>set =newHashSet<>();
29+
while (cellExists(r,c,grid)) {
30+
arrTopLeft[r][c] =set.size();
31+
set.add(grid[r++][c++]);
32+
}
33+
}
34+
35+
for (inti =0;i <n;i++) {
36+
intr =m -1;
37+
intc =i;
38+
Set<Integer>set =newHashSet<>();
39+
while (cellExists(r,c,grid)) {
40+
arrBotRight[r][c] =set.size();
41+
set.add(grid[r--][c--]);
42+
}
43+
}
44+
45+
for (inti =m -1;i >=0;i--) {
46+
intc =n -1;
47+
intr =i;
48+
Set<Integer>set =newHashSet<>();
49+
while (cellExists(r,c,grid)) {
50+
arrBotRight[r][c] =set.size();
51+
set.add(grid[r--][c--]);
52+
}
53+
}
54+
55+
int[][]result =newint[m][n];
56+
for (intr =0;r <m;r++) {
57+
for (intc =0;c <n;c++) {
58+
result[r][c] =Math.abs(arrTopLeft[r][c] -arrBotRight[r][c]);
59+
}
60+
}
61+
62+
returnresult;
63+
}
64+
65+
privatebooleancellExists(intr,intc,int[][]grid) {
66+
returnr >=0 &&r <grid.length &&c >=0 &&c <grid[0].length;
67+
}
68+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp