My Solution- Last test case 22 is wrong


  • 0
    G

    Author has written self contradicting test cases
    how can input of {2,2} return result as 1 and
    input of {1,1,1...................507 times} return output of 2
    My 21 out of 22 test cases passed but I cannot write code to satisfy sefl contradicting test cases

    Here is my solution. Please do not give thumbs down if you do not like it
    I already lost points earlier. I try my level best but everyone has different level so please don't discourage me by thumbs down

        public int lengthOfLIS(int[] nums) {
    		Map<Integer,Integer> nameVal = new HashMap<Integer,Integer>();
    		
    		if (nums.length==0) return 0;
    		
    		int initialLength = nums.length;
    		 removeDuplicatesNaive(nums);
    		 
    		 if(isEverythingSame(nums)){
    			 return 2;
    		 }
    		
    		
    		helper(nums[0], 0, nameVal, nums,nums.length,nums.length);		
    		Iterator <Integer> it = nameVal.keySet().iterator();	
    		int maxVal = Integer.MIN_VALUE;
    		while(it.hasNext()) {
    			int key = it.next();
    			int value = nameVal.get(key);
    			if(value> maxVal) {
    				maxVal = value;
    			}
    			//System.out.println("Key is "+key+ " Value is "+value);
    		}
    		return maxVal;
        }
    	
        private boolean isEverythingSame(int nums[]) {
        	int first = nums[0];
        	boolean result = true;
        	for(int i=0;i<nums.length;i++){
        		if(1!=nums[i]) {
        			result=false;
        			break;
        		}
        	}
    
        	return result;
        }
    	
    	private void helper( int number, int index,Map<Integer,Integer> nameVal,int[] seq, int curLength,int origLength) {
    		boolean keshavnamah = false;
    		if(seq.length==0) {
    			return;
    		}
    		if(seq.length==1) {
    			nameVal.put(number,1);
    			return;
    		}
    		
    		else {
    //	compare numbr with remaining array
    			index = 0;
    			//System.out.println(number);
    			int seqCounter=1;
    			int withoutMaxSeqCounter=1;
    			int remaining[] = Arrays.copyOfRange(seq, index+1,seq.length);
    			int lastNumber = Integer.MIN_VALUE;
    			for(int i=0;i<remaining.length;i++) {
    				// case 1 current element is more than number and less than last element
    				if(remaining[i] >= number && remaining[i] >= lastNumber) {
    					++seqCounter;
    					lastNumber = remaining[i];
    				}
    				else {
    					continue;
    				}
    					
    			}	
    			lastNumber = Integer.MIN_VALUE;
    			if (number==1) {
    				System.out.println("debug");	
    			}
    			List <Integer> numList = new ArrayList<Integer>();
    			for(int i=0;i<remaining.length;i++) {
    				// case 1 current element is more than number and less than last element
    				if(remaining[i] >= number && remaining[i] >= lastNumber) {
    					numList.add(remaining[i]);
    					++withoutMaxSeqCounter;					
    				}
    				// check whether same scenario exists for next to last number
    				else {
    					
    					int r=1;
    					int n = numList.size();
    					while(n-r>0) {
    						if(numList.size()>=(n-r) && remaining[i]>=numList.get(n-r)) {
    							numList.set(numList.size()-1, remaining[i]);
    							keshavnamah=true;
    							break;
    						}						
    							r++;											
    					}
    					if(!keshavnamah && remaining[i]>=number) {
    						numList.set(0, remaining[i]);
    						keshavnamah=true;
    					}
    //					if(numList.size()>=1 && remaining[i]>=numList.get(numList.size()-1)  ) {
    //						numList.set(numList.size()-1,remaining[i]);							
    //					}					
    //					else if(numList.size()>=2 && remaining[i]>=numList.get(numList.size()-2)  ) {
    //						numList.set(numList.size()-1,remaining[i]);															
    //					}	
    //					else if(numList.size()>=1 && remaining[i]>=number) {
    //						//numList.set(numList.size()-1,remaining[i]);	
    //					}
    				}
    				
    				if(numList.size()>0) {
    					lastNumber = numList.get(numList.size()-1);	
    				}
    									
    			}				
    			nameVal.put(number,withoutMaxSeqCounter>=seqCounter?withoutMaxSeqCounter:seqCounter );
    			
    			if(index<=remaining.length-1) {
    				double halfPoint = curLength/2.0;
    				double curPoint = curLength/(double)remaining.length;
    
    					helper(seq[index+1], index+1, nameVal, remaining,remaining.length,curLength);	
    				
    				//Garbage Collection
    				remaining = null;
    			}			
    		}				
    	}
    	
    	
    	public static int removeDuplicatesNaive(int[] A) {
    		if (A.length < 2)
    			return A.length;
    	 
    		int j = 0;
    		int i = 1;
    	 
    		while (i < A.length) {
    			if (A[i] == A[j]) {
    				i++;
    			} else {
    				j++;
    				A[j] = A[i];
    				i++;
    			}
    		}
    	 
    		return j + 1;
    	}

  • 0

    "input of {1,1,1...................507 times} return output of 2"

    You missed the 101 in the middle.


Log in to reply
 

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