Skip to content

Commit ac79a0c

Browse files
authored
Added tasks 1861, 1862.
1 parent 6f8d2de commit ac79a0c

File tree

6 files changed

+232
-0
lines changed

6 files changed

+232
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g1801_1900.s1861_rotating_the_box;
2+
3+
// #Medium #Array #Matrix #Two_Pointers #2022_05_08_Time_16_ms_(30.81%)_Space_77.6_MB_(95.11%)
4+
5+
public class Solution {
6+
public char[][] rotateTheBox(char[][] box) {
7+
int m = box.length;
8+
int n = box[0].length;
9+
for (int i = 0; i < m; i++) {
10+
for (int j = n - 1; j >= 0; j--) {
11+
if (box[i][j] == '#') {
12+
int empty = j + 1;
13+
while (empty < n && box[i][empty] == '.') {
14+
empty++;
15+
}
16+
if (empty < n && box[i][empty] == '.') {
17+
box[i][empty] = '#';
18+
box[i][j] = '.';
19+
} else if (empty - 1 < n && box[i][empty - 1] == '.') {
20+
box[i][empty - 1] = '#';
21+
box[i][j] = '.';
22+
}
23+
}
24+
}
25+
}
26+
char[][] result = new char[n][m];
27+
int k = m - 1;
28+
for (int i = 0; i < m; i++) {
29+
for (int j = 0; j < n; j++) {
30+
result[j][k] = box[i][j];
31+
}
32+
k--;
33+
}
34+
return result;
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
1861\. Rotating the Box
2+
3+
Medium
4+
5+
You are given an `m x n` matrix of characters `box` representing a side-view of a box. Each cell of the box is one of the following:
6+
7+
* A stone `'#'`
8+
* A stationary obstacle `'*'`
9+
* Empty `'.'`
10+
11+
The box is rotated **90 degrees clockwise**, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity **does not** affect the obstacles' positions, and the inertia from the box's rotation **does not** affect the stones' horizontal positions.
12+
13+
It is **guaranteed** that each stone in `box` rests on an obstacle, another stone, or the bottom of the box.
14+
15+
Return _an_ `n x m` _matrix representing the box after the rotation described above_.
16+
17+
**Example 1:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcodewithstones.png)
20+
21+
**Input:** box = [["#",".","#"]]
22+
23+
**Output:** [["."],
24+
["#"],
25+
["#"]]
26+
27+
**Example 2:**
28+
29+
![](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode2withstones.png)
30+
31+
**Input:** box = [["#",".","*","."],
32+
["#","#","*","."]]
33+
34+
**Output:** [["#","."],
35+
["#","#"],
36+
["*","*"],
37+
[".","."]]
38+
39+
**Example 3:**
40+
41+
![](https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode3withstone.png)
42+
43+
**Input:** box = [["#","#","*",".","*","."],
44+
["#","#","#","*",".","."],
45+
["#","#","#",".","#","."]]
46+
47+
**Output:** [[".","#","#"],
48+
[".","#","#"],
49+
["#","#","*"],
50+
["#","*","."],
51+
["#",".","*"],
52+
["#",".","."]]
53+
54+
**Constraints:**
55+
56+
* `m == box.length`
57+
* `n == box[i].length`
58+
* `1 <= m, n <= 500`
59+
* `box[i][j]` is either `'#'`, `'*'`, or `'.'`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g1801_1900.s1862_sum_of_floored_pairs;
2+
3+
// #Hard #Array #Math #Binary_Search #Prefix_Sum
4+
// #2022_05_08_Time_115_ms_(70.91%)_Space_57.2_MB_(81.82%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int sumOfFlooredPairs(int[] nums) {
10+
long mod = 1000000007;
11+
Arrays.sort(nums);
12+
int max = nums[nums.length - 1];
13+
int[] counts = new int[max + 1];
14+
long[] qnts = new long[max + 1];
15+
for (int k : nums) {
16+
counts[k]++;
17+
}
18+
for (int i = 1; i < max + 1; i++) {
19+
if (counts[i] == 0) {
20+
continue;
21+
}
22+
int j = i;
23+
while (j <= max) {
24+
qnts[j] += counts[i];
25+
j = j + i;
26+
}
27+
}
28+
for (int i = 1; i < max + 1; i++) {
29+
qnts[i] = (qnts[i] + qnts[i - 1]) % mod;
30+
}
31+
long sum = 0;
32+
for (int k : nums) {
33+
sum = (sum + qnts[k]) % mod;
34+
}
35+
return (int) sum;
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1862\. Sum of Floored Pairs
2+
3+
Hard
4+
5+
Given an integer array `nums`, return the sum of `floor(nums[i] / nums[j])` for all pairs of indices `0 <= i, j < nums.length` in the array. Since the answer may be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
The `floor()` function returns the integer part of the division.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [2,5,9]
12+
13+
**Output:** 10
14+
15+
**Explanation:**
16+
17+
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
18+
19+
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
20+
21+
floor(5 / 2) = 2
22+
23+
floor(9 / 2) = 4
24+
25+
floor(9 / 5) = 1
26+
27+
We calculate the floor of the division for every pair of indices in the array then sum them up.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [7,7,7,7,7,7,7]
32+
33+
**Output:** 49
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g1801_1900.s1861_rotating_the_box;
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 rotateTheBox() {
11+
assertThat(
12+
new Solution().rotateTheBox(new char[][] {{'#', '.', '#'}}),
13+
equalTo(new char[][] {{'.'}, {'#'}, {'#'}}));
14+
}
15+
16+
@Test
17+
void rotateTheBox2() {
18+
assertThat(
19+
new Solution()
20+
.rotateTheBox(new char[][] {{'#', '.', '*', '.'}, {'#', '#', '*', '.'}}),
21+
equalTo(new char[][] {{'#', '.'}, {'#', '#'}, {'*', '*'}, {'.', '.'}}));
22+
}
23+
24+
@Test
25+
void rotateTheBox3() {
26+
assertThat(
27+
new Solution()
28+
.rotateTheBox(
29+
new char[][] {
30+
{'#', '#', '*', '.', '*', '.'},
31+
{'#', '#', '#', '*', '.', '.'},
32+
{'#', '#', '#', '.', '#', '.'}
33+
}),
34+
equalTo(
35+
new char[][] {
36+
{'.', '#', '#'},
37+
{'.', '#', '#'},
38+
{'#', '#', '*'},
39+
{'#', '*', '.'},
40+
{'#', '.', '*'},
41+
{'#', '.', '.'}
42+
}));
43+
}
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1801_1900.s1862_sum_of_floored_pairs;
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 sumOfFlooredPairs() {
11+
assertThat(new Solution().sumOfFlooredPairs(new int[] {2, 5, 9}), equalTo(10));
12+
}
13+
14+
@Test
15+
void sumOfFlooredPairs2() {
16+
assertThat(new Solution().sumOfFlooredPairs(new int[] {7, 7, 7, 7, 7, 7, 7}), equalTo(49));
17+
}
18+
}

0 commit comments

Comments
 (0)