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

Commitfd1cee4

Browse files
authored
Added tasks 2874-2878
1 parentfeab657 commitfd1cee4

File tree

13 files changed

+376
-0
lines changed

13 files changed

+376
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packageg2801_2900.s2874_maximum_value_of_an_ordered_triplet_ii;
2+
3+
// #Medium #Array #2023_12_22_Time_2_ms_(99.67%)_Space_57.5_MB_(41.20%)
4+
5+
publicclassSolution {
6+
publiclongmaximumTripletValue(int[]nums) {
7+
int[]diff =newint[nums.length];
8+
inttempMax =nums[0];
9+
for (inti =1;i <diff.length -1;i++) {
10+
diff[i] =tempMax -nums[i];
11+
tempMax =Math.max(tempMax,nums[i]);
12+
}
13+
longmax =Long.MIN_VALUE;
14+
tempMax =nums[nums.length -1];
15+
for (inti =nums.length -2;i >0;i--) {
16+
max =Math.max(max, (long)tempMax *diff[i]);
17+
tempMax =Math.max(tempMax,nums[i]);
18+
}
19+
returnMath.max(max,0);
20+
}
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2874\. Maximum Value of an Ordered Triplet II
2+
3+
Medium
4+
5+
You are given a**0-indexed** integer array`nums`.
6+
7+
Return_**the maximum value over all triplets of indices**_`(i, j, k)`_such that_`i < j < k`_._ If all such triplets have a negative value, return`0`.
8+
9+
The**value of a triplet of indices**`(i, j, k)` is equal to`(nums[i] - nums[j]) * nums[k]`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[12,6,1,2,7]
14+
15+
**Output:** 77
16+
17+
**Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2])\* nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[1,10,3,4,19]
22+
23+
**Output:** 133
24+
25+
**Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2])\* nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133.
26+
27+
**Example 3:**
28+
29+
**Input:** nums =[1,2,3]
30+
31+
**Output:** 0
32+
33+
**Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1])\* nums[2] = -3. Hence, the answer would be 0.
34+
35+
**Constraints:**
36+
37+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageg2801_2900.s2875_minimum_size_subarray_in_infinite_array;
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #Sliding_Window
4+
// #2023_12_22_Time_6_ms_(87.63%)_Space_55.1_MB_(54.84%)
5+
6+
publicclassSolution {
7+
publicintminSizeSubarray(int[]nums,inttarget) {
8+
intsum =0;
9+
for (intnum :nums) {
10+
sum +=num;
11+
}
12+
if (sum ==0) {
13+
return -1;
14+
}
15+
intresult = (target /sum) *nums.length;
16+
sum =target %sum;
17+
intcurrentSum =0;
18+
intmin =nums.length;
19+
intstart =0;
20+
for (inti =0;i <nums.length *2;i++) {
21+
currentSum +=nums[i %nums.length];
22+
while (currentSum >sum) {
23+
currentSum -=nums[start %nums.length];
24+
start++;
25+
}
26+
if (currentSum ==sum) {
27+
min =Math.min(min,i -start +1);
28+
}
29+
}
30+
if (min ==nums.length) {
31+
return -1;
32+
}
33+
returnresult +min;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2875\. Minimum Size Subarray in Infinite Array
2+
3+
Medium
4+
5+
You are given a**0-indexed** array`nums` and an integer`target`.
6+
7+
A**0-indexed** array`infinite_nums` is generated by infinitely appending the elements of`nums` to itself.
8+
9+
Return_the length of the**shortest** subarray of the array_`infinite_nums`_with a sum equal to_`target`_._ If there is no such subarray return`-1`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[1,2,3], target = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** In this example infinite\_nums =[1,2,3,1,2,3,1,2,...]. The subarray in the range[1,2], has the sum equal to target = 5 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[1,1,1,2,3], target = 4
22+
23+
**Output:** 2
24+
25+
**Explanation:** In this example infinite\_nums =[1,1,1,2,3,1,1,1,2,3,1,1,...]. The subarray in the range[4,5], has the sum equal to target = 4 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
26+
27+
**Example 3:**
28+
29+
**Input:** nums =[2,4,6,8], target = 3
30+
31+
**Output:** -1
32+
33+
**Explanation:** In this example infinite\_nums =[2,4,6,8,2,4,6,8,...]. It can be proven that there is no subarray with sum equal to target = 3.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
39+
* <code>1 <= target <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageg2801_2900.s2876_count_visited_nodes_in_a_directed_graph;
2+
3+
// #Hard #Dynamic_Programming #Graph #Memoization
4+
// #2023_12_22_Time_36_ms_(93.33%)_Space_76.9_MB_(25.00%)
5+
6+
importjava.util.List;
7+
8+
publicclassSolution {
9+
publicint[]countVisitedNodes(List<Integer>edges) {
10+
intn =edges.size();
11+
boolean[]visited =newboolean[n];
12+
int[]ans =newint[n];
13+
int[]level =newint[n];
14+
for (inti =0;i <n;i++) {
15+
if (!visited[i]) {
16+
visit(edges,0,i,ans,visited,level);
17+
}
18+
}
19+
returnans;
20+
}
21+
22+
privateint[]visit(
23+
List<Integer>edges,intcount,intcurr,int[]ans,boolean[]visited,int[]level) {
24+
if (ans[curr] !=0) {
25+
returnnewint[] {-1,ans[curr]};
26+
}
27+
if (visited[curr]) {
28+
returnnewint[] {level[curr],count -level[curr]};
29+
}
30+
level[curr] =count;
31+
visited[curr] =true;
32+
int[]ret =visit(edges,count +1,edges.get(curr),ans,visited,level);
33+
if (ret[0] == -1 ||count <ret[0]) {
34+
ret[1]++;
35+
}
36+
ans[curr] =ret[1];
37+
returnret;
38+
}
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2876\. Count Visited Nodes in a Directed Graph
2+
3+
Hard
4+
5+
There is a**directed** graph consisting of`n` nodes numbered from`0` to`n - 1` and`n` directed edges.
6+
7+
You are given a**0-indexed** array`edges` where`edges[i]` indicates that there is an edge from node`i` to node`edges[i]`.
8+
9+
Consider the following process on the graph:
10+
11+
* You start from a node`x` and keep visiting other nodes through edges until you reach a node that you have already visited before on this**same** process.
12+
13+
Return_an array_`answer`_where_`answer[i]`_is the number of**different** nodes that you will visit if you perform the process starting from node_`i`.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png)
18+
19+
**Input:** edges =[1,2,0,0]
20+
21+
**Output:**[3,3,3,4]
22+
23+
**Explanation:** We perform the process starting from each node in the following way:
24+
- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
25+
- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
26+
- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
27+
- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png)
32+
33+
**Input:** edges =[1,2,3,4,0]
34+
35+
**Output:**[5,5,5,5,5]
36+
37+
**Explanation:** Starting from any node we can visit every node in the graph in the process.
38+
39+
**Constraints:**
40+
41+
*`n == edges.length`
42+
* <code>2 <= n <= 10<sup>5</sup></code>
43+
*`0 <= edges[i] <= n - 1`
44+
*`edges[i] != i`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2877\. Create a DataFrame from List
2+
3+
Easy
4+
5+
Write a solution to**create** a DataFrame from a 2D list called`student_data`. This 2D list contains the IDs and ages of some students.
6+
7+
The DataFrame should have two columns,`student_id` and`age`, and be in the same order as the original 2D list.
8+
9+
The result format is in the following example.
10+
11+
**Example 1:**
12+
13+
**Input:** student\_data:`[ [1, 15], [2, 11], [3, 11], [4, 20] ]`
14+
15+
**Output:**
16+
17+
+------------+-----+
18+
| student_id | age |
19+
+------------+-----+
20+
| 1 | 15 |
21+
| 2 | 11 |
22+
| 3 | 11 |
23+
| 4 | 20 |
24+
+------------+-----+
25+
26+
**Explanation:** A DataFrame was created on top of student\_data, with two columns named`student_id` and`age`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# #Easy #2023_12_22_Time_406_ms_(82.57%)_Space_59.2_MB_(81.15%)
2+
3+
importpandasaspd
4+
5+
defcreateDataframe(student_data:List[List[int]])->pd.DataFrame:
6+
column_name= ['student_id','age']
7+
result=pd.DataFrame(student_data,columns=column_name)
8+
returnresult
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2878\. Get the Size of a DataFrame
2+
3+
Easy
4+
5+
DataFrame`players:`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| player_id | int |
11+
| name | object |
12+
| age | int |
13+
| position | object |
14+
| ... | ... |
15+
+-------------+--------+
16+
17+
Write a solution to calculate and display the**number of rows and columns** of`players`.
18+
19+
Return the result as an array:
20+
21+
`[number of rows, number of columns]`
22+
23+
The result format is in the following example.
24+
25+
**Example 1:**
26+
27+
**Input:**
28+
29+
+-----------+----------+-----+-------------+--------------------+
30+
| player_id | name | age | position | team |
31+
+-----------+----------+-----+-------------+--------------------+
32+
| 846 | Mason | 21 | Forward | RealMadrid |
33+
| 749 | Riley | 30 | Winger | Barcelona |
34+
| 155 | Bob | 28 | Striker | ManchesterUnited |
35+
| 583 | Isabella | 32 | Goalkeeper | Liverpool |
36+
| 388 | Zachary | 24 | Midfielder | BayernMunich |
37+
| 883 | Ava | 23 | Defender | Chelsea |
38+
| 355 | Violet | 18 | Striker | Juventus |
39+
| 247 | Thomas | 27 | Striker | ParisSaint-Germain |
40+
| 761 | Jack | 33 | Midfielder | ManchesterCity |
41+
| 642 | Charlie | 36 | Center-back | Arsenal |
42+
+-----------+----------+-----+-------------+--------------------+
43+
44+
**Output:**[10, 5]
45+
46+
**Explanation:** This DataFrame contains 10 rows and 5 columns.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# #Easy #2023_12_22_Time_413_ms_(94.68%)_Space_59.9_MB_(74.79%)
2+
3+
importpandasaspd
4+
5+
defgetDataframeSize(players:pd.DataFrame)->List[int]:
6+
return[players.shape[0],players.shape[1]]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg2801_2900.s2874_maximum_value_of_an_ordered_triplet_ii;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidmaximumTripletValue() {
11+
assertThat(newSolution().maximumTripletValue(newint[] {12,6,1,2,7}),equalTo(77L));
12+
}
13+
14+
@Test
15+
voidmaximumTripletValue2() {
16+
assertThat(newSolution().maximumTripletValue(newint[] {1,10,3,4,19}),equalTo(133L));
17+
}
18+
19+
@Test
20+
voidmaximumTripletValue3() {
21+
assertThat(newSolution().maximumTripletValue(newint[] {1,2,3}),equalTo(0L));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageg2801_2900.s2875_minimum_size_subarray_in_infinite_array;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidminSizeSubarray() {
11+
assertThat(newSolution().minSizeSubarray(newint[] {1,2,3},5),equalTo(2));
12+
}
13+
14+
@Test
15+
voidminSizeSubarray2() {
16+
assertThat(newSolution().minSizeSubarray(newint[] {1,1,1,2,3},4),equalTo(2));
17+
}
18+
19+
@Test
20+
voidminSizeSubarray3() {
21+
assertThat(newSolution().minSizeSubarray(newint[] {2,4,6,8},3),equalTo(-1));
22+
}
23+
24+
@Test
25+
voidminSizeSubarray4() {
26+
assertThat(newSolution().minSizeSubarray(newint[] {0},1),equalTo(-1));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg2801_2900.s2876_count_visited_nodes_in_a_directed_graph;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importjava.util.Arrays;
7+
importorg.junit.jupiter.api.Test;
8+
9+
classSolutionTest {
10+
@Test
11+
voidcountVisitedNodes() {
12+
assertThat(
13+
newSolution().countVisitedNodes(Arrays.asList(1,2,0,0)),
14+
equalTo(newint[] {3,3,3,4}));
15+
}
16+
17+
@Test
18+
voidcountVisitedNodes2() {
19+
assertThat(
20+
newSolution().countVisitedNodes(Arrays.asList(1,2,3,4,0)),
21+
equalTo(newint[] {5,5,5,5,5}));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp