Solve unsorted-substring
Write a function that returns the indices demarcating a substring, which if sorted, would make the entire array sorted.
This commit is contained in:
		
							parent
							
								
									47c5c6ac05
								
							
						
					
					
						commit
						48fde5f278
					
				
					 1 changed files with 21 additions and 0 deletions
				
			
		
							
								
								
									
										21
									
								
								scratch/facebook/moderate/unsorted-substring.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								scratch/facebook/moderate/unsorted-substring.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,21 @@
 | 
				
			||||||
 | 
					# Write a function that accepts an array of integers and returns the indices for
 | 
				
			||||||
 | 
					# the starting and ending integers that, if their elements were sorted, the
 | 
				
			||||||
 | 
					# entire array would be sorted.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def unsorted_substring(xs):
 | 
				
			||||||
 | 
					    ys = xs[:]; ys.sort()
 | 
				
			||||||
 | 
					    m = 0
 | 
				
			||||||
 | 
					    while xs[m] == ys[m]:
 | 
				
			||||||
 | 
					        m += 1
 | 
				
			||||||
 | 
					        if m >= len(xs):
 | 
				
			||||||
 | 
					            return -1, -1
 | 
				
			||||||
 | 
					    n = len(xs) - 1
 | 
				
			||||||
 | 
					    while xs[n] == ys[n]:
 | 
				
			||||||
 | 
					        n -= 1
 | 
				
			||||||
 | 
					    return m, n
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(unsorted_substring([1,2,4,7,10,11,7,12,6,7,16,18,19]))
 | 
				
			||||||
 | 
					print(unsorted_substring([1,2,3,4]))
 | 
				
			||||||
 | 
					print(unsorted_substring([4,3,2,1]))
 | 
				
			||||||
 | 
					print(unsorted_substring([1,3,2,4]))
 | 
				
			||||||
 | 
					print(unsorted_substring([2,1,3,4]))
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue