Skip to content

Commit 63f7aee

Browse files
authored
Added tasks 2544, 2545
1 parent 6e17d93 commit 63f7aee

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+
package g2501_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+
public class Solution {
6+
public int alternateDigitSum(int n) {
7+
String s = Integer.toString(n);
8+
char[] arr = s.toCharArray();
9+
int res = 0;
10+
for (int i = 0; i < arr.length; i++) {
11+
res += (int) Math.pow(-1, i) * (arr[i] - '0');
12+
}
13+
return res;
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+
package g2501_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+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[][] sortTheStudents(int[][] score, int k) {
9+
Arrays.sort(score, (o1, o2) -> o2[k] - o1[k]);
10+
return score;
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+
package g2501_2600.s2544_alternating_digit_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void alternateDigitSum() {
11+
assertThat(new Solution().alternateDigitSum(521), equalTo(4));
12+
}
13+
14+
@Test
15+
void alternateDigitSum2() {
16+
assertThat(new Solution().alternateDigitSum(111), equalTo(1));
17+
}
18+
19+
@Test
20+
void alternateDigitSum3() {
21+
assertThat(new Solution().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+
package g2501_2600.s2545_sort_the_students_by_their_kth_score;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void sortTheStudents() {
11+
assertThat(
12+
new Solution()
13+
.sortTheStudents(
14+
new int[][] {{10, 6, 9, 1}, {7, 5, 11, 2}, {4, 8, 3, 15}}, 2),
15+
equalTo(new int[][] {{7, 5, 11, 2}, {10, 6, 9, 1}, {4, 8, 3, 15}}));
16+
}
17+
18+
@Test
19+
void sortTheStudents2() {
20+
assertThat(
21+
new Solution().sortTheStudents(new int[][] {{3, 4}, {5, 6}}, 0),
22+
equalTo(new int[][] {{5, 6}, {3, 4}}));
23+
}
24+
}

0 commit comments

Comments
 (0)