Skip to content

Commit ce3dd87

Browse files
authored
Added tasks 492, 493, 495, 496, 497.
1 parent 254faa9 commit ce3dd87

File tree

15 files changed

+459
-0
lines changed

15 files changed

+459
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0401_0500.s0492_construct_the_rectangle;
2+
3+
// #Easy #Math
4+
5+
public class Solution {
6+
/*
7+
Algorithm:
8+
- start with an index i from the square root all the way to 1;
9+
- if at any time, area % i == 0 (so i is a divisor of area), then it's the closest solution.
10+
*/
11+
public int[] constructRectangle(int area) {
12+
int low = (int) Math.sqrt(area);
13+
while (low > 0) {
14+
if (area % low == 0) {
15+
return new int[] {area / low, low};
16+
}
17+
low--;
18+
}
19+
return new int[] {0, 0};
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
492\. Construct the Rectangle
2+
3+
Easy
4+
5+
A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
6+
7+
1. The area of the rectangular web page you designed must equal to the given target area.
8+
2. The width `W` should not be larger than the length `L`, which means `L >= W`.
9+
3. The difference between length `L` and width `W` should be as small as possible.
10+
11+
Return _an array `[L, W]` where `L` and `W` are the length and width of the web page you designed in sequence._
12+
13+
**Example 1:**
14+
15+
**Input:** area = 4
16+
17+
**Output:** [2,2]
18+
19+
**Explanation:** The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
20+
21+
**Example 2:**
22+
23+
**Input:** area = 37
24+
25+
**Output:** [37,1]
26+
27+
**Example 3:**
28+
29+
**Input:** area = 122122
30+
31+
**Output:** [427,286]
32+
33+
**Constraints:**
34+
35+
* <code>1 <= area <= 10<sup>7</sup></code>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0401_0500.s0493_reverse_pairs;
2+
3+
// #Hard #Array #Binary_Search #Ordered_Set #Divide_and_Conquer #Segment_Tree #Binary_Indexed_Tree
4+
// #Merge_Sort
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
// reference:
10+
// https://discuss.leetcode.com/topic/78933/very-short-and-clear-mergesort-bst-java-solutions
11+
public int reversePairs(int[] nums) {
12+
return mergeSort(nums, 0, nums.length - 1);
13+
}
14+
15+
private int mergeSort(int[] nums, int start, int end) {
16+
if (start >= end) {
17+
return 0;
18+
}
19+
int mid = start + (end - start) / 2;
20+
int cnt = mergeSort(nums, start, mid) + mergeSort(nums, mid + 1, end);
21+
for (int i = start; i <= mid; i++) {
22+
// it has to be 2.0 instead of 2, otherwise it's going to stack overflow, i.e. test3 is
23+
// going to fail
24+
int j = mid + 1;
25+
while (j <= end && nums[i] > nums[j] * 2.0) {
26+
j++;
27+
}
28+
cnt += j - (mid + 1);
29+
}
30+
Arrays.sort(nums, start, end + 1);
31+
return cnt;
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
493\. Reverse Pairs
2+
3+
Hard
4+
5+
Given an integer array `nums`, return _the number of **reverse pairs** in the array_.
6+
7+
A reverse pair is a pair `(i, j)` where `0 <= i < j < nums.length` and `nums[i] > 2 * nums[j]`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,2,3,1]
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [2,4,3,5,1]
18+
19+
**Output:** 3
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
24+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0495_teemo_attacking;
2+
3+
// #Easy #Array #Simulation
4+
5+
public class Solution {
6+
public int findPoisonedDuration(int[] timeSeries, int duration) {
7+
if (duration == 0) {
8+
return 0;
9+
}
10+
int start = timeSeries[0];
11+
int end = timeSeries[0] + duration - 1;
12+
int poisonDuration = end - start + 1;
13+
for (int i = 1; i < timeSeries.length; i++) {
14+
if (timeSeries[i] <= end) {
15+
poisonDuration += (duration - (end - timeSeries[i] + 1));
16+
end += (duration - (end - timeSeries[i] + 1));
17+
} else {
18+
start = timeSeries[i];
19+
end = timeSeries[i] + duration - 1;
20+
poisonDuration += end - start + 1;
21+
}
22+
}
23+
return poisonDuration;
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
495\. Teemo Attacking
2+
3+
Easy
4+
5+
Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly `duration` seconds. More formally, an attack at second `t` will mean Ashe is poisoned during the **inclusive** time interval `[t, t + duration - 1]`. If Teemo attacks again **before** the poison effect ends, the timer for it is **reset**, and the poison effect will end `duration` seconds after the new attack.
6+
7+
You are given a **non-decreasing** integer array `timeSeries`, where `timeSeries[i]` denotes that Teemo attacks Ashe at second `timeSeries[i]`, and an integer `duration`.
8+
9+
Return _the **total** number of seconds that Ashe is poisoned_.
10+
11+
**Example 1:**
12+
13+
**Input:** timeSeries = [1,4], duration = 2
14+
15+
**Output:** 4
16+
17+
**Explanation:** Teemo's attacks on Ashe go as follows: - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2. - At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5. Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.
18+
19+
**Example 2:**
20+
21+
**Input:** timeSeries = [1,2], duration = 2
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
Teemo's attacks on Ashe go as follows:
28+
29+
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
30+
31+
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3. Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= timeSeries.length <= 10<sup>4</sup></code>
36+
* <code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code>
37+
* `timeSeries` is sorted in **non-decreasing** order.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0401_0500.s0496_next_greater_element_i;
2+
3+
// #Easy #Array #Hash_Table #Stack #Monotonic_Stack
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
10+
Map<Integer, Integer> indexMap = new HashMap<>();
11+
for (int i = 0; i < nums2.length; i++) {
12+
indexMap.put(nums2[i], i);
13+
}
14+
15+
for (int i = 0; i < nums1.length; i++) {
16+
int num = nums1[i];
17+
int index = indexMap.get(num);
18+
if (index == nums2.length - 1) {
19+
nums1[i] = -1;
20+
} else {
21+
boolean found = false;
22+
while (index < nums2.length) {
23+
if (nums2[index] > num) {
24+
nums1[i] = nums2[index];
25+
found = true;
26+
break;
27+
}
28+
index++;
29+
}
30+
if (!found) {
31+
nums1[i] = -1;
32+
}
33+
}
34+
}
35+
36+
return nums1;
37+
}
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
496\. Next Greater Element I
2+
3+
Easy
4+
5+
The **next greater element** of some element `x` in an array is the **first greater** element that is **to the right** of `x` in the same array.
6+
7+
You are given two **distinct 0-indexed** integer arrays `nums1` and `nums2`, where `nums1` is a subset of `nums2`.
8+
9+
For each `0 <= i < nums1.length`, find the index `j` such that `nums1[i] == nums2[j]` and determine the **next greater element** of `nums2[j]` in `nums2`. If there is no next greater element, then the answer for this query is `-1`.
10+
11+
Return _an array_ `ans` _of length_ `nums1.length` _such that_ `ans[i]` _is the **next greater element** as described above._
12+
13+
**Example 1:**
14+
15+
**Input:** nums1 = [4,1,2], nums2 = [1,3,4,2]
16+
17+
**Output:** [-1,3,-1]
18+
19+
**Explanation:**
20+
21+
The next greater element for each value of nums1 is as follows:
22+
23+
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
24+
25+
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
26+
27+
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
28+
29+
**Example 2:**
30+
31+
**Input:** nums1 = [2,4], nums2 = [1,2,3,4]
32+
33+
**Output:** [3,-1]
34+
35+
**Explanation:**
36+
37+
The next greater element for each value of nums1 is as follows:
38+
39+
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
40+
41+
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
42+
43+
**Constraints:**
44+
45+
* `1 <= nums1.length <= nums2.length <= 1000`
46+
* <code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code>
47+
* All integers in `nums1` and `nums2` are **unique**.
48+
* All the integers of `nums1` also appear in `nums2`.
49+
50+
**Follow up:** Could you find an `O(nums1.length + nums2.length)` solution?
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0401_0500.s0497_random_point_in_non_overlapping_rectangles;
2+
3+
// #Medium #Math #Binary_Search #Prefix_Sum #Ordered_Set #Randomized #Reservoir_Sampling
4+
5+
import java.security.SecureRandom;
6+
7+
public class Solution {
8+
private final int[] weights;
9+
private final int[][] rects;
10+
private final SecureRandom random;
11+
12+
public Solution(int[][] rects) {
13+
this.weights = new int[rects.length];
14+
this.rects = rects;
15+
this.random = new SecureRandom();
16+
for (int i = 0; i < rects.length; i++) {
17+
int[] rect = rects[i];
18+
int count = (1 + rect[2] - rect[0]) * (1 + rect[3] - rect[1]);
19+
weights[i] = (i == 0 ? 0 : weights[i - 1]) + count;
20+
}
21+
}
22+
23+
public int[] pick() {
24+
int picked = 1 + random.nextInt(weights[weights.length - 1]);
25+
int idx = findGreaterOrEqual(picked);
26+
return getRandomPoint(idx);
27+
}
28+
29+
private int findGreaterOrEqual(int target) {
30+
int left = 0;
31+
int right = weights.length - 1;
32+
while (left + 1 < right) {
33+
int mid = left + (right - left) / 2;
34+
if (weights[mid] >= target) {
35+
right = mid;
36+
} else {
37+
left = mid + 1;
38+
}
39+
}
40+
return weights[left] >= target ? left : right;
41+
}
42+
43+
private int[] getRandomPoint(int idx) {
44+
int[] r = rects[idx];
45+
int left = r[0];
46+
int right = r[2];
47+
int bot = r[1];
48+
int top = r[3];
49+
return new int[] {
50+
left + random.nextInt(right - left + 1), bot + random.nextInt(top - bot + 1)
51+
};
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
497\. Random Point in Non-overlapping Rectangles
2+
3+
Medium
4+
5+
You are given an array of non-overlapping axis-aligned rectangles `rects` where <code>rects[i] = [a<sub>i</sub>, b<sub>i</sub>, x<sub>i</sub>, y<sub>i</sub>]</code> indicates that <code>(a<sub>i</sub>, b<sub>i</sub>)</code> is the bottom-left corner point of the <code>i<sup>th</sup></code> rectangle and <code>(x<sub>i</sub>, y<sub>i</sub>)</code> is the top-right corner point of the <code>i<sup>th</sup></code> rectangle. Design an algorithm to pick a random integer point inside the space covered by one of the given rectangles. A point on the perimeter of a rectangle is included in the space covered by the rectangle.
6+
7+
Any integer point inside the space covered by one of the given rectangles should be equally likely to be returned.
8+
9+
**Note** that an integer point is a point that has integer coordinates.
10+
11+
Implement the `Solution` class:
12+
13+
* `Solution(int[][] rects)` Initializes the object with the given rectangles `rects`.
14+
* `int[] pick()` Returns a random integer point `[u, v]` inside the space covered by one of the given rectangles.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2021/07/24/lc-pickrandomrec.jpg)
19+
20+
**Input** ["Solution", "pick", "pick", "pick", "pick", "pick"] [[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]
21+
22+
**Output:** [null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]
23+
24+
**Explanation:**
25+
26+
Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
27+
solution.pick(); // return [1, -2]
28+
solution.pick(); // return [1, -1]
29+
solution.pick(); // return [-1, -2]
30+
solution.pick(); // return [-2, -2]
31+
solution.pick(); // return [0, 0]
32+
33+
**Constraints:**
34+
35+
* `1 <= rects.length <= 100`
36+
* `rects[i].length == 4`
37+
* <code>-10<sup>9</sup> <= a<sub>i</sub> < x<sub>i</sub> <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= b<sub>i</sub> < y<sub>i</sub> <= 10<sup>9</sup></code>
39+
* <code>x<sub>i</sub> - a<sub>i</sub> <= 2000</code>
40+
* <code>y<sub>i</sub> - b<sub>i</sub> <= 2000</code>
41+
* All the rectangles do not overlap.
42+
* At most <code>10<sup>4</sup></code> calls will be made to `pick`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0401_0500.s0492_construct_the_rectangle;
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 constructRectangle() {
11+
assertThat(new Solution().constructRectangle(4), equalTo(new int[] {2, 2}));
12+
}
13+
14+
@Test
15+
void constructRectangle2() {
16+
assertThat(new Solution().constructRectangle(37), equalTo(new int[] {37, 1}));
17+
}
18+
19+
@Test
20+
void constructRectangle3() {
21+
assertThat(new Solution().constructRectangle(122122), equalTo(new int[] {427, 286}));
22+
}
23+
}

0 commit comments

Comments
 (0)