Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53a3bed

Browse files
Added tasks 350, 352, 354.
1 parent 3e16d72 commit 53a3bed

File tree

9 files changed

+330
-0
lines changed

9 files changed

+330
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g0301_0400.s0350_intersection_of_two_arrays_ii;
2+
3+
// #Easy #Top_Interview_Questions #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] intersect(int[] nums1, int[] nums2) {
9+
// First sort the array
10+
Arrays.sort(nums1);
11+
// Similar this one as well
12+
Arrays.sort(nums2);
13+
// "i" will point at nums1
14+
int i = 0;
15+
// "j" will point at nums2
16+
int j = 0;
17+
// "k" will point at nums1 and helps in getting the intersection answer;
18+
int k = 0;
19+
// Loop will run until "i" & "j" doesn't reach the array boundary;
20+
while (i < nums1.length && j < nums2.length) {
21+
if (nums1[i] < nums2[j]) {
22+
// Check if nums1 value is less then nums2 value;
23+
// Increment "i"
24+
i++;
25+
} else if (nums1[i] > nums2[j]) {
26+
// Check if nums2 value is less then nums1 value;
27+
// Increment "j"
28+
j++;
29+
30+
} else {
31+
// Check if nums1 value is equals to nums2 value;
32+
// Dump into nums1 and increment k, increment i & increment j as well;
33+
nums1[k++] = nums1[i++];
34+
j++;
35+
}
36+
}
37+
// Only return nums1 0th index to kth index value, because that's will be our intersection;
38+
return Arrays.copyOfRange(nums1, 0, k);
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
350\. Intersection of Two Arrays II
2+
3+
Easy
4+
5+
Given two integer arrays `nums1` and `nums2`, return _an array of their intersection_. Each element in the result must appear as many times as it shows in both arrays and you may return the result in **any order**.
6+
7+
**Example 1:**
8+
9+
**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]
10+
11+
**Output:** \[2,2\]
12+
13+
**Example 2:**
14+
15+
**Input:** nums1 = \[4,9,5\], nums2 = \[9,4,9,8,4\]
16+
17+
**Output:** \[4,9\]
18+
19+
**Explanation:** \[9,4\] is also accepted.
20+
21+
**Constraints:**
22+
23+
* `1 <= nums1.length, nums2.length <= 1000`
24+
* `0 <= nums1[i], nums2[i] <= 1000`
25+
26+
**Follow up:**
27+
28+
* What if the given array is already sorted? How would you optimize your algorithm?
29+
* What if `nums1`'s size is small compared to `nums2`'s size? Which algorithm is better?
30+
* What if elements of `nums2` are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g0301_0400.s0352_data_stream_as_disjoint_intervals;
2+
3+
// #Hard #Binary_Search #Design #Ordered_Set
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class SummaryRanges {
9+
private static class Node {
10+
int min;
11+
int max;
12+
13+
public Node(int min, int max) {
14+
this.min = min;
15+
this.max = max;
16+
}
17+
18+
public Node(int val) {
19+
min = val;
20+
max = val;
21+
}
22+
}
23+
24+
List<Node> list;
25+
// Initialize your data structure here.
26+
public SummaryRanges() {
27+
list = new ArrayList<>();
28+
}
29+
30+
public void addNum(int val) {
31+
int left = 0;
32+
int right = list.size() - 1;
33+
int index = list.size();
34+
while (left <= right) {
35+
int mid = left + (right - left) / 2;
36+
Node node = list.get(mid);
37+
if (node.min <= val && val <= node.max) {
38+
return;
39+
} else if (val < node.min) {
40+
index = mid;
41+
right = mid - 1;
42+
} else if (val > node.max) {
43+
left = mid + 1;
44+
}
45+
}
46+
list.add(index, new Node(val));
47+
}
48+
49+
public int[][] getIntervals() {
50+
for (int i = 1; i < list.size(); i++) {
51+
Node prev = list.get(i - 1);
52+
Node curr = list.get(i);
53+
if (curr.min == prev.max + 1) {
54+
prev.max = curr.max;
55+
list.remove(i--);
56+
}
57+
}
58+
int len = list.size();
59+
int[][] res = new int[len][2];
60+
for (int i = 0; i < len; i++) {
61+
Node node = list.get(i);
62+
res[i][0] = node.min;
63+
res[i][1] = node.max;
64+
}
65+
return res;
66+
}
67+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
352\. Data Stream as Disjoint Intervals
2+
3+
Hard
4+
5+
Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.
6+
7+
Implement the `SummaryRanges` class:
8+
9+
* `SummaryRanges()` Initializes the object with an empty stream.
10+
* `void addNum(int val)` Adds the integer `val` to the stream.
11+
* `int[][] getIntervals()` Returns a summary of the integers in the stream currently as a list of disjoint intervals <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.
12+
13+
**Example 1:**
14+
15+
**Input** \["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"\] \[\[\], \[1\], \[\], \[3\], \[\], \[7\], \[\], \[2\], \[\], \[6\], \[\]\]
16+
17+
**Output:** \[null, null, \[\[1, 1\]\], null, \[\[1, 1\], \[3, 3\]\], null, \[\[1, 1\], \[3, 3\], \[7, 7\]\], null, \[\[1, 3\], \[7, 7\]\], null, \[\[1, 3\], \[6, 7\]\]\]
18+
19+
**Explanation:**
20+
```
21+
SummaryRanges summaryRanges = new SummaryRanges();
22+
summaryRanges.addNum(1); // arr = [1]
23+
summaryRanges.getIntervals(); // return [[1, 1]]
24+
summaryRanges.addNum(3); // arr = [1, 3]
25+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
26+
summaryRanges.addNum(7); // arr = [1, 3, 7]
27+
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
28+
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
29+
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
30+
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
31+
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
32+
```
33+
34+
**Constraints:**
35+
36+
* <code>0 <= val <= 10<sup>4</sup></code>
37+
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `addNum` and `getIntervals`.
38+
39+
**Follow up:** What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0301_0400.s0354_russian_doll_envelopes;
2+
3+
// #Hard #Array #Dynamic_Programming #Sorting #Binary_Search
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxEnvelopes(int[][] envelopes) {
9+
Arrays.sort(envelopes, (a, b) -> a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]);
10+
int[] tails = new int[envelopes.length];
11+
int size = 0;
12+
for (int[] enve : envelopes) {
13+
int i = 0;
14+
int j = size;
15+
while (i != j) {
16+
int mid = i + ((j - i) >> 1);
17+
if (tails[mid] < enve[1]) {
18+
i = mid + 1;
19+
} else {
20+
j = mid;
21+
}
22+
}
23+
tails[i] = enve[1];
24+
if (i == size) {
25+
size++;
26+
}
27+
}
28+
return size;
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
354\. Russian Doll Envelopes
2+
3+
Hard
4+
5+
You are given a 2D array of integers `envelopes` where <code>envelopes[i] = [w<sub>i</sub>, h<sub>i</sub>]</code> represents the width and the height of an envelope.
6+
7+
One envelope can fit into another if and only if both the width and height of one envelope are greater than the other envelope's width and height.
8+
9+
Return _the maximum number of envelopes you can Russian doll (i.e., put one inside the other)_.
10+
11+
**Note:** You cannot rotate an envelope.
12+
13+
**Example 1:**
14+
15+
**Input:** envelopes = \[\[5,4\],\[6,4\],\[6,7\],\[2,3\]\]
16+
17+
**Output:** 3
18+
19+
**Explanation:** The maximum number of envelopes you can Russian doll is `3` (\[2,3\] => \[5,4\] => \[6,7\]).
20+
21+
**Example 2:**
22+
23+
**Input:** envelopes = \[\[1,1\],\[1,1\],\[1,1\]\]
24+
25+
**Output:** 1
26+
27+
**Constraints:**
28+
29+
* `1 <= envelopes.length <= 5000`
30+
* `envelopes[i].length == 2`
31+
* <code>1 <= w<sub>i</sub>, h<sub>i</sub> <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0301_0400.s0350_intersection_of_two_arrays_ii;
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 intersect() {
11+
assertThat(
12+
new Solution().intersect(new int[] {1, 2, 2, 1}, new int[] {2, 2}),
13+
equalTo(new int[] {2, 2}));
14+
}
15+
16+
@Test
17+
void intersect2() {
18+
assertThat(
19+
new Solution().intersect(new int[] {4, 9, 5}, new int[] {9, 4, 9, 8, 4}),
20+
equalTo(new int[] {4, 9}));
21+
}
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0301_0400.s0352_data_stream_as_disjoint_intervals;
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 SummaryRangesTest {
9+
SummaryRanges summaryRanges = new SummaryRanges();
10+
11+
@Test
12+
void testGetInterval() {
13+
summaryRanges.addNum(1);
14+
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}}));
15+
}
16+
17+
@Test
18+
void testGetInterval2() {
19+
summaryRanges.addNum(1);
20+
summaryRanges.addNum(3);
21+
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}, {3, 3}}));
22+
}
23+
24+
@Test
25+
void testGetInterval3() {
26+
summaryRanges.addNum(1);
27+
summaryRanges.addNum(3);
28+
summaryRanges.addNum(7);
29+
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}, {3, 3}, {7, 7}}));
30+
}
31+
32+
@Test
33+
void testGetInterval4() {
34+
summaryRanges.addNum(1);
35+
summaryRanges.addNum(2);
36+
summaryRanges.addNum(3);
37+
summaryRanges.addNum(7);
38+
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 3}, {7, 7}}));
39+
}
40+
41+
@Test
42+
void testGetInterval5() {
43+
summaryRanges.addNum(1);
44+
summaryRanges.addNum(2);
45+
summaryRanges.addNum(3);
46+
summaryRanges.addNum(6);
47+
summaryRanges.addNum(7);
48+
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 3}, {6, 7}}));
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0301_0400.s0354_russian_doll_envelopes;
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+
10+
@Test
11+
void testMaxEnvelopes() {
12+
assertThat(
13+
new Solution().maxEnvelopes(new int[][] {{5, 4}, {6, 4}, {6, 7}, {2, 3}}),
14+
equalTo(3));
15+
}
16+
17+
@Test
18+
void testMaxEnvelopes2() {
19+
assertThat(new Solution().maxEnvelopes(new int[][] {{1, 1}, {1, 1}, {1, 1}}), equalTo(1));
20+
}
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /