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

Commit63f7aee

Browse files
authored
Added tasks 2544, 2545
1 parent6e17d93 commit63f7aee

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.21'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2545 |[Sort the Students by Their Kth Score](src/main/java/g2501_2600/s2545_sort_the_students_by_their_kth_score/Solution.java)| Medium | Array, Sorting, Matrix | 1 | 99.50
1852+
| 2544 |[Alternating Digit Sum](src/main/java/g2501_2600/s2544_alternating_digit_sum/Solution.java)| Easy | Math | 0 | 100.00
18511853
| 2543 |[Check if Point Is Reachable](src/main/java/g2501_2600/s2543_check_if_point_is_reachable/Solution.java)| Hard | Math, Number_Theory | 0 | 100.00
18521854
| 2542 |[Maximum Subsequence Score](src/main/java/g2501_2600/s2542_maximum_subsequence_score/Solution.java)| Medium | Array, Sorting, Greedy, Heap_(Priority_Queue) | 94 | 84.75
18531855
| 2541 |[Minimum Operations to Make Array Equal II](src/main/java/g2501_2600/s2541_minimum_operations_to_make_array_equal_ii/Solution.java)| Medium | Array, Math, Greedy | 3 | 100.00
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packageg2501_2600.s2544_alternating_digit_sum;
2+
3+
// #Easy #Math #2023_05_11_Time_0_ms_(100.00%)_Space_39.4_MB_(79.49%)
4+
5+
publicclassSolution {
6+
publicintalternateDigitSum(intn) {
7+
Strings =Integer.toString(n);
8+
char[]arr =s.toCharArray();
9+
intres =0;
10+
for (inti =0;i <arr.length;i++) {
11+
res += (int)Math.pow(-1,i) * (arr[i] -'0');
12+
}
13+
returnres;
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2544\. Alternating Digit Sum
2+
3+
Easy
4+
5+
You are given a positive integer`n`. Each digit of`n` has a sign according to the following rules:
6+
7+
* The**most significant digit** is assigned a**positive** sign.
8+
* Each other digit has an opposite sign to its adjacent digits.
9+
10+
Return_the sum of all digits with their corresponding sign_.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 521
15+
16+
**Output:** 4
17+
18+
**Explanation:** (+5) + (-2) + (+1) = 4.
19+
20+
**Example 2:**
21+
22+
**Input:** n = 111
23+
24+
**Output:** 1
25+
26+
**Explanation:** (+1) + (-1) + (+1) = 1.
27+
28+
**Example 3:**
29+
30+
**Input:** n = 886996
31+
32+
**Output:** 0
33+
34+
**Explanation:** (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= n <= 10<sup>9</sup></code>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packageg2501_2600.s2545_sort_the_students_by_their_kth_score;
2+
3+
// #Medium #Array #Sorting #Matrix #2023_05_11_Time_1_ms_(99.50%)_Space_53.2_MB_(6.86%)
4+
5+
importjava.util.Arrays;
6+
7+
publicclassSolution {
8+
publicint[][]sortTheStudents(int[][]score,intk) {
9+
Arrays.sort(score, (o1,o2) ->o2[k] -o1[k]);
10+
returnscore;
11+
}
12+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2545\. Sort the Students by Their Kth Score
2+
3+
Medium
4+
5+
There is a class with`m` students and`n` exams. You are given a**0-indexed**`m x n` integer matrix`score`, where each row represents one student and`score[i][j]` denotes the score the <code>i<sup>th</sup></code> student got in the <code>j<sup>th</sup></code> exam. The matrix`score` contains**distinct** integers only.
6+
7+
You are also given an integer`k`. Sort the students (i.e., the rows of the matrix) by their scores in the <code>k<sup>th</sup></code> (**0-indexed**) exam from the highest to the lowest.
8+
9+
Return_the matrix after sorting it._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2022/11/30/example1.png)
14+
15+
**Input:** score =[[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2
16+
17+
**Output:**[[7,5,11,2],[10,6,9,1],[4,8,3,15]]
18+
19+
**Explanation:** In the above diagram, S denotes the student, while E denotes the exam.
20+
21+
- The student with index 1 scored 11 in exam 2, which is the highest score, so they got first place.
22+
23+
- The student with index 0 scored 9 in exam 2, which is the second highest score, so they got second place.
24+
25+
- The student with index 2 scored 3 in exam 2, which is the lowest score, so they got third place.
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2022/11/30/example2.png)
30+
31+
**Input:** score =[[3,4],[5,6]], k = 0
32+
33+
**Output:**[[5,6],[3,4]]
34+
35+
**Explanation:** In the above diagram, S denotes the student, while E denotes the exam.
36+
37+
- The student with index 1 scored 5 in exam 0, which is the highest score, so they got first place.
38+
39+
- The student with index 0 scored 3 in exam 0, which is the lowest score, so they got second place.
40+
41+
**Constraints:**
42+
43+
*`m == score.length`
44+
*`n == score[i].length`
45+
*`1 <= m, n <= 250`
46+
* <code>1 <= score[i][j] <= 10<sup>5</sup></code>
47+
*`score` consists of**distinct** integers.
48+
*`0 <= k < n`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg2501_2600.s2544_alternating_digit_sum;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidalternateDigitSum() {
11+
assertThat(newSolution().alternateDigitSum(521),equalTo(4));
12+
}
13+
14+
@Test
15+
voidalternateDigitSum2() {
16+
assertThat(newSolution().alternateDigitSum(111),equalTo(1));
17+
}
18+
19+
@Test
20+
voidalternateDigitSum3() {
21+
assertThat(newSolution().alternateDigitSum(886996),equalTo(0));
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packageg2501_2600.s2545_sort_the_students_by_their_kth_score;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidsortTheStudents() {
11+
assertThat(
12+
newSolution()
13+
.sortTheStudents(
14+
newint[][] {{10,6,9,1}, {7,5,11,2}, {4,8,3,15}},2),
15+
equalTo(newint[][] {{7,5,11,2}, {10,6,9,1}, {4,8,3,15}}));
16+
}
17+
18+
@Test
19+
voidsortTheStudents2() {
20+
assertThat(
21+
newSolution().sortTheStudents(newint[][] {{3,4}, {5,6}},0),
22+
equalTo(newint[][] {{5,6}, {3,4}}));
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp