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

added tasks 350, 352, 354 #111

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
javadev merged 13 commits into javadev:main from ThanhNIT:tasks-350-352-354
Dec 13, 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
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g0301_0400.s0350_intersection_of_two_arrays_ii;

// #Easy #Top_Interview_Questions #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers

import java.util.Arrays;

public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
// First sort the array
Arrays.sort(nums1);
// Similar this one as well
Arrays.sort(nums2);
// "i" will point at nums1
int i = 0;
// "j" will point at nums2
int j = 0;
// "k" will point at nums1 and helps in getting the intersection answer;
int k = 0;
// Loop will run until "i" & "j" doesn't reach the array boundary;
while (i < nums1.length && j < nums2.length) {
if (nums1[i] < nums2[j]) {
// Check if nums1 value is less then nums2 value;
// Increment "i"
i++;
} else if (nums1[i] > nums2[j]) {
// Check if nums2 value is less then nums1 value;
// Increment "j"
j++;

} else {
// Check if nums1 value is equals to nums2 value;
// Dump into nums1 and increment k, increment i & increment j as well;
nums1[k++] = nums1[i++];
j++;
}
}
// Only return nums1 0th index to kth index value, because that's will be our intersection;
return Arrays.copyOfRange(nums1, 0, k);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may keep comments in solutions.

Trailing comment may be moved to separate line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like that ?
image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we may keep comments. We only need to move them, in separate line above.

}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
350\. Intersection of Two Arrays II

Easy

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

**Example 1:**

**Input:** nums1 = \[1,2,2,1\], nums2 = \[2,2\]

**Output:** \[2,2\]

**Example 2:**

**Input:** nums1 = \[4,9,5\], nums2 = \[9,4,9,8,4\]

**Output:** \[4,9\]

**Explanation:** \[9,4\] is also accepted.

**Constraints:**

* `1 <= nums1.length, nums2.length <= 1000`
* `0 <= nums1[i], nums2[i] <= 1000`

**Follow up:**

* What if the given array is already sorted? How would you optimize your algorithm?
* What if `nums1`'s size is small compared to `nums2`'s size? Which algorithm is better?
* 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?
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package g0301_0400.s0352_data_stream_as_disjoint_intervals;

// #Hard #Binary_Search #Design #Ordered_Set

import java.util.ArrayList;
import java.util.List;

