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

Commit4832093

Browse files
authored
Added tasks 2889-2901
1 parent5f2a1b4 commit4832093

File tree

12 files changed

+391
-0
lines changed

12 files changed

+391
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2889\. Reshape Data: Pivot
2+
3+
Easy
4+
5+
DataFrame`weather`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| city | object |
11+
| month | object |
12+
| temperature | int |
13+
+-------------+--------+
14+
15+
Write a solution to**pivot** the data so that each row represents temperatures for a specific month, and each city is a separate column.
16+
17+
The result format is in the following example.
18+
19+
**Example 1:****Input:**
20+
21+
+--------------+----------+-------------+
22+
| city | month | temperature |
23+
+--------------+----------+-------------+
24+
| Jacksonville | January | 13 |
25+
| Jacksonville | February | 23 |
26+
| Jacksonville | March | 38 |
27+
| Jacksonville | April | 5 |
28+
| Jacksonville | May | 34 |
29+
| ElPaso | January | 20 |
30+
| ElPaso | February | 6 |
31+
| ElPaso | March | 26 |
32+
| ElPaso | April | 2 |
33+
| ElPaso | May | 43 |
34+
+--------------+----------+-------------+
35+
36+
**Output:**`
37+
38+
+----------+--------+--------------+
39+
| month | ElPaso | Jacksonville |
40+
+----------+--------+--------------+
41+
| April | 2 | 5 |
42+
| February | 6 | 23 |
43+
| January | 20 | 13 |
44+
| March | 26 | 38 |
45+
| May | 43 | 34 |
46+
+----------+--------+--------------+`
47+
48+
**Explanation:** The table is pivoted, each column represents a city, and each row represents a specific month.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# #Easy #2023_12_25_Time_416_ms_(99.87%)_Space_61.8_MB_(21.28%)
2+
3+
importpandasaspd
4+
5+
defpivotTable(weather:pd.DataFrame)->pd.DataFrame:
6+
returnweather.pivot(index='month',columns='city',values='temperature')
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2890\. Reshape Data: Melt
2+
3+
Easy
4+
5+
DataFrame`report`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| product | object |
11+
| quarter_1 | int |
12+
| quarter_2 | int |
13+
| quarter_3 | int |
14+
| quarter_4 | int |
15+
+-------------+--------+
16+
17+
Write a solution to**reshape** the data so that each row represents sales data for a product in a specific quarter.
18+
19+
The result format is in the following example.
20+
21+
**Example 1:**
22+
23+
**Input:**
24+
25+
+-------------+-----------+-----------+-----------+-----------+
26+
| product | quarter_1 | quarter_2 | quarter_3 | quarter_4 |
27+
+-------------+-----------+-----------+-----------+-----------+
28+
| Umbrella | 417 | 224 | 379 | 611 |
29+
| SleepingBag | 800 | 936 | 93 | 875 |
30+
+-------------+-----------+-----------+-----------+-----------+
31+
32+
**Output:**
33+
34+
+-------------+-----------+-------+
35+
| product | quarter | sales |
36+
+-------------+-----------+-------+
37+
| Umbrella | quarter_1 | 417 |
38+
| SleepingBag | quarter_1 | 800 |
39+
| Umbrella | quarter_2 | 224 |
40+
| SleepingBag | quarter_2 | 936 |
41+
| Umbrella | quarter_3 | 379 |
42+
| SleepingBag | quarter_3 | 93 |
43+
| Umbrella | quarter_4 | 611 |
44+
| SleepingBag | quarter_4 | 875 |
45+
+-------------+-----------+-------+
46+
47+
**Explanation:** The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# #Easy #2023_12_25_Time_446_ms_(97.89%)_Space_60.5_MB_(68.02%)
2+
3+
importpandasaspd
4+
5+
defmeltTable(report:pd.DataFrame)->pd.DataFrame:
6+
returnreport.melt(id_vars='product',var_name='quarter',value_name='sales')
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2891\. Method Chaining
2+
3+
Easy
4+
5+
DataFrame`animals`
6+
7+
+-------------+--------+
8+
| Column Name | Type |
9+
+-------------+--------+
10+
| name | object |
11+
| species | object |
12+
| age | int |
13+
| weight | int |
14+
+-------------+--------+
15+
16+
Write a solution to list the names of animals that weigh**strictly more than**`100` kilograms.
17+
18+
Return the animals sorted by weight in**descending order**.
19+
20+
The result format is in the following example.
21+
22+
**Example 1:**
23+
24+
**Input:** DataFrame animals:
25+
26+
+----------+---------+-----+--------+
27+
| name | species | age | weight |
28+
+----------+---------+-----+--------+
29+
| Tatiana | Snake | 98 | 464 |
30+
| Khaled | Giraffe | 50 | 41 |
31+
| Alex | Leopard | 6 | 328 |
32+
| Jonathan | Monkey | 45 | 463 |
33+
| Stefan | Bear | 100 | 50 |
34+
| Tommy | Panda | 26 | 349 |
35+
+----------+---------+-----+--------+
36+
37+
**Output:**
38+
39+
+----------+
40+
| name |
41+
+----------+
42+
| Tatiana |
43+
| Jonathan |
44+
| Tommy |
45+
| Alex |
46+
+----------+
47+
48+
**Explanation:** All animals weighing more than 100 should be included in the results table. Tatiana's weight is 464, Jonathan's weight is 463, Tommy's weight is 349, and Alex's weight is 328. The results should be sorted in descending order of weight.
49+
50+
In Pandas,**method chaining** enables us to perform operations on a DataFrame without breaking up each operation into a separate line or creating multiple temporary variables.
51+
52+
Can you complete this task in just**one line** of code using method chaining?
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# #Easy #2023_12_25_Time_412_ms_(99.23%)_Space_60.8_MB_(50.69%)
2+
3+
importpandasaspd
4+
5+
deffindHeavyAnimals(animals:pd.DataFrame)->pd.DataFrame:
6+
animal_data= {}
7+
forindexinanimals.index:
8+
animal=animals.iloc[index]
9+
ifanimal['weight']>100:
10+
animal_data[animal['name']]=animal['weight']
11+
12+
animal_data=dict(sorted(animal_data.items() ,key=lambdax :x[1] ,reverse=True))
13+
result=pd.DataFrame(animal_data.keys() ,columns= ['name'])
14+
returnresult
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packageg2801_2900.s2894_divisible_and_non_divisible_sums_difference;
2+
3+
// #Easy #Math #2023_12_25_Time_1_ms_(92.30%)_Space_40.7_MB_(7.01%)
4+
5+
publicclassSolution {
6+
publicintdifferenceOfSums(intn,intm) {
7+
intsum1 =0;
8+
intsum2 =0;
9+
for (inti =1;i <=n;i++) {
10+
if (i %m ==0) {
11+
sum1 +=i;
12+
}else {
13+
sum2 +=i;
14+
}
15+
}
16+
returnsum2 -sum1;
17+
}
18+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2894\. Divisible and Non-divisible Sums Difference
2+
3+
Easy
4+
5+
You are given positive integers`n` and`m`.
6+
7+
Define two integers,`num1` and`num2`, as follows:
8+
9+
*`num1`: The sum of all integers in the range`[1, n]` that are**not divisible** by`m`.
10+
*`num2`: The sum of all integers in the range`[1, n]` that are**divisible** by`m`.
11+
12+
Return_the integer_`num1 - num2`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 10, m = 3
17+
18+
**Output:** 19
19+
20+
**Explanation:** In the given example:
21+
- Integers in the range[1, 10] that are not divisible by 3 are[1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
22+
- Integers in the range[1, 10] that are divisible by 3 are[3,6,9], num2 is the sum of those integers = 18. We return 37 - 18 = 19 as the answer.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 5, m = 6
27+
28+
**Output:** 15
29+
30+
**Explanation:** In the given example:
31+
- Integers in the range[1, 5] that are not divisible by 6 are[1,2,3,4,5], num1 is the sum of those integers = 15.
32+
- Integers in the range[1, 5] that are divisible by 6 are[], num2 is the sum of those integers = 0. We return 15 - 0 = 15 as the answer.
33+
34+
**Example 3:**
35+
36+
**Input:** n = 5, m = 1
37+
38+
**Output:** -15
39+
40+
**Explanation:** In the given example:
41+
- Integers in the range[1, 5] that are not divisible by 1 are[], num1 is the sum of those integers = 0.
42+
- Integers in the range[1, 5] that are divisible by 1 are[1,2,3,4,5], num2 is the sum of those integers = 15. We return 0 - 15 = -15 as the answer.
43+
44+
**Constraints:**
45+
46+
*`1 <= n, m <= 1000`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
packageg2901_3000.s2901_longest_unequal_adjacent_groups_subsequence_ii;
2+
3+
// #Medium #Array #String #Dynamic_Programming #2023_12_25_Time_40_ms_(92.26%)_Space_45_MB_(41.07%)
4+
5+
importjava.util.ArrayList;
6+
importjava.util.Arrays;
7+
importjava.util.Collections;
8+
importjava.util.List;
9+
10+
publicclassSolution {
11+
publicList<String>getWordsInLongestSubsequence(intn,String[]words,int[]groups) {
12+
int[]check =newint[groups.length];
13+
int[]before =newint[groups.length];
14+
Arrays.fill(check,1);
15+
Arrays.fill(before, -1);
16+
intindex =0;
17+
intmax =1;
18+
for (inti =1;i <n;i++) {
19+
for (intj =i -1;j >=0;j--) {
20+
if (groups[i] !=groups[j] &&ham(words[i],words[j]) &&check[j] +1 >check[i]) {
21+
check[i] =check[j] +1;
22+
before[i] =j;
23+
if (check[i] >max) {
24+
max =check[i];
25+
index =i;
26+
}
27+
}
28+
}
29+
}
30+
List<String>ans =newArrayList<>();
31+
while (index >=0) {
32+
ans.add(words[index]);
33+
index =before[index];
34+
}
35+
Collections.reverse(ans);
36+
returnans;
37+
}
38+
39+
privatebooleanham(Strings1,Strings2) {
40+
if (s1.length() !=s2.length()) {
41+
returnfalse;
42+
}
43+
intcount =0;
44+
for (inti =0;i <s1.length();i++) {
45+
if (s1.charAt(i) !=s2.charAt(i)) {
46+
count++;
47+
}
48+
if (count >1) {
49+
returnfalse;
50+
}
51+
}
52+
returncount ==1;
53+
}
54+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2901\. Longest Unequal Adjacent Groups Subsequence II
2+
3+
Medium
4+
5+
You are given an integer`n`, a**0-indexed** string array`words`, and a**0-indexed** array`groups`, both arrays having length`n`.
6+
7+
The**hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are**different**.
8+
9+
You need to select the**longest****subsequence** from an array of indices`[0, 1, ..., n - 1]`, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length`k`, the following holds:
10+
11+
* For**adjacent** indices in the subsequence, their corresponding groups are**unequal**, i.e., <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each`j` where`0 < j + 1 < k`.
12+
* <code>words[i<sub>j</sub>]</code> and <code>words[i<sub>j + 1</sub>]</code> are**equal** in length, and the**hamming distance** between them is`1`, where`0 < j + 1 < k`, for all indices in the subsequence.
13+
14+
Return_a string array containing the words corresponding to the indices**(in order)** in the selected subsequence_. If there are multiple answers, return_any of them_.
15+
16+
A**subsequence** of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
17+
18+
**Note:** strings in`words` may be**unequal** in length.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 3, words =["bab","dab","cab"], groups =[1,2,2]
23+
24+
**Output:**["bab","cab"]
25+
26+
**Explanation:** A subsequence that can be selected is[0,2].
27+
- groups[0] != groups[2]
28+
- words[0].length == words[2].length, and the hamming distance between them is 1.
29+
30+
So, a valid answer is[words[0],words[2]] =["bab","cab"]. Another subsequence that can be selected is[0,1].
31+
- groups[0] != groups[1]
32+
- words[0].length == words[1].length, and the hamming distance between them is 1.
33+
34+
So, another valid answer is[words[0],words[1]] =["bab","dab"]. It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
35+
36+
**Example 2:**
37+
38+
**Input:** n = 4, words =["a","b","c","d"], groups =[1,2,3,4]
39+
40+
**Output:**["a","b","c","d"]
41+
42+
**Explanation:** We can select the subsequence[0,1,2,3]. It satisfies both conditions. Hence, the answer is[words[0],words[1],words[2],words[3]] =["a","b","c","d"]. It has the longest length among all subsequences of indices that satisfy the conditions. Hence, it is the only answer.
43+
44+
**Constraints:**
45+
46+
*`1 <= n == words.length == groups.length <= 1000`
47+
*`1 <= words[i].length <= 10`
48+
*`1 <= groups[i] <= n`
49+
*`words` consists of**distinct** strings.
50+
*`words[i]` consists of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg2801_2900.s2894_divisible_and_non_divisible_sums_difference;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voiddifferenceOfSums() {
11+
assertThat(newSolution().differenceOfSums(10,3),equalTo(19));
12+
}
13+
14+
@Test
15+
voiddifferenceOfSums2() {
16+
assertThat(newSolution().differenceOfSums(5,6),equalTo(15));
17+
}
18+
19+
@Test
20+
voiddifferenceOfSums3() {
21+
assertThat(newSolution().differenceOfSums(5,1),equalTo(-15));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp