Code sometimes AC sometimes TLE, confused with my code's correctness


  • 0
    N

    hi leetcode,
    I try to rerun my code after contest, it sometimes TLE and sometimes AC, i'm confused with this issue, I give some my personal submission here.

    https://leetcode.com/submissions/detail/124723683/ AC
    https://leetcode.com/submissions/detail/124723759/ AC
    https://leetcode.com/submissions/detail/124723794/ TLE
    https://leetcode.com/submissions/detail/124723858/ TLE
    https://leetcode.com/submissions/detail/124723934/ AC
    https://leetcode.com/submissions/detail/124723964/ TLE
    https://leetcode.com/submissions/detail/124724474/ AC

    my code is here:

      private static class Interval implements Comparable<Interval> {
            public int start , end;
            public Interval(int start , int end) {
                this.start = start;
                this.end = end;
            }
            @Override
            public int compareTo(Interval interval) {
                if (this.start < interval.start) {
                    return - 1;
                } else if (this.start > interval.start) {
                    return 1;
                } else {
                    return 0;
                }
            }
            public String toString() {
                return String.format("(%d,%d)" , start , end);
            }
        }
    
        private List<Interval> list = new ArrayList<>();
    
        public RangeModule() {
        }
    
        public void addRange(int left, int right) {
    
            list.add(new Interval(left , right));
            merge();
    
        }
    
        public boolean queryRange(int left, int right) {
    
            if (list.isEmpty()) {
                return false;
            } else {
                int low = 0 , high = list.size();
                while (low < high) {
                    int mid = (low + high) / 2;
                    if (list.get(mid).start > left) {
                        high = mid;
                    } else {
                        low = mid + 1;
                    }
                }
                int index = high - 1;
                if (index >= 0) {
                    if (list.get(index).start <= left && list.get(index).end >= right) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            }
    
        }
    
        public void removeRange(int left, int right) {
    
            List<Interval> temp = new ArrayList<>();
            for (Interval interval : list) {
                if (interval.start >= left && interval.end <= right) {
                    continue;
                } else if (interval.start <= left && interval.end >= right) {
                    if (left > interval.start) {
                        temp.add(new Interval(interval.start , left));
                    }
                    if (right < interval.end) {
                        temp.add(new Interval(right , interval.end));
                    }
                } else {
                    if (left >= interval.start && left <= interval.end) {
                        temp.add(new Interval(interval.start , left));
                    } else if (right >= interval.start && right <= interval.end) {
                        temp.add(new Interval(right , interval.end));
                    } else {
                        temp.add(interval);
                    }
                }
            }
            list = temp;
    
        }
    
        private void merge() {
    
            Collections.sort(list);
            int i , n = list.size();
            List<Interval> temp = new ArrayList<>();
            for (i = 0;i < n;i ++) {
                int start = list.get(i).start , end = list.get(i).end;
                while (i < n && list.get(i).start <= end) {
                    if (list.get(i).end > end) {
                        end = list.get(i).end;
                    }
                    i ++;
                }
                i --;
                temp.add(new Interval(start , end));
            }
            list = temp;
    
        }
    

    and it pass all test cases in contest actually, but appear TLE
    0_1508645524949_7e629621-62f4-40d5-aa2e-37f2eef2afe6-image.png

    Thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.