public class SummaryRanges {
private static class Node {
int min;
int max;

public Node(int min, int max) {
this.min = min;
this.max = max;
}

public Node(int val) {
min = val;
max = val;
}
}

List<Node> list;
// Initialize your data structure here.
public SummaryRanges() {
list = new ArrayList<>();
}

public void addNum(int val) {
int left = 0;
int right = list.size() - 1;
int index = list.size();
while (left <= right) {
int mid = left + (right - left) / 2;
Node node = list.get(mid);
if (node.min <= val && val <= node.max) {
return;
} else if (val < node.min) {
index = mid;
right = mid - 1;
} else if (val > node.max) {
left = mid + 1;
}
}
list.add(index, new Node(val));
}

public int[][] getIntervals() {
for (int i = 1; i < list.size(); i++) {
Node prev = list.get(i - 1);
Node curr = list.get(i);
if (curr.min == prev.max + 1) {
prev.max = curr.max;
list.remove(i--);
}
}
int len = list.size();
int[][] res = new int[len][2];
for (int i = 0; i < len; i++) {
Node node = list.get(i);
res[i][0] = node.min;
res[i][1] = node.max;
}
return res;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
352\. Data Stream as Disjoint Intervals

Hard

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.

Implement the `SummaryRanges` class:

* `SummaryRanges()` Initializes the object with an empty stream.
* `void addNum(int val)` Adds the integer `val` to the stream.
* `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>.

**Example 1:**

**Input** \["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"\] \[\[\], \[1\], \[\], \[3\], \[\], \[7\], \[\], \[2\], \[\], \[6\], \[\]\]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like two lines. It may be changes with code block.


**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\]\]\]

**Explanation:**
```
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1); // arr = [1]
summaryRanges.getIntervals(); // return [[1, 1]]
summaryRanges.addNum(3); // arr = [1, 3]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
summaryRanges.addNum(7); // arr = [1, 3, 7]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2); // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
summaryRanges.addNum(6); // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]
```
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove escapes and replace code block with 4 spaces ident.

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may use four space ident for code blocks.


**Constraints:**

* <code>0 <= val <= 10<sup>4</sup></code>
* At most <code>3 * 10<sup>4</sup></code> calls will be made to `addNum` and `getIntervals`.

**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?
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g0301_0400.s0354_russian_doll_envelopes;

// #Hard #Array #Dynamic_Programming #Sorting #Binary_Search

import java.util.Arrays;

public class Solution {
public int maxEnvelopes(int[][] envelopes) {
Arrays.sort(envelopes, (a, b) -> a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]);
int[] tails = new int[envelopes.length];
int size = 0;
for (int[] enve : envelopes) {
int i = 0;
int j = size;
while (i != j) {
int mid = i + ((j - i) >> 1);
if (tails[mid] < enve[1]) {
i = mid + 1;
} else {
j = mid;
}
}
tails[i] = enve[1];
if (i == size) {
size++;
}
}
return size;
}
}
31 changes: 31 additions & 0 deletions src/main/java/g0301_0400/s0354_russian_doll_envelopes/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
354\. Russian Doll Envelopes

Hard

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.

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.

Return _the maximum number of envelopes you can Russian doll (i.e., put one inside the other)_.

**Note:** You cannot rotate an envelope.

**Example 1:**

**Input:** envelopes = \[\[5,4\],\[6,4\],\[6,7\],\[2,3\]\]

**Output:** 3

**Explanation:** The maximum number of envelopes you can Russian doll is `3` (\[2,3\] => \[5,4\] => \[6,7\]).

**Example 2:**

**Input:** envelopes = \[\[1,1\],\[1,1\],\[1,1\]\]

**Output:** 1

**Constraints:**

* `1 <= envelopes.length <= 5000`
* `envelopes[i].length == 2`
* <code>1 <= w<sub>i</sub>, h<sub>i</sub> <= 10<sup>4</sup></code>
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g0301_0400.s0350_intersection_of_two_arrays_ii;

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

import org.junit.jupiter.api.Test;

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

@Test
void intersect2() {
assertThat(
new Solution().intersect(new int[] {4, 9, 5}, new int[] {9, 4, 9, 8, 4}),
equalTo(new int[] {4, 9}));
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g0301_0400.s0352_data_stream_as_disjoint_intervals;

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

import org.junit.jupiter.api.Test;

class SummaryRangesTest {
SummaryRanges summaryRanges = new SummaryRanges();

@Test
void testGetInterval() {
summaryRanges.addNum(1);
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}}));
}

@Test
void testGetInterval2() {
summaryRanges.addNum(1);
summaryRanges.addNum(3);
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}, {3, 3}}));
}

@Test
void testGetInterval3() {
summaryRanges.addNum(1);
summaryRanges.addNum(3);
summaryRanges.addNum(7);
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 1}, {3, 3}, {7, 7}}));
}

@Test
void testGetInterval4() {
summaryRanges.addNum(1);
summaryRanges.addNum(2);
summaryRanges.addNum(3);
summaryRanges.addNum(7);
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 3}, {7, 7}}));
}

@Test
void testGetInterval5() {
summaryRanges.addNum(1);
summaryRanges.addNum(2);
summaryRanges.addNum(3);
summaryRanges.addNum(6);
summaryRanges.addNum(7);
assertThat(summaryRanges.getIntervals(), equalTo(new int[][] {{1, 3}, {6, 7}}));
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0301_0400.s0354_russian_doll_envelopes;

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

import org.junit.jupiter.api.Test;

class SolutionTest {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty line.

@Test
void testMaxEnvelopes() {
assertThat(
new Solution().maxEnvelopes(new int[][] {{5, 4}, {6, 4}, {6, 7}, {2, 3}}),
equalTo(3));
}

@Test
void testMaxEnvelopes2() {
assertThat(new Solution().maxEnvelopes(new int[][] {{1, 1}, {1, 1}, {1, 1}}), equalTo(1));
}
}

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