Skip to content

Tasks 492 493 495 496 497 #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0401_0500.s0492_construct_the_rectangle;

// #Easy #Math

public class Solution {
/*
Algorithm:
- start with an index i from the square root all the way to 1;
- if at any time, area % i == 0 (so i is a divisor of area), then it's the closest solution.
*/
public int[] constructRectangle(int area) {
int low = (int) Math.sqrt(area);
while (low > 0) {
if (area % low == 0) {
return new int[] {area / low, low};
}
low--;
}
return new int[] {0, 0};
}
}
35 changes: 35 additions & 0 deletions src/main/java/g0401_0500/s0492_construct_the_rectangle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
492\. Construct the Rectangle

Easy

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:

1. The area of the rectangular web page you designed must equal to the given target area.
2. The width `W` should not be larger than the length `L`, which means `L >= W`.
3. The difference between length `L` and width `W` should be as small as possible.

Return _an array `[L, W]` where `L` and `W` are the length and width of the web page you designed in sequence._

**Example 1:**

**Input:** area = 4

**Output:** [2,2]

**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.

**Example 2:**

**Input:** area = 37

**Output:** [37,1]

**Example 3:**

**Input:** area = 122122

**Output:** [427,286]

**Constraints:**

* <code>1 <= area <= 10<sup>7</sup></code>
33 changes: 33 additions & 0 deletions src/main/java/g0401_0500/s0493_reverse_pairs/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g0401_0500.s0493_reverse_pairs;

// #Hard #Array #Binary_Search #Ordered_Set #Divide_and_Conquer #Segment_Tree #Binary_Indexed_Tree
// #Merge_Sort

import java.util.Arrays;

public class Solution {
// reference:
// https://discuss.leetcode.com/topic/78933/very-short-and-clear-mergesort-bst-java-solutions
public int reversePairs(int[] nums) {
return mergeSort(nums, 0, nums.length - 1);
}

private int mergeSort(int[] nums, int start, int end) {
if (start >= end) {
return 0;
}
int mid = start + (end - start) / 2;
int cnt = mergeSort(nums, start, mid) + mergeSort(nums, mid + 1, end);
for (int i = start; i <= mid; i++) {
// it has to be 2.0 instead of 2, otherwise it's going to stack overflow, i.e. test3 is
// going to fail
int j = mid + 1;
while (j <= end && nums[i] > nums[j] * 2.0) {
j++;
}
cnt += j - (mid + 1);
}
Arrays.sort(nums, start, end + 1);
return cnt;
}
}
24 changes: 24 additions & 0 deletions src/main/java/g0401_0500/s0493_reverse_pairs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
493\. Reverse Pairs

Hard

Given an integer array `nums`, return _the number of **reverse pairs** in the array_.

A reverse pair is a pair `(i, j)` where `0 <= i < j < nums.length` and `nums[i] > 2 * nums[j]`.

**Example 1:**

**Input:** nums = [1,3,2,3,1]

**Output:** 2

**Example 2:**

**Input:** nums = [2,4,3,5,1]

**Output:** 3

**Constraints:**

* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
25 changes: 25 additions & 0 deletions src/main/java/g0401_0500/s0495_teemo_attacking/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g0401_0500.s0495_teemo_attacking;

// #Easy #Array #Simulation

public class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
if (duration == 0) {
return 0;
}
int start = timeSeries[0];
int end = timeSeries[0] + duration - 1;
int poisonDuration = end - start + 1;
for (int i = 1; i < timeSeries.length; i++) {
if (timeSeries[i] <= end) {
poisonDuration += (duration - (end - timeSeries[i] + 1));
end += (duration - (end - timeSeries[i] + 1));
} else {
start = timeSeries[i];
end = timeSeries[i] + duration - 1;
poisonDuration += end - start + 1;
}
}
return poisonDuration;
}
}
37 changes: 37 additions & 0 deletions src/main/java/g0401_0500/s0495_teemo_attacking/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
495\. Teemo Attacking

Easy

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.

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`.

Return _the **total** number of seconds that Ashe is poisoned_.

**Example 1:**

**Input:** timeSeries = [1,4], duration = 2

**Output:** 4

**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.

**Example 2:**

**Input:** timeSeries = [1,2], duration = 2

**Output:** 3

**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 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.

**Constraints:**

* <code>1 <= timeSeries.length <= 10<sup>4</sup></code>
* <code>0 <= timeSeries[i], duration <= 10<sup>7</sup></code>
* `timeSeries` is sorted in **non-decreasing** order.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g0401_0500.s0496_next_greater_element_i;

// #Easy #Array #Hash_Table #Stack #Monotonic_Stack

import java.util.HashMap;
import java.util.Map;

public class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums2.length; i++) {
indexMap.put(nums2[i], i);
}

for (int i = 0; i < nums1.length; i++) {
int num = nums1[i];
int index = indexMap.get(num);
if (index == nums2.length - 1) {
nums1[i] = -1;
} else {
boolean found = false;
while (index < nums2.length) {
if (nums2[index] > num) {
nums1[i] = nums2[index];
found = true;
break;
}
index++;
}
if (!found) {
nums1[i] = -1;
}
}
}

return nums1;
}
}
50 changes: 50 additions & 0 deletions src/main/java/g0401_0500/s0496_next_greater_element_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
496\. Next Greater Element I

Easy

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.

You are given two **distinct 0-indexed** integer arrays `nums1` and `nums2`, where `nums1` is a subset of `nums2`.

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`.

Return _an array_ `ans` _of length_ `nums1.length` _such that_ `ans[i]` _is the **next greater element** as described above._

**Example 1:**

**Input:** nums1 = [4,1,2], nums2 = [1,3,4,2]

**Output:** [-1,3,-1]

**Explanation:**

The next greater element for each value of nums1 is as follows:

- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.

- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.

- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.

**Example 2:**

**Input:** nums1 = [2,4], nums2 = [1,2,3,4]

**Output:** [3,-1]

**Explanation:**

The next greater element for each value of nums1 is as follows:

- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.

- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.

**Constraints:**

* `1 <= nums1.length <= nums2.length <= 1000`
* <code>0 <= nums1[i], nums2[i] <= 10<sup>4</sup></code>
* All integers in `nums1` and `nums2` are **unique**.
* All the integers of `nums1` also appear in `nums2`.

**Follow up:** Could you find an `O(nums1.length + nums2.length)` solution?
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package g0401_0500.s0497_random_point_in_non_overlapping_rectangles;

// #Medium #Math #Binary_Search #Prefix_Sum #Ordered_Set #Randomized #Reservoir_Sampling

import java.security.SecureRandom;

public class Solution {
private final int[] weights;
private final int[][] rects;
private final SecureRandom random;

public Solution(int[][] rects) {
this.weights = new int[rects.length];
this.rects = rects;
this.random = new SecureRandom();
for (int i = 0; i < rects.length; i++) {
int[] rect = rects[i];
int count = (1 + rect[2] - rect[0]) * (1 + rect[3] - rect[1]);
weights[i] = (i == 0 ? 0 : weights[i - 1]) + count;
}
}

public int[] pick() {
int picked = 1 + random.nextInt(weights[weights.length - 1]);
int idx = findGreaterOrEqual(picked);
return getRandomPoint(idx);
}

private int findGreaterOrEqual(int target) {
int left = 0;
int right = weights.length - 1;
while (left + 1 < right) {
int mid = left + (right - left) / 2;
if (weights[mid] >= target) {
right = mid;
} else {
left = mid + 1;
}
}
return weights[left] >= target ? left : right;
}

private int[] getRandomPoint(int idx) {
int[] r = rects[idx];
int left = r[0];
int right = r[2];
int bot = r[1];
int top = r[3];
return new int[] {
left + random.nextInt(right - left + 1), bot + random.nextInt(top - bot + 1)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
497\. Random Point in Non-overlapping Rectangles

Medium

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.

Any integer point inside the space covered by one of the given rectangles should be equally likely to be returned.

**Note** that an integer point is a point that has integer coordinates.

Implement the `Solution` class:

* `Solution(int[][] rects)` Initializes the object with the given rectangles `rects`.
* `int[] pick()` Returns a random integer point `[u, v]` inside the space covered by one of the given rectangles.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/07/24/lc-pickrandomrec.jpg)

**Input** ["Solution", "pick", "pick", "pick", "pick", "pick"] [[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]

**Output:** [null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]

**Explanation:**

Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
solution.pick(); // return [1, -2]
solution.pick(); // return [1, -1]
solution.pick(); // return [-1, -2]
solution.pick(); // return [-2, -2]
solution.pick(); // return [0, 0]

**Constraints:**

* `1 <= rects.length <= 100`
* `rects[i].length == 4`
* <code>-10<sup>9</sup> <= a<sub>i</sub> < x<sub>i</sub> <= 10<sup>9</sup></code>
* <code>-10<sup>9</sup> <= b<sub>i</sub> < y<sub>i</sub> <= 10<sup>9</sup></code>
* <code>x<sub>i</sub> - a<sub>i</sub> <= 2000</code>
* <code>y<sub>i</sub> - b<sub>i</sub> <= 2000</code>
* All the rectangles do not overlap.
* At most <code>10<sup>4</sup></code> calls will be made to `pick`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g0401_0500.s0492_construct_the_rectangle;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void constructRectangle() {
assertThat(new Solution().constructRectangle(4), equalTo(new int[] {2, 2}));
}

@Test
void constructRectangle2() {
assertThat(new Solution().constructRectangle(37), equalTo(new int[] {37, 1}));
}

@Test
void constructRectangle3() {
assertThat(new Solution().constructRectangle(122122), equalTo(new int[] {427, 286}));
}
}
Loading