Why this Add O(n) & Find O(n) solution not accepted?


  • 0
    public class TwoSum {
        private ArrayList<Integer> list = new ArrayList<Integer>();
    
        // Add => O(n)
        public void add(int number) {
            if (list.size() < 1) {
                list.add(number);
                return;
            }
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) >= number) {
                    list.add(i, number);
                    return;
                }
            }
            list.add(number);
        }
    
        // Find => O(n)
        public boolean find(int value) {
            if (list.size() < 2) {
                return false;
            }
            int start = 0;
            int end = list.size() - 1;
            while (start < end) {
                int left = list.get(start);
                int right = list.get(end);
                if (left + right < value) {
                    start ++;
                } else if (left + right > value) {
                    end --;
                } else {
                    return true;
                }
            }
            return false;
        }
    }
    

Log in to reply
 

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