-
Notifications
You must be signed in to change notification settings - Fork 89
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
Conversation
|
||
**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\]\] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change structure to the blocks.
|
||
// #Hard #Binary_Search #Design #Ordered_Set | ||
|
||
import java.util.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be concrete classes.
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be SummaryRangesTest
class name
summaryRanges.addNum(2); | ||
summaryRanges.addNum(3); | ||
summaryRanges.addNum(7); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove empty line.
size++; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove empty lines.
int i = 0; | ||
int j = 0; | ||
int k = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove empty lines.
|
||
public class SummaryRanges { | ||
|
||
private final NavigableSet<int[]> intervals; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found better implemetation
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;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
summaryRanges.getIntervals(); // return \[\[1, 3\], \[7, 7\]\] | ||
summaryRanges.addNum(6); // arr = \[1, 2, 3, 6, 7\] | ||
summaryRanges.getIntervals(); // return \[\[1, 3\], \[6, 7\]\] | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
|
||
**Example 1:** | ||
|
||
**Input** \["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"\] \[\[\], \[1\], \[\], \[3\], \[\], \[7\], \[\], \[2\], \[\], \[6\], \[\]\] |
There was a problem hiding this comment.
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.
import org.junit.jupiter.api.Test; | ||
|
||
class SolutionTest { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove empty line.
|
||
while (i != j) { | ||
int mid = i + ((j - i) >> 1); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove empty lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually code spaces like that to be easier to see than just writing a block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea to use plugin to format codes. If it allows to remove empty line - remove it.
} | ||
} | ||
return Arrays.copyOfRange(nums1, 0, k); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
@@ -20,7 +20,8 @@ | |||
while (i < nums1.length && j < nums2.length) { | |||
// Check if nums1 value is less then nums2 value; | |||
if (nums1[i] < nums2[j]) { | |||
i++; // Increment "i" | |||
// Increment "i" | |||
i++; | |||
} | |||
// Check if nums2 value is less then nums1 value; | |||
else if (nums1[i] > nums2[j]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may move comment inside if or remove it. The description looks very simple.
Kudos, SonarCloud Quality Gate passed! |
LGTM |
No description provided.