Skip to content

Commit af6cd7c

Browse files
authored
Added tasks 2194, 2195.
1 parent 880a263 commit af6cd7c

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.10'
13671367
|------|----------------|-------------|-------------|----------|---------
13681368
| 2246 |[Longest Path With Different Adjacent Characters](src/main/java/g2201_2300/s2246_longest_path_with_different_adjacent_characters/Solution.java)| Hard | Array, String, Depth_First_Search, Tree, Graph, Topological_Sort | 75 | 97.79
13691369
| 2200 |[Find All K-Distant Indices in an Array](src/main/java/g2101_2200/s2200_find_all_k_distant_indices_in_an_array/Solution.java)| Easy | Array | 2 | 95.30
1370+
| 2195 |[Append K Integers With Minimal Sum](src/main/java/g2101_2200/s2195_append_k_integers_with_minimal_sum/Solution.java)| Medium | Array, Math, Sorting, Greedy | 19 | 96.88
1371+
| 2194 |[Cells in a Range on an Excel Sheet](src/main/java/g2101_2200/s2194_cells_in_a_range_on_an_excel_sheet/Solution.java)| Easy | String | 1 | 99.92
13701372
| 2193 |[Minimum Number of Moves to Make Palindrome](src/main/java/g2101_2200/s2193_minimum_number_of_moves_to_make_palindrome/Solution.java)| Hard | String, Greedy, Two_Pointers, Binary_Indexed_Tree | 8 | 98.76
13711373
| 2187 |[Minimum Time to Complete Trips](src/main/java/g2101_2200/s2187_minimum_time_to_complete_trips/Solution.java)| Medium | Array, Binary_Search | 187 | 95.03
13721374
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros/Solution.java)| Medium | Simulation, Linked_List | 6 | 96.26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2101_2200.s2194_cells_in_a_range_on_an_excel_sheet;
2+
3+
// #Easy #String #2022_06_06_Time_1_ms_(99.92%)_Space_42.6_MB_(99.23%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public List<String> cellsInRange(String str) {
10+
char[] c = str.toCharArray();
11+
List<String> strings = new ArrayList<>();
12+
for (char i = c[0]; i <= c[3]; i++) {
13+
for (char j = c[1]; j <= c[4]; j++) {
14+
strings.add(new String(new char[] {i, j}));
15+
}
16+
}
17+
return strings;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2194\. Cells in a Range on an Excel Sheet
2+
3+
Easy
4+
5+
A cell `(r, c)` of an excel sheet is represented as a string `"<col><row>"` where:
6+
7+
* `<col>` denotes the column number `c` of the cell. It is represented by **alphabetical letters**.
8+
* For example, the <code>1<sup>st</sup></code> column is denoted by `'A'`, the <code>2<sup>nd</sup></code> by `'B'`, the <code>3<sup>rd</sup></code> by `'C'`, and so on.
9+
* `<row>` is the row number `r` of the cell. The <code>r<sup>th</sup></code> row is represented by the **integer** `r`.
10+
11+
You are given a string `s` in the format `"<col1><row1>:<col2><row2>"`, where `<col1>` represents the column `c1`, `<row1>` represents the row `r1`, `<col2>` represents the column `c2`, and `<row2>` represents the row `r2`, such that `r1 <= r2` and `c1 <= c2`.
12+
13+
Return _the **list of cells**_ `(x, y)` _such that_ `r1 <= x <= r2` _and_ `c1 <= y <= c2`. The cells should be represented as **strings** in the format mentioned above and be sorted in **non-decreasing** order first by columns and then by rows.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png)
18+
19+
**Input:** s = "K1:L2"
20+
21+
**Output:** ["K1","K2","L1","L2"]
22+
23+
**Explanation:**
24+
25+
The above diagram shows the cells which should be present in the list.
26+
27+
The red arrows denote the order in which the cells should be presented.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png)
32+
33+
**Input:** s = "A1:F1"
34+
35+
**Output:** ["A1","B1","C1","D1","E1","F1"]
36+
37+
**Explanation:**
38+
39+
The above diagram shows the cells which should be present in the list.
40+
41+
The red arrow denotes the order in which the cells should be presented.
42+
43+
**Constraints:**
44+
45+
* `s.length == 5`
46+
* `'A' <= s[0] <= s[3] <= 'Z'`
47+
* `'1' <= s[1] <= s[4] <= '9'`
48+
* `s` consists of uppercase English letters, digits and `':'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2101_2200.s2195_append_k_integers_with_minimal_sum;
2+
3+
// #Medium #Array #Math #Sorting #Greedy #2022_06_06_Time_19_ms_(96.88%)_Space_60_MB_(78.44%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long minimalKSum(int[] nums, int k) {
9+
Arrays.sort(nums);
10+
long sum = 0;
11+
int n = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
if (i == 0 || nums[i] != nums[i - 1]) {
14+
if (nums[i] - n > k) {
15+
break;
16+
}
17+
sum += nums[i];
18+
n++;
19+
}
20+
}
21+
long t = n + (long) k;
22+
return (1 + t) * t / 2 - sum;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2195\. Append K Integers With Minimal Sum
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `k`. Append `k` **unique positive** integers that do **not** appear in `nums` to `nums` such that the resulting total sum is **minimum**.
6+
7+
Return _the sum of the_ `k` _integers appended to_ `nums`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,4,25,10,25], k = 2
12+
13+
**Output:** 5
14+
15+
**Explanation:** The two unique positive integers that do not appear in nums which we append are 2 and 3.
16+
17+
The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
18+
19+
The sum of the two integers appended is 2 + 3 = 5, so we return 5.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [5,6], k = 6
24+
25+
**Output:** 25
26+
27+
**Explanation:** The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
28+
29+
The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum.
30+
31+
The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
37+
* <code>1 <= k <= 10<sup>8</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2101_2200.s2194_cells_in_a_range_on_an_excel_sheet;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.util.Arrays;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SolutionTest {
10+
@Test
11+
void cellsInRange() {
12+
assertThat(
13+
new Solution().cellsInRange("K1:L2"),
14+
equalTo(Arrays.asList("K1", "K2", "L1", "L2")));
15+
}
16+
17+
@Test
18+
void cellsInRange2() {
19+
assertThat(
20+
new Solution().cellsInRange("A1:F1"),
21+
equalTo(Arrays.asList("A1", "B1", "C1", "D1", "E1", "F1")));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2195_append_k_integers_with_minimal_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 minimalKSum() {
11+
assertThat(new Solution().minimalKSum(new int[] {1, 4, 25, 10, 25}, 2), equalTo(5L));
12+
}
13+
14+
@Test
15+
void minimalKSum2() {
16+
assertThat(new Solution().minimalKSum(new int[] {5, 6}, 6), equalTo(25L));
17+
}
18+
}

0 commit comments

Comments
 (0)