Skip to content

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
merged 13 commits into from
Dec 13, 2021
Merged

added tasks 350, 352, 354 #111

merged 13 commits into from
Dec 13, 2021

Conversation

ThanhNIT
Copy link
Contributor

No description provided.


**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 change structure to the blocks.


// #Hard #Binary_Search #Design #Ordered_Set

import java.util.*;
Copy link
Owner

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 {
Copy link
Owner

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);

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.

size++;
}
}

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

int i = 0;
int j = 0;
int k = 0;

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


public class SummaryRanges {

private final NavigableSet<int[]> intervals;
Copy link
Owner

Choose a reason for hiding this comment

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

This solution is not very fast

image

Copy link
Owner

@javadev javadev Dec 13, 2021

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;
    }
}

Copy link
Owner

Choose a reason for hiding this comment

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

image

Copy link
Contributor Author

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


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

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.


while (i != j) {
int mid = i + ((j - i) >> 1);

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 lines

Copy link
Contributor Author

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

Copy link
Owner

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);
}
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.

@@ -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]) {
Copy link
Owner

@javadev javadev Dec 13, 2021

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.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

88.6% 88.6% Coverage
0.0% 0.0% Duplication

@javadev
Copy link
Owner

javadev commented Dec 13, 2021

LGTM

@javadev javadev merged commit 53a3bed into javadev:main Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants