import java . math . BigInteger ; import java . util . Arrays ; public final class p104 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p104 ( ) . run ( ) ) ; } public String run ( ) { int i = 0 ; int a = 0 ; int b = 1 ; while ( ! isFound ( i , a ) ) { int c = ( a + b ) % 1000000000 ; a = b ; b = c ; i ++ ; } return Integer . toString ( i ) ; } private static boolean isFound ( int n , int fibMod ) { if ( ! isPandigital ( Integer . toString ( fibMod ) ) ) return false ; BigInteger fib = fibonacci ( n ) [ 0 ] ; if ( fib . mod ( BigInteger . valueOf ( 1000000000 ) ) . intValue ( ) != fibMod ) throw new AssertionError ( ) ; return isPandigital ( leading9Digits ( fib ) ) ; } private static String leading9Digits ( BigInteger x ) { int log10 = ( x . bitLength ( ) - 1 ) * 3 / 10 ; x = x . divide ( BigInteger . TEN . pow ( Math . max ( log10 + 1 - 9 , 0 ) ) ) ; return x . toString ( ) . substring ( 0 , 9 ) ; } private static boolean isPandigital ( String s ) { if ( s . length ( ) != 9 ) return false ; char [ ] temp = s . toCharArray ( ) ; Arrays . sort ( temp ) ; return new String ( temp ) . equals ( "123456789" ) ; } private static BigInteger [ ] fibonacci ( int n ) { if ( n < 0 ) throw new IllegalArgumentException ( ) ; else if ( n == 0 ) return new BigInteger [ ] { BigInteger . ZERO , BigInteger . ONE } ; else { BigInteger [ ] ab = fibonacci ( n / 2 ) ; BigInteger a = ab [ 0 ] ; BigInteger b = ab [ 1 ] ; BigInteger c = a . multiply ( b . shiftLeft ( 1 ) . subtract ( a ) ) ; BigInteger d = a . multiply ( a ) . add ( b . multiply ( b ) ) ; if ( n % 2 == 0 ) return new BigInteger [ ] { c , d } ; else return new BigInteger [ ] { d , c . add ( d ) } ; } } }

import itertools NEW_LINE def compute ( ) : NEW_LINE INDENT MOD = 10 ** 9 NEW_LINE a = 0 NEW_LINE b = 1 NEW_LINE for i in itertools . count ( ) : NEW_LINE INDENT if " " . join ( sorted ( str ( a ) ) ) == "123456789" : NEW_LINE INDENT f = fibonacci ( i ) [ 0 ] NEW_LINE if " " . join ( sorted ( str ( f ) [ : 9 ] ) ) == "123456789" : NEW_LINE INDENT return str ( i ) NEW_LINE DEDENT DEDENT a , b = b , ( a + b ) % MOD NEW_LINE DEDENT return str ( ans ) NEW_LINE DEDENT def fibonacci ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return ( 0 , 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT a , b = fibonacci ( n // 2 ) NEW_LINE c = a * ( b * 2 - a ) NEW_LINE d = a * a + b * b NEW_LINE if n % 2 == 0 : NEW_LINE INDENT return ( c , d ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( d , c + d ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p206 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p206 ( ) . run ( ) ) ; } public String run ( ) { long n = 1000000000 ; int [ ] ndigits = new int [ 10 ] ; int [ ] n2digits = new int [ 19 ] ; long temp = n ; for ( int i = 0 ; i < ndigits . length ; i ++ , temp /= 10 ) ndigits [ i ] = ( int ) ( temp % 10 ) ; temp = n * n ; for ( int i = 0 ; i < n2digits . length ; i ++ , temp /= 10 ) n2digits [ i ] = ( int ) ( temp % 10 ) ; while ( ! isConcealedSquare ( n2digits ) ) { add20n ( ndigits , n2digits ) ; add10Pow ( n2digits , 2 ) ; n += 10 ; add10Pow ( ndigits , 1 ) ; } return Long . toString ( n ) ; } private static boolean isConcealedSquare ( int [ ] n ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( n [ 20 - i * 2 ] != i ) return false ; } return n [ 0 ] == 0 ; } private static void add10Pow ( int [ ] n , int i ) { while ( n [ i ] == 9 ) { n [ i ] = 0 ; i ++ ; } n [ i ] ++ ; } private static void add20n ( int [ ] n , int [ ] n2 ) { int carry = 0 ; int i ; for ( i = 0 ; i < n . length ; i ++ ) { int sum = n [ i ] * 2 + n2 [ i + 1 ] + carry ; n2 [ i + 1 ] = sum % 10 ; carry = sum / 10 ; } for ( i ++ ; carry > 0 ; i ++ ) { int sum = n2 [ i ] + carry ; n2 [ i ] = sum % 10 ; carry = sum / 10 ; } } }

def compute ( ) : NEW_LINE INDENT n = 1000000000 NEW_LINE ndigits = [ 0 ] * 10 NEW_LINE temp = n NEW_LINE for i in range ( len ( ndigits ) ) : NEW_LINE INDENT ndigits [ i ] = temp % 10 NEW_LINE temp //= 10 NEW_LINE DEDENT n2digits = [ 0 ] * 19 NEW_LINE temp = n * n NEW_LINE for i in range ( len ( n2digits ) ) : NEW_LINE INDENT n2digits [ i ] = temp % 10 NEW_LINE temp //= 10 NEW_LINE DEDENT while not is_concealed_square ( n2digits ) : NEW_LINE INDENT add_20n ( ndigits , n2digits ) NEW_LINE add_10pow ( n2digits , 2 ) NEW_LINE n += 10 NEW_LINE add_10pow ( ndigits , 1 ) NEW_LINE DEDENT return str ( n ) NEW_LINE DEDENT def is_concealed_square ( n ) : NEW_LINE INDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT if n [ 20 - i * 2 ] != i : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return n [ 0 ] == 0 NEW_LINE DEDENT def add_10pow ( n , i ) : NEW_LINE INDENT while n [ i ] == 9 : NEW_LINE INDENT n [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT n [ i ] += 1 NEW_LINE DEDENT def add_20n ( n , n2 ) : NEW_LINE INDENT carry = 0 NEW_LINE i = 0 NEW_LINE while i < len ( n ) : NEW_LINE INDENT sum = n [ i ] * 2 + n2 [ i + 1 ] + carry NEW_LINE n2 [ i + 1 ] = sum % 10 NEW_LINE carry = sum // 10 NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE while carry > 0 : NEW_LINE INDENT sum = n2 [ i ] + carry NEW_LINE n2 [ i ] = sum % 10 NEW_LINE carry = sum // 10 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p077 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p077 ( ) . run ( ) ) ; } private static final int TARGET = 5000 ; public String run ( ) { for ( int limit = 1 ; ; limit *= 2 ) { int result = search ( limit , TARGET ) ; if ( result != - 1 ) return Integer . toString ( result ) ; } } private static int search ( int limit , int target ) { int [ ] partitions = new int [ limit ] ; partitions [ 0 ] = 1 ; for ( int i = 0 ; i < partitions . length ; i ++ ) { if ( ! Library . isPrime ( i ) ) continue ; for ( int j = i ; j < partitions . length ; j ++ ) partitions [ j ] += partitions [ j - i ] ; } for ( int i = 0 ; i < limit ; i ++ ) { if ( partitions [ i ] > target ) return i ; } return - 1 ; } }

import eulerlib , itertools NEW_LINE def compute ( ) : NEW_LINE INDENT cond = lambda n : num_prime_sum_ways ( n ) > 5000 NEW_LINE ans = next ( filter ( cond , itertools . count ( 2 ) ) ) NEW_LINE return str ( ans ) NEW_LINE DEDENT primes = [ 2 ] NEW_LINE def num_prime_sum_ways ( n ) : NEW_LINE INDENT for i in range ( primes [ - 1 ] + 1 , n + 1 ) : NEW_LINE INDENT if eulerlib . is_prime ( i ) : NEW_LINE INDENT primes . append ( i ) NEW_LINE DEDENT DEDENT ways = [ 1 ] + [ 0 ] * n NEW_LINE for p in primes : NEW_LINE INDENT for i in range ( n + 1 - p ) : NEW_LINE INDENT ways [ i + p ] += ways [ i ] NEW_LINE DEDENT DEDENT return ways [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p116 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p116 ( ) . run ( ) ) ; } private static final int LENGTH = 50 ; public String run ( ) { return Long . toString ( countWays ( LENGTH , 2 ) + countWays ( LENGTH , 3 ) + countWays ( LENGTH , 4 ) ) ; } private static long countWays ( int length , int m ) { long [ ] ways = new long [ length + 1 ] ; ways [ 0 ] = 1 ; for ( int n = 1 ; n <= length ; n ++ ) { ways [ n ] += ways [ n - 1 ] ; if ( n >= m ) ways [ n ] += ways [ n - m ] ; } return ways [ length ] - 1 ; } }

def compute ( ) : NEW_LINE INDENT LENGTH = 50 NEW_LINE return str ( sum ( count_ways ( LENGTH , i ) for i in range ( 2 , 5 ) ) ) NEW_LINE DEDENT def count_ways ( length , m ) : NEW_LINE INDENT ways = [ 1 ] + [ 0 ] * length NEW_LINE for n in range ( 1 , len ( ways ) ) : NEW_LINE INDENT ways [ n ] += ways [ n - 1 ] NEW_LINE if n >= m : NEW_LINE INDENT ways [ n ] += ways [ n - m ] NEW_LINE DEDENT DEDENT return ways [ - 1 ] - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . math . BigInteger ; public final class p100 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p100 ( ) . run ( ) ) ; } public String run ( ) { BigInteger x0 = BigInteger . valueOf ( 3 ) ; BigInteger y0 = BigInteger . valueOf ( 1 ) ; BigInteger x = x0 ; BigInteger y = y0 ; while ( true ) { BigInteger sqrt = Library . sqrt ( y . multiply ( y ) . multiply ( BigInteger . valueOf ( 8 ) ) . add ( BigInteger . ONE ) ) ; if ( sqrt . testBit ( 0 ) ) { BigInteger blue = sqrt . add ( BigInteger . ONE ) . divide ( BigInteger . valueOf ( 2 ) ) . add ( y ) ; if ( blue . add ( y ) . compareTo ( BigInteger . TEN . pow ( 12 ) ) > 0 ) return blue . toString ( ) ; } BigInteger nextx = x . multiply ( x0 ) . add ( y . multiply ( y0 ) . multiply ( BigInteger . valueOf ( 8 ) ) ) ; BigInteger nexty = x . multiply ( y0 ) . add ( y . multiply ( x0 ) ) ; x = nextx ; y = nexty ; } } }

import eulerlib NEW_LINE def compute ( ) : NEW_LINE INDENT x0 = 3 NEW_LINE y0 = 1 NEW_LINE x = x0 NEW_LINE y = y0 NEW_LINE while True : NEW_LINE INDENT sqrt = eulerlib . sqrt ( y ** 2 * 8 + 1 ) NEW_LINE if sqrt % 2 == 1 : NEW_LINE INDENT blue = ( sqrt + 1 ) // 2 + y NEW_LINE if blue + y > 10 ** 12 : NEW_LINE INDENT return str ( blue ) NEW_LINE DEDENT DEDENT nextx = x * x0 + y * y0 * 8 NEW_LINE nexty = x * y0 + y * x0 NEW_LINE x = nextx NEW_LINE y = nexty NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


public final class p222 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p222 ( ) . run ( ) ) ; } public String run ( ) { sphereRadii = new double [ 21 ] ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) sphereRadii [ i ] = ( i + 30 ) * 1000 ; minLength = new double [ sphereRadii . length ] [ 1 << sphereRadii . length ] ; double min = Double . POSITIVE_INFINITY ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) min = Math . min ( findMinimumLength ( i , ( 1 << sphereRadii . length ) - 1 ) + sphereRadii [ i ] , min ) ; return Long . toString ( Math . round ( min ) ) ; } private double [ ] sphereRadii ; private double [ ] [ ] minLength ; private double findMinimumLength ( int currentSphereIndex , int setOfSpheres ) { if ( ( setOfSpheres & ( 1 << currentSphereIndex ) ) == 0 ) throw new IllegalArgumentException ( ) ; if ( minLength [ currentSphereIndex ] [ setOfSpheres ] == 0 ) { double result ; if ( Integer . bitCount ( setOfSpheres ) == 1 ) result = sphereRadii [ currentSphereIndex ] ; else { result = Double . POSITIVE_INFINITY ; int newSetOfSpheres = setOfSpheres ^ ( 1 << currentSphereIndex ) ; for ( int i = 0 ; i < sphereRadii . length ; i ++ ) { if ( ( newSetOfSpheres & ( 1 << i ) ) == 0 ) continue ; double temp = Math . sqrt ( ( sphereRadii [ i ] + sphereRadii [ currentSphereIndex ] - 50000 ) * 200000 ) ; temp += findMinimumLength ( i , newSetOfSpheres ) ; result = Math . min ( temp , result ) ; } } minLength [ currentSphereIndex ] [ setOfSpheres ] = result ; } return minLength [ currentSphereIndex ] [ setOfSpheres ] ; } }

import eulerlib , math NEW_LINE def compute ( ) : NEW_LINE INDENT NUM_SPHERES = 21 NEW_LINE sphereradii = [ ( i + 30 ) * 1000 for i in range ( NUM_SPHERES ) ] NEW_LINE minlength = [ [ None ] * ( 2 ** NUM_SPHERES ) for _ in range ( NUM_SPHERES ) ] NEW_LINE def find_minimum_length ( currentsphereindex , setofspheres ) : NEW_LINE INDENT if setofspheres & ( 1 << currentsphereindex ) == 0 : NEW_LINE INDENT raise ValueError ( ) NEW_LINE DEDENT if minlength [ currentsphereindex ] [ setofspheres ] is None : NEW_LINE INDENT if eulerlib . popcount ( setofspheres ) == 1 : NEW_LINE INDENT result = sphereradii [ currentsphereindex ] NEW_LINE DEDENT else : NEW_LINE INDENT result = float ( " inf " ) NEW_LINE newsetofspheres = setofspheres ^ ( 1 << currentsphereindex ) NEW_LINE for i in range ( NUM_SPHERES ) : NEW_LINE INDENT if newsetofspheres & ( 1 << i ) == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT temp = math . sqrt ( ( sphereradii [ i ] + sphereradii [ currentsphereindex ] - 50000 ) * 200000 ) NEW_LINE temp += find_minimum_length ( i , newsetofspheres ) NEW_LINE result = min ( temp , result ) NEW_LINE DEDENT DEDENT minlength [ currentsphereindex ] [ setofspheres ] = result NEW_LINE DEDENT return minlength [ currentsphereindex ] [ setofspheres ] NEW_LINE DEDENT ans = min ( ( find_minimum_length ( i , ( 1 << NUM_SPHERES ) - 1 ) + sphereradii [ i ] ) for i in range ( NUM_SPHERES ) ) NEW_LINE return str ( int ( round ( ans ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


import java . util . HashSet ; import java . util . Set ; public final class p074 implements EulerSolution { public static void main ( String [ ] args ) { System . out . println ( new p074 ( ) . run ( ) ) ; } private static final int LIMIT = Library . pow ( 10 , 6 ) ; public String run ( ) { int count = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { if ( getChainLength ( i ) == 60 ) count ++ ; } return Integer . toString ( count ) ; } private static int getChainLength ( int n ) { Set < Integer > seen = new HashSet < > ( ) ; while ( true ) { if ( ! seen . add ( n ) ) return seen . size ( ) ; n = factorialize ( n ) ; } } private static int [ ] FACTORIAL = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; private static int factorialize ( int n ) { int sum = 0 ; for ( ; n != 0 ; n /= 10 ) sum += FACTORIAL [ n % 10 ] ; return sum ; } }

import math NEW_LINE def compute ( ) : NEW_LINE INDENT LIMIT = 10 ** 6 NEW_LINE ans = sum ( 1 for i in range ( LIMIT ) if get_chain_length ( i ) == 60 ) NEW_LINE return str ( ans ) NEW_LINE DEDENT def get_chain_length ( n ) : NEW_LINE INDENT seen = set ( ) NEW_LINE while True : NEW_LINE INDENT seen . add ( n ) NEW_LINE n = factorialize ( n ) NEW_LINE if n in seen : NEW_LINE INDENT return len ( seen ) NEW_LINE DEDENT DEDENT DEDENT def factorialize ( n ) : NEW_LINE INDENT result = 0 NEW_LINE while n != 0 : NEW_LINE INDENT result += FACTORIAL [ n % 10 ] NEW_LINE n //= 10 NEW_LINE DEDENT return result NEW_LINE DEDENT FACTORIAL = [ math . factorial ( i ) for i in range ( 10 ) ] NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( compute ( ) ) NEW_LINE DEDENT


class Solution { private TreeNode ans ; public Solution ( ) { this . ans = null ; } private boolean recurseTree ( TreeNode currentNode , TreeNode p , TreeNode q ) { if ( currentNode == null ) { return false ; } int left = this . recurseTree ( currentNode . left , p , q ) ? 1 : 0 ; int right = this . recurseTree ( currentNode . right , p , q ) ? 1 : 0 ; int mid = ( currentNode == p || currentNode == q ) ? 1 : 0 ; if ( mid + left + right >= 2 ) { this . ans = currentNode ; } return ( mid + left + right > 0 ) ; } public TreeNode lowestCommonAncestor ( TreeNode root , TreeNode p , TreeNode q ) { this . recurseTree ( root , p , q ) ; return this . ans ; } }

class Solution ( object ) : NEW_LINE INDENT def lowestCommonAncestor ( self , root , p , q ) : NEW_LINE INDENT stack = [ root ] NEW_LINE parent = { root : None } NEW_LINE while p not in parent or q not in parent : NEW_LINE INDENT node = stack . pop ( ) NEW_LINE if node . left : NEW_LINE INDENT parent [ node . left ] = node NEW_LINE stack . append ( node . left ) NEW_LINE DEDENT if node . right : NEW_LINE INDENT parent [ node . right ] = node NEW_LINE stack . append ( node . right ) NEW_LINE DEDENT DEDENT ancestors = set ( ) NEW_LINE while p : NEW_LINE INDENT ancestors . add ( p ) NEW_LINE p = parent [ p ] NEW_LINE DEDENT while q not in ancestors : NEW_LINE INDENT q = parent [ q ] NEW_LINE DEDENT return q NEW_LINE DEDENT DEDENT


public class Solution { public int findRadius ( int [ ] houses , int [ ] heaters ) { Arrays . sort ( heaters ) ; int result = Integer . MIN_VALUE ; for ( int house : houses ) { int index = Arrays . binarySearch ( heaters , house ) ; if ( index < 0 ) index = - ( index + 1 ) ; int dist1 = index - 1 >= 0 ? house - heaters [ index - 1 ] : Integer . MAX_VALUE ; int dist2 = index < heaters . length ? heaters [ index ] - house : Integer . MAX_VALUE ; result = Math . max ( result , Math . min ( dist1 , dist2 ) ) ; } return result ; } }

class Solution ( object ) : NEW_LINE INDENT def findRadius ( self , houses , heaters ) : NEW_LINE INDENT heaters = sorted ( heaters ) + [ float ( ' inf ' ) ] NEW_LINE i = r = 0 NEW_LINE for x in sorted ( houses ) : NEW_LINE INDENT while x >= sum ( heaters [ i : i + 2 ] ) / 2. : NEW_LINE INDENT i += 1 NEW_LINE DEDENT r = max ( r , abs ( heaters [ i ] - x ) ) NEW_LINE DEDENT return r NEW_LINE DEDENT DEDENT


class Solution { private class LargerNumberComparator implements Comparator < String > { @ Override public int compare ( String a , String b ) { String order1 = a + b ; String order2 = b + a ; return order2 . compareTo ( order1 ) ; } } public String largestNumber ( int [ ] nums ) { String [ ] asStrs = new String [ nums . length ] ; for ( int i = 0 ; i < nums . length ; i ++ ) { asStrs [ i ] = String . valueOf ( nums [ i ] ) ; } Arrays . sort ( asStrs , new LargerNumberComparator ( ) ) ; if ( asStrs [ 0 ] . equals ( "0" ) ) { return "0" ; } String largestNumberStr = new String ( ) ; for ( String numAsStr : asStrs ) { largestNumberStr += numAsStr ; } return largestNumberStr ; } }

class LargerNumKey ( str ) : NEW_LINE INDENT def __lt__ ( x , y ) : NEW_LINE INDENT return x + y > y + x NEW_LINE DEDENT DEDENT class Solution : NEW_LINE INDENT def largestNumber ( self , nums ) : NEW_LINE INDENT largest_num = ' ' . join ( sorted ( map ( str , nums ) , key = LargerNumKey ) ) NEW_LINE return '0' if largest_num [ 0 ] == '0' else largest_num NEW_LINE DEDENT DEDENT


class Solution { public int largestPalindrome ( int n ) { if ( n == 1 ) { return 9 ; } int upperBound = ( int ) Math . pow ( 10 , n ) - 1 , lowerBound = upperBound / 10 ; long maxNumber = ( long ) upperBound * ( long ) upperBound ; int firstHalf = ( int ) ( maxNumber / ( long ) Math . pow ( 10 , n ) ) ; boolean palindromFound = false ; long palindrom = 0 ; while ( ! palindromFound ) { palindrom = createPalindrom ( firstHalf ) ; for ( long i = upperBound ; upperBound > lowerBound ; i -- ) { if ( palindrom / i > maxNumber || i * i < palindrom ) { break ; } if ( palindrom % i == 0 ) { palindromFound = true ; break ; } } firstHalf -- ; } return ( int ) ( palindrom % 1337 ) ; } private long createPalindrom ( long num ) { String str = num + new StringBuilder ( ) . append ( num ) . reverse ( ) . toString ( ) ; return Long . parseLong ( str ) ; } }

class Solution ( object ) : NEW_LINE INDENT def largestPalindrome ( self , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 9 NEW_LINE DEDENT for a in xrange ( 2 , 9 * 10 ** ( n - 1 ) ) : NEW_LINE INDENT hi = ( 10 ** n ) - a NEW_LINE lo = int ( str ( hi ) [ : : - 1 ] ) NEW_LINE if a ** 2 - 4 * lo < 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( a ** 2 - 4 * lo ) ** .5 == int ( ( a ** 2 - 4 * lo ) ** .5 ) : NEW_LINE INDENT return ( lo + 10 ** n * ( 10 ** n - a ) ) % 1337 NEW_LINE DEDENT DEDENT DEDENT DEDENT


class Solution { public int binaryGap ( int N ) { int last = - 1 , ans = 0 ; for ( int i = 0 ; i < 32 ; ++ i ) if ( ( ( N >> i ) & 1 ) > 0 ) { if ( last >= 0 ) ans = Math . max ( ans , i - last ) ; last = i ; } return ans ; } }

class Solution : NEW_LINE INDENT def binaryGap ( self , n : int ) -> int : NEW_LINE INDENT current = 1 NEW_LINE last1 = - 1 NEW_LINE out = 0 NEW_LINE while n > 0 : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT if last1 >= 1 : NEW_LINE INDENT out = max ( out , current - last1 ) NEW_LINE DEDENT last1 = current NEW_LINE DEDENT current += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT return out NEW_LINE DEDENT DEDENT


class Solution { public int [ ] anagramMappings ( int [ ] A , int [ ] B ) { int [ ] ans = new int [ A . length ] ; HashMap < Integer , Integer > valIndex = new HashMap < > ( ) ; for ( int i = 0 ; i < B . length ; i ++ ) valIndex . put ( B [ i ] , i ) ; for ( int i = 0 ; i < A . length ; i ++ ) ans [ i ] = valIndex . get ( A [ i ] ) ; return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def anagramMappings ( self , A , B ) : NEW_LINE INDENT val_index = { } NEW_LINE ans = [ ] NEW_LINE for i , n in enumerate ( B ) : NEW_LINE INDENT val_index [ n ] = i NEW_LINE DEDENT for n in A : NEW_LINE INDENT ans . append ( val_index [ n ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


public class Solution { public String addStrings ( String num1 , String num2 ) { StringBuilder sb = new StringBuilder ( ) ; int carry = 0 ; for ( int i = num1 . length ( ) - 1 , j = num2 . length ( ) - 1 ; i >= 0 || j >= 0 || carry == 1 ; i -- , j -- ) { int x = i < 0 ? 0 : num1 . charAt ( i ) - '0' ; int y = j < 0 ? 0 : num2 . charAt ( j ) - '0' ; sb . append ( ( x + y + carry ) % 10 ) ; carry = ( x + y + carry ) / 10 ; } return sb . reverse ( ) . toString ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def addStrings ( self , num1 , num2 ) : NEW_LINE INDENT res = [ ] NEW_LINE pos1 = len ( num1 ) - 1 NEW_LINE pos2 = len ( num2 ) - 1 NEW_LINE carry = 0 NEW_LINE while pos1 >= 0 or pos2 >= 0 or carry == 1 : NEW_LINE INDENT digit1 = digit2 = 0 NEW_LINE if pos1 >= 0 : NEW_LINE INDENT digit1 = ord ( num1 [ pos1 ] ) - ord ( '0' ) NEW_LINE DEDENT if pos2 >= 0 : NEW_LINE INDENT digit2 = ord ( num2 [ pos2 ] ) - ord ( '0' ) NEW_LINE DEDENT res . append ( str ( ( digit1 + digit2 + carry ) % 10 ) ) NEW_LINE carry = ( digit1 + digit2 + carry ) / 10 NEW_LINE pos1 -= 1 NEW_LINE pos2 -= 1 NEW_LINE DEDENT return ' ' . join ( res [ : : - 1 ] ) NEW_LINE DEDENT DEDENT


public class Solution { public int subarraySum ( int [ ] nums , int k ) { int count = 0 , sum = 0 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( 0 , 1 ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { sum += nums [ i ] ; if ( map . containsKey ( sum - k ) ) count += map . get ( sum - k ) ; map . put ( sum , map . getOrDefault ( sum , 0 ) + 1 ) ; } return count ; } }

class Solution ( object ) : NEW_LINE INDENT def subarraySum ( self , nums , k ) : NEW_LINE INDENT sum_map = { } NEW_LINE sum_map [ 0 ] = 1 NEW_LINE count = curr_sum = 0 NEW_LINE for num in nums : NEW_LINE INDENT curr_sum += num NEW_LINE count += sum_map . get ( curr_sum - k , 0 ) NEW_LINE sum_map [ curr_sum ] = sum_map . get ( curr_sum , 0 ) + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT DEDENT


class Solution { public int findShortestSubArray ( int [ ] nums ) { Map < Integer , Integer > left = new HashMap ( ) , right = new HashMap ( ) , count = new HashMap ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { int x = nums [ i ] ; if ( left . get ( x ) == null ) left . put ( x , i ) ; right . put ( x , i ) ; count . put ( x , count . getOrDefault ( x , 0 ) + 1 ) ; } int ans = nums . length ; int degree = Collections . max ( count . values ( ) ) ; for ( int x : count . keySet ( ) ) { if ( count . get ( x ) == degree ) { ans = Math . min ( ans , right . get ( x ) - left . get ( x ) + 1 ) ; } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def findShortestSubArray ( self , nums ) : NEW_LINE INDENT left , right , count = { } , { } , { } NEW_LINE for i , x in enumerate ( nums ) : NEW_LINE INDENT if x not in left : left [ x ] = i NEW_LINE right [ x ] = i NEW_LINE count [ x ] = count . get ( x , 0 ) + 1 NEW_LINE DEDENT ans = len ( nums ) NEW_LINE degree = max ( count . values ( ) ) NEW_LINE for x in count : NEW_LINE INDENT if count [ x ] == degree : NEW_LINE INDENT ans = min ( ans , right [ x ] - left [ x ] + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


public class Solution { public int maximumProduct ( int [ ] nums ) { int min1 = Integer . MAX_VALUE , min2 = Integer . MAX_VALUE ; int max1 = Integer . MIN_VALUE , max2 = Integer . MIN_VALUE , max3 = Integer . MIN_VALUE ; for ( int n : nums ) { if ( n <= min1 ) { min2 = min1 ; min1 = n ; } else if ( n <= min2 ) { min2 = n ; } if ( n >= max1 ) { max3 = max2 ; max2 = max1 ; max1 = n ; } else if ( n >= max2 ) { max3 = max2 ; max2 = n ; } else if ( n >= max3 ) { max3 = n ; } } return Math . max ( min1 * min2 * max1 , max1 * max2 * max3 ) ; } }

class Solution ( object ) : NEW_LINE INDENT def maximumProduct ( self , nums ) : NEW_LINE INDENT min1 = min2 = float ( ' inf ' ) NEW_LINE max1 = max2 = max3 = float ( ' - inf ' ) NEW_LINE for num in nums : NEW_LINE INDENT if num <= min1 : NEW_LINE INDENT min2 = min1 NEW_LINE min1 = num NEW_LINE DEDENT elif num <= min2 : NEW_LINE INDENT min2 = num NEW_LINE DEDENT if num >= max1 : NEW_LINE INDENT max3 = max2 NEW_LINE max2 = max1 NEW_LINE max1 = num NEW_LINE DEDENT elif num >= max2 : NEW_LINE INDENT max3 = max2 NEW_LINE max2 = num NEW_LINE DEDENT elif num >= max3 : NEW_LINE INDENT max3 = num NEW_LINE DEDENT DEDENT return max ( min1 * min2 * max1 , max1 * max2 * max3 ) NEW_LINE DEDENT DEDENT


public class Solution { public TreeNode constructMaximumBinaryTree ( int [ ] nums ) { return construct ( nums , 0 , nums . length ) ; } public TreeNode construct ( int [ ] nums , int l , int r ) { if ( l == r ) return null ; int max_i = max ( nums , l , r ) ; TreeNode root = new TreeNode ( nums [ max_i ] ) ; root . left = construct ( nums , l , max_i ) ; root . right = construct ( nums , max_i + 1 , r ) ; return root ; } public int max ( int [ ] nums , int l , int r ) { int max_i = l ; for ( int i = l ; i < r ; i ++ ) { if ( nums [ max_i ] < nums [ i ] ) max_i = i ; } return max_i ; } }

class Solution ( object ) : NEW_LINE INDENT def constructMaximumBinaryTree ( self , nums ) : NEW_LINE INDENT if nums is None or len ( nums ) == 0 : NEW_LINE INDENT return None NEW_LINE DEDENT max_index , max_value = 0 , 0 NEW_LINE for i , value in enumerate ( nums ) : NEW_LINE INDENT if value >= max_value : NEW_LINE INDENT max_value = value NEW_LINE max_index = i NEW_LINE DEDENT DEDENT root = TreeNode ( max_value ) NEW_LINE root . left = self . constructMaximumBinaryTree ( nums [ : max_index ] ) NEW_LINE root . right = self . constructMaximumBinaryTree ( nums [ max_index + 1 : ] ) NEW_LINE return root NEW_LINE DEDENT DEDENT


class Solution { public int firstUniqChar ( String s ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( freq [ s . charAt ( i ) - ' a ' ] == 1 ) return i ; return - 1 ; } }

class Solution ( object ) : NEW_LINE INDENT def firstUniqChar ( self , s ) : NEW_LINE INDENT count_map = { } NEW_LINE for c in s : NEW_LINE INDENT count_map [ c ] = count_map . get ( c , 0 ) + 1 NEW_LINE DEDENT for i , c in enumerate ( s ) : NEW_LINE INDENT if count_map [ c ] == 1 : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT DEDENT


class Solution { public boolean isPalindromeRange ( String s , int i , int j ) { for ( int k = i ; k <= i + ( j - i ) / 2 ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( j - k + i ) ) return false ; } return true ; } public boolean validPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) / 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - 1 - i ) ) { int j = s . length ( ) - 1 - i ; return ( isPalindromeRange ( s , i + 1 , j ) || isPalindromeRange ( s , i , j - 1 ) ) ; } } return true ; } }

class Solution ( object ) : NEW_LINE INDENT def validPalindrome ( self , s ) : NEW_LINE INDENT return self . validPalindromeHelper ( s , 0 , len ( s ) - 1 , 1 ) NEW_LINE DEDENT def validPalindromeHelper ( self , s , left , right , budget ) : NEW_LINE INDENT while left < len ( s ) and right >= 0 and left <= right and s [ left ] == s [ right ] : NEW_LINE INDENT left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT if left >= len ( s ) or right < 0 or left >= right : NEW_LINE INDENT return True NEW_LINE DEDENT if budget == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT budget -= 1 NEW_LINE return self . validPalindromeHelper ( s , left + 1 , right , budget ) or self . validPalindromeHelper ( s , left , right - 1 , budget ) NEW_LINE DEDENT DEDENT


class Solution { private List < Integer > memo ; public Solution ( ) { memo = new ArrayList ( ) ; memo . add ( 0 ) ; memo . add ( 1 ) ; } public int fib ( int N ) { if ( N < memo . size ( ) ) return memo . get ( N ) ; for ( int i = memo . size ( ) ; i <= N ; i ++ ) { memo . add ( memo . get ( i - 1 ) + memo . get ( i - 2 ) ) ; } return memo . get ( N ) ; } }

class Solution ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . memo = [ ] NEW_LINE self . memo . append ( 0 ) NEW_LINE self . memo . append ( 1 ) NEW_LINE DEDENT def fib ( self , N ) : NEW_LINE INDENT if N < len ( self . memo ) : NEW_LINE INDENT return self . memo [ N ] NEW_LINE DEDENT for i in range ( len ( self . memo ) , N + 1 ) : NEW_LINE INDENT self . memo . append ( self . memo [ i - 1 ] + self . memo [ i - 2 ] ) NEW_LINE DEDENT return self . memo [ N ] NEW_LINE DEDENT DEDENT


class Solution { public int [ ] [ ] transpose ( int [ ] [ ] A ) { int R = A . length , C = A [ 0 ] . length ; int [ ] [ ] ans = new int [ C ] [ R ] ; for ( int r = 0 ; r < R ; ++ r ) for ( int c = 0 ; c < C ; ++ c ) { ans [ c ] [ r ] = A [ r ] [ c ] ; } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def transpose ( self , A ) : NEW_LINE INDENT R , C = len ( A ) , len ( A [ 0 ] ) NEW_LINE ans = [ [ None ] * R for _ in xrange ( C ) ] NEW_LINE for r , row in enumerate ( A ) : NEW_LINE INDENT for c , val in enumerate ( row ) : NEW_LINE INDENT ans [ c ] [ r ] = val NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public int totalFruit ( int [ ] tree ) { int ans = 0 , i = 0 ; Counter count = new Counter ( ) ; for ( int j = 0 ; j < tree . length ; ++ j ) { count . add ( tree [ j ] , 1 ) ; while ( count . size ( ) >= 3 ) { count . add ( tree [ i ] , - 1 ) ; if ( count . get ( tree [ i ] ) == 0 ) count . remove ( tree [ i ] ) ; i ++ ; } ans = Math . max ( ans , j - i + 1 ) ; } return ans ; } } class Counter extends HashMap < Integer , Integer > { public int get ( int k ) { return containsKey ( k ) ? super . get ( k ) : 0 ; } public void add ( int k , int v ) { put ( k , get ( k ) + v ) ; } }

class Solution ( object ) : NEW_LINE INDENT def totalFruit ( self , tree ) : NEW_LINE INDENT ans = i = 0 NEW_LINE count = collections . Counter ( ) NEW_LINE for j , x in enumerate ( tree ) : NEW_LINE INDENT count [ x ] += 1 NEW_LINE while len ( count ) >= 3 : NEW_LINE INDENT count [ tree [ i ] ] -= 1 NEW_LINE if count [ tree [ i ] ] == 0 : NEW_LINE INDENT del count [ tree [ i ] ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT ans = max ( ans , j - i + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public boolean canConstruct ( String ransomNote , String magazine ) { int [ ] table = new int [ 128 ] ; for ( char c : magazine . toCharArray ( ) ) table [ c ] ++ ; for ( char c : ransomNote . toCharArray ( ) ) if ( -- table [ c ] < 0 ) return false ; return true ; } }

class Solution ( object ) : NEW_LINE INDENT def canConstruct ( self , ransomNote , magazine ) : NEW_LINE INDENT letter_map = { } NEW_LINE for letter in magazine : NEW_LINE INDENT letter_map [ letter ] = letter_map . get ( letter , 0 ) + 1 NEW_LINE DEDENT for letter in ransomNote : NEW_LINE INDENT letter_map [ letter ] = letter_map . get ( letter , 0 ) - 1 NEW_LINE if letter_map [ letter ] < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT DEDENT


class Solution { public ListNode reverseList ( ListNode head ) { ListNode newHead = null ; while ( head != null ) { ListNode next = head . next ; head . next = newHead ; newHead = head ; head = next ; } return newHead ; } }

class Solution ( object ) : NEW_LINE INDENT def reverseList ( self , head ) : NEW_LINE INDENT if head is None or head . next is None : NEW_LINE INDENT return head NEW_LINE DEDENT p = self . reverseList ( head . next ) NEW_LINE head . next . next = head NEW_LINE head . next = None NEW_LINE return p NEW_LINE DEDENT DEDENT


class Solution { public boolean enough ( int x , int m , int n , int k ) { int count = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { count += Math . min ( x / i , n ) ; } return count >= k ; } public int findKthNumber ( int m , int n , int k ) { int lo = 1 , hi = m * n ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! enough ( mi , m , n , k ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } }

class Solution : NEW_LINE INDENT def findKthNumber ( self , m : int , n : int , k : int ) -> int : NEW_LINE INDENT def enough ( x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT count += min ( x // i , n ) NEW_LINE DEDENT return count >= k NEW_LINE DEDENT lo , hi = 1 , m * n NEW_LINE while lo < hi : NEW_LINE INDENT mi = ( lo + hi ) // 2 NEW_LINE if not enough ( mi ) : NEW_LINE INDENT lo = mi + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mi NEW_LINE DEDENT DEDENT return lo NEW_LINE DEDENT DEDENT


class Solution { public List < Integer > findAnagrams ( String s , String p ) { List < Integer > list = new ArrayList < > ( ) ; if ( s == null || s . length ( ) == 0 || p == null || p . length ( ) == 0 ) return list ; int [ ] hash = new int [ 256 ] ; for ( char c : p . toCharArray ( ) ) { hash [ c ] ++ ; } int left = 0 , right = 0 , count = p . length ( ) ; while ( right < s . length ( ) ) { if ( hash [ s . charAt ( right ++ ) ] -- >= 1 ) count -- ; if ( count == 0 ) list . add ( left ) ; if ( right - left == p . length ( ) && hash [ s . charAt ( left ++ ) ] ++ >= 0 ) count ++ ; } return list ; } }

class Solution ( object ) : NEW_LINE INDENT def findAnagrams ( self , s , p ) : NEW_LINE INDENT res = [ ] NEW_LINE if s is None or p is None or len ( s ) == 0 or len ( p ) == 0 : NEW_LINE INDENT return res NEW_LINE DEDENT char_map = [ 0 ] * 256 NEW_LINE for c in p : NEW_LINE INDENT char_map [ ord ( c ) ] += 1 NEW_LINE DEDENT left , right , count = 0 , 0 , len ( p ) NEW_LINE while right < len ( s ) : NEW_LINE INDENT if char_map [ ord ( s [ right ] ) ] >= 1 : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT char_map [ ord ( s [ right ] ) ] -= 1 NEW_LINE right += 1 NEW_LINE if count == 0 : NEW_LINE INDENT res . append ( left ) NEW_LINE DEDENT if right - left == len ( p ) : NEW_LINE INDENT if char_map [ ord ( s [ left ] ) ] >= 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT char_map [ ord ( s [ left ] ) ] += 1 NEW_LINE left += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT DEDENT


public class Solution { public TreeNode mergeTrees ( TreeNode t1 , TreeNode t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . val += t2 . val ; t1 . left = mergeTrees ( t1 . left , t2 . left ) ; t1 . right = mergeTrees ( t1 . right , t2 . right ) ; return t1 ; } }

class Solution ( object ) : NEW_LINE INDENT def mergeTrees ( self , t1 , t2 ) : NEW_LINE INDENT if t1 is None : NEW_LINE INDENT return t2 NEW_LINE DEDENT if t2 is None : NEW_LINE INDENT return t1 NEW_LINE DEDENT t1 . val += t2 . val NEW_LINE t1 . left = self . mergeTrees ( t1 . left , t2 . left ) NEW_LINE t1 . right = self . mergeTrees ( t1 . right , t2 . right ) NEW_LINE return t1 NEW_LINE DEDENT DEDENT


import java . util . Arrays ; import java . util . Collections ; class Solution { public int minMoves ( int [ ] nums ) { if ( nums . length == 0 ) return 0 ; Arrays . sort ( nums ) ; int min_num = nums [ 0 ] ; int ans = 0 ; for ( int num : nums ) { ans += num - min_num ; } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def minMoves ( self , nums ) : NEW_LINE INDENT if nums is None or len ( nums ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT min_num = min ( nums ) NEW_LINE return sum ( [ i - min_num for i in nums ] ) NEW_LINE DEDENT DEDENT


class Solution { public int peakIndexInMountainArray ( int [ ] A ) { int lo = 0 , hi = A . length - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( A [ mid ] < A [ mid + 1 ] ) lo = mid + 1 ; else hi = mid ; } return lo ; } }

class Solution ( object ) : NEW_LINE INDENT def peakIndexInMountainArray ( self , A ) : NEW_LINE INDENT lo , hi = 0 , len ( A ) - 1 NEW_LINE while lo < hi : NEW_LINE INDENT mid = ( lo + hi ) / 2 NEW_LINE if A [ mid ] < A [ mid + 1 ] : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid NEW_LINE DEDENT DEDENT return lo NEW_LINE DEDENT DEDENT


class Solution { public String toLowerCase ( String str ) { return str . toLowerCase ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def toLowerCase ( self , str ) : NEW_LINE INDENT res = [ ] NEW_LINE gap = ord ( ' a ' ) - ord ( ' A ' ) NEW_LINE for c in str : NEW_LINE INDENT if ord ( c ) >= ord ( ' A ' ) and ord ( c ) <= ord ( ' Z ' ) : NEW_LINE INDENT res . append ( chr ( ord ( c ) + gap ) ) NEW_LINE DEDENT else : NEW_LINE INDENT res . append ( c ) NEW_LINE DEDENT DEDENT return ' ' . join ( res ) NEW_LINE DEDENT DEDENT


class Solution { public int maxAreaOfIsland ( int [ ] [ ] grid ) { int [ ] dr = new int [ ] { 1 , - 1 , 0 , 0 } ; int [ ] dc = new int [ ] { 0 , 0 , 1 , - 1 } ; int ans = 0 ; for ( int r0 = 0 ; r0 < grid . length ; r0 ++ ) { for ( int c0 = 0 ; c0 < grid [ 0 ] . length ; c0 ++ ) { if ( grid [ r0 ] [ c0 ] == 1 ) { int shape = 0 ; Stack < int [ ] > stack = new Stack ( ) ; stack . push ( new int [ ] { r0 , c0 } ) ; grid [ r0 ] [ c0 ] = 0 ; while ( ! stack . empty ( ) ) { int [ ] node = stack . pop ( ) ; int r = node [ 0 ] , c = node [ 1 ] ; shape ++ ; for ( int k = 0 ; k < 4 ; k ++ ) { int nr = r + dr [ k ] ; int nc = c + dc [ k ] ; if ( 0 <= nr && nr < grid . length && 0 <= nc && nc < grid [ 0 ] . length && grid [ nr ] [ nc ] == 1 ) { stack . push ( new int [ ] { nr , nc } ) ; grid [ nr ] [ nc ] = 0 ; } } } ans = Math . max ( ans , shape ) ; } } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def maxAreaOfIsland ( self , grid ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( grid ) ) : NEW_LINE INDENT for j in range ( len ( grid [ 0 ] ) ) : NEW_LINE INDENT if grid [ i ] [ j ] == 1 : NEW_LINE INDENT grid [ i ] [ j ] = 0 NEW_LINE ans = max ( self . dfs ( grid , i , j ) , ans ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT def dfs ( self , grid , i , j ) : NEW_LINE INDENT stack = [ ( i , j ) ] NEW_LINE area = 0 NEW_LINE while stack : NEW_LINE INDENT r , c = stack . pop ( - 1 ) NEW_LINE area += 1 NEW_LINE for nr , nc in ( ( r - 1 , c ) , ( r + 1 , c ) , ( r , c - 1 ) , ( r , c + 1 ) ) : NEW_LINE INDENT if ( 0 <= nr < len ( grid ) and 0 <= nc < len ( grid [ 0 ] ) and grid [ nr ] [ nc ] ) : NEW_LINE INDENT stack . append ( ( nr , nc ) ) NEW_LINE grid [ nr ] [ nc ] = 0 NEW_LINE DEDENT DEDENT DEDENT return area NEW_LINE DEDENT DEDENT


class Solution { public boolean validateStackSequences ( int [ ] pushed , int [ ] popped ) { Stack < Integer > inStack = new Stack < > ( ) ; int posPush = 0 , posPop = 0 ; while ( posPush != pushed . length ) { int curr = pushed [ posPush ] ; while ( ! inStack . empty ( ) && popped . length > 0 && inStack . peek ( ) == popped [ posPop ] ) { inStack . pop ( ) ; posPop ++ ; } if ( popped . length == 0 ) break ; if ( curr == popped [ posPop ] ) posPop ++ ; else inStack . push ( curr ) ; posPush ++ ; } while ( ! inStack . empty ( ) && popped . length > 0 && inStack . peek ( ) == popped [ posPop ] ) { inStack . pop ( ) ; posPop ++ ; } if ( inStack . empty ( ) ) return true ; return false ; } }

class Solution ( object ) : NEW_LINE INDENT def validateStackSequences ( self , pushed , popped ) : NEW_LINE INDENT in_stack = [ ] NEW_LINE pos = 0 NEW_LINE while pos != len ( pushed ) : NEW_LINE INDENT curr = pushed [ pos ] NEW_LINE while len ( in_stack ) > 0 and len ( popped ) > 0 and in_stack [ - 1 ] == popped [ 0 ] : NEW_LINE INDENT in_stack . pop ( - 1 ) NEW_LINE popped . pop ( 0 ) NEW_LINE DEDENT if len ( popped ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT if curr == popped [ 0 ] : NEW_LINE INDENT popped . pop ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT in_stack . append ( curr ) NEW_LINE DEDENT pos += 1 NEW_LINE DEDENT while len ( in_stack ) > 0 and len ( popped ) > 0 and in_stack [ - 1 ] == popped [ 0 ] : NEW_LINE INDENT in_stack . pop ( - 1 ) NEW_LINE popped . pop ( 0 ) NEW_LINE DEDENT if len ( in_stack ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . validateStackSequences ( [ 1 , 0 , 3 , 2 ] , [ 0 , 1 , 2 , 3 ] ) NEW_LINE DEDENT


class Solution { public int minMeetingRooms ( Interval [ ] intervals ) { int ans = 0 , curr = 0 ; List < TimePoint > timeline = new ArrayList < > ( ) ; for ( Interval interval : intervals ) { timeline . add ( new TimePoint ( interval . start , 1 ) ) ; timeline . add ( new TimePoint ( interval . end , - 1 ) ) ; } timeline . sort ( new Comparator < TimePoint > ( ) { public int compare ( TimePoint a , TimePoint b ) { if ( a . time != b . time ) return a . time - b . time ; else return a . room - b . room ; } } ) ; for ( TimePoint t : timeline ) { curr += t . room ; if ( curr >= ans ) ans = curr ; } return ans ; } private class TimePoint { int time ; int room ; TimePoint ( int time , int room ) { this . time = time ; this . room = room ; } } }

class Solution ( object ) : NEW_LINE INDENT def minMeetingRooms ( self , intervals ) : NEW_LINE INDENT timeline = [ ] NEW_LINE for interval in intervals : NEW_LINE INDENT timeline . append ( ( interval . start , 1 ) ) NEW_LINE timeline . append ( ( interval . end , - 1 ) ) NEW_LINE DEDENT timeline . sort ( ) NEW_LINE ans = curr = 0 NEW_LINE for _ , v in timeline : NEW_LINE INDENT curr += v NEW_LINE ans = max ( ans , curr ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public String longestWord ( String [ ] words ) { Trie trie = new Trie ( ) ; int index = 0 ; for ( String word : words ) { trie . insert ( word , ++ index ) ; } trie . words = words ; return trie . dfs ( ) ; } } class Node { char c ; HashMap < Character , Node > children = new HashMap ( ) ; int end ; public Node ( char c ) { this . c = c ; } } class Trie { Node root ; String [ ] words ; public Trie ( ) { root = new Node ( '0' ) ; } public void insert ( String word , int index ) { Node cur = root ; for ( char c : word . toCharArray ( ) ) { cur . children . putIfAbsent ( c , new Node ( c ) ) ; cur = cur . children . get ( c ) ; } cur . end = index ; } public String dfs ( ) { String ans = " " ; Stack < Node > stack = new Stack ( ) ; stack . push ( root ) ; while ( ! stack . empty ( ) ) { Node node = stack . pop ( ) ; if ( node . end > 0 || node == root ) { if ( node != root ) { String word = words [ node . end - 1 ] ; if ( word . length ( ) > ans . length ( ) || word . length ( ) == ans . length ( ) && word . compareTo ( ans ) < 0 ) { ans = word ; } } for ( Node nei : node . children . values ( ) ) { stack . push ( nei ) ; } } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def longestWord ( self , words ) : NEW_LINE INDENT Trie = lambda : collections . defaultdict ( Trie ) NEW_LINE trie = Trie ( ) NEW_LINE END = True NEW_LINE for i , word in enumerate ( words ) : NEW_LINE INDENT reduce ( dict . __getitem__ , word , trie ) [ END ] = i NEW_LINE DEDENT stack = trie . values ( ) NEW_LINE ans = " " NEW_LINE while stack : NEW_LINE INDENT cur = stack . pop ( ) NEW_LINE if END in cur : NEW_LINE INDENT word = words [ cur [ END ] ] NEW_LINE if len ( word ) > len ( ans ) or len ( word ) == len ( ans ) and word < ans : NEW_LINE INDENT ans = word NEW_LINE DEDENT stack . extend ( [ cur [ letter ] for letter in cur if letter != END ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


import java . util . ArrayList ; import java . util . List ; class MinStack { private Stack < Integer > stack ; private Stack < Integer > minStack ; public MinStack ( ) { stack = new Stack < > ( ) ; minStack = new Stack < > ( ) ; } public void push ( int x ) { stack . push ( x ) ; if ( minStack . size ( ) == 0 || x <= minStack . peek ( ) ) minStack . push ( x ) ; else minStack . push ( minStack . peek ( ) ) ; } public void pop ( ) { stack . pop ( ) ; minStack . pop ( ) ; } public int top ( ) { return stack . peek ( ) ; } public int getMin ( ) { return minStack . peek ( ) ; } }

class MinStack ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . stack = [ ] NEW_LINE self . min_stack = [ ] NEW_LINE DEDENT def push ( self , x ) : NEW_LINE INDENT self . stack . append ( x ) NEW_LINE if len ( self . min_stack ) == 0 : NEW_LINE INDENT self . min_stack . append ( x ) NEW_LINE return NEW_LINE DEDENT if x <= self . min_stack [ - 1 ] : NEW_LINE INDENT self . min_stack . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT self . min_stack . append ( self . min_stack [ - 1 ] ) NEW_LINE DEDENT DEDENT def pop ( self ) : NEW_LINE INDENT if len ( self . stack ) > 0 : NEW_LINE INDENT self . min_stack . pop ( ) NEW_LINE self . stack . pop ( ) NEW_LINE DEDENT DEDENT def top ( self ) : NEW_LINE INDENT if len ( self . stack ) > 0 : NEW_LINE INDENT return self . stack [ - 1 ] NEW_LINE DEDENT return None NEW_LINE DEDENT def getMin ( self ) : NEW_LINE INDENT if len ( self . min_stack ) > 0 : NEW_LINE INDENT return self . min_stack [ - 1 ] NEW_LINE DEDENT return None NEW_LINE DEDENT DEDENT


class Solution { public int minIncrementForUnique ( int [ ] A ) { if ( A . length == 0 ) return 0 ; HashSet < Integer > numSet = new HashSet < > ( ) ; List < Integer > duplicated = new ArrayList < > ( ) ; int res = 0 ; Arrays . sort ( A ) ; int left = A [ 0 ] ; int right = A [ A . length - 1 ] ; int holes = right - left + 1 ; for ( int v : A ) { if ( numSet . contains ( v ) ) duplicated . add ( v ) ; else numSet . add ( v ) ; } holes -= numSet . size ( ) ; for ( int i = left + 1 ; i < right ; i ++ ) { if ( holes == 0 || duplicated . size ( ) == 0 ) break ; if ( ! numSet . contains ( i ) && i > duplicated . get ( 0 ) ) { res += i - duplicated . get ( 0 ) ; holes -- ; duplicated . remove ( 0 ) ; } } if ( duplicated . size ( ) == 0 ) return res ; while ( duplicated . size ( ) != 0 ) { right += 1 ; res += right - duplicated . get ( 0 ) ; duplicated . remove ( 0 ) ; } return res ; } }

class Solution ( object ) : NEW_LINE INDENT def minIncrementForUnique ( self , A ) : NEW_LINE INDENT if A is None or len ( A ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE num_set = set ( ) NEW_LINE duplicate = [ ] NEW_LINE A . sort ( ) NEW_LINE left , right = A [ 0 ] , A [ - 1 ] NEW_LINE holes = right - left + 1 NEW_LINE for v in A : NEW_LINE INDENT if v in num_set : NEW_LINE INDENT duplicate . append ( v ) NEW_LINE DEDENT else : NEW_LINE INDENT num_set . add ( v ) NEW_LINE DEDENT DEDENT holes = holes - len ( num_set ) NEW_LINE for hole in range ( left + 1 , right ) : NEW_LINE INDENT if holes == 0 or len ( duplicate ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT if hole not in num_set and hole > duplicate [ 0 ] : NEW_LINE INDENT res += hole - duplicate . pop ( 0 ) NEW_LINE holes -= 1 NEW_LINE DEDENT DEDENT while len ( duplicate ) != 0 : NEW_LINE INDENT right += 1 NEW_LINE res += right - duplicate . pop ( 0 ) NEW_LINE DEDENT return res NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE DEDENT


class Solution { public boolean isPalindrome ( int x ) { if ( x < 0 ) return false ; int temp = x ; int len = 0 ; while ( temp != 0 ) { temp /= 10 ; len ++ ; } temp = x ; int left , right ; for ( int i = 0 ; i < len / 2 ; i ++ ) { right = temp % 10 ; left = temp / ( int ) Math . pow ( 10 , len - 2 * i - 1 ) ; left = left % 10 ; if ( left != right ) return false ; temp /= 10 ; } return true ; } public boolean isPalindrome ( int x ) { if ( x < 0 ) return false ; int div = 1 ; while ( x / div >= 10 ) { div *= 10 ; } while ( x != 0 ) { int l = x / div ; int r = x % 10 ; if ( l != r ) return false ; x = ( x % div ) / 10 ; div /= 100 ; } return true ; } } class Solution { public boolean isPalindrome ( int x ) { int r , s = 0 , number = x ; if ( number < 0 ) { return false ; } while ( number != 0 ) { r = number % 10 ; s = s * 10 + r ; number /= 10 ; } if ( s == x ) { return true ; } else { return false ; } } }

class Solution ( object ) : NEW_LINE INDENT def isPalindrome ( self , x ) : NEW_LINE INDENT if x < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT ls = len ( str ( x ) ) NEW_LINE tmp = x NEW_LINE for i in range ( int ( ls / 2 ) ) : NEW_LINE INDENT right = int ( tmp % 10 ) NEW_LINE left = tmp / ( 10 ** ( ls - 2 * i - 1 ) ) NEW_LINE left = int ( left % 10 ) NEW_LINE if left != right : NEW_LINE INDENT return False NEW_LINE DEDENT tmp = tmp // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . isPalindrome ( 1001 ) NEW_LINE DEDENT


class Solution { public List < String > readBinaryWatch ( int num ) { List < String > times = new ArrayList < > ( ) ; for ( int h = 0 ; h < 12 ; h ++ ) for ( int m = 0 ; m < 60 ; m ++ ) if ( Integer . bitCount ( h * 64 + m ) == num ) times . add ( String . format ( " % d : %02d " , h , m ) ) ; return times ; } }

class Solution ( object ) : NEW_LINE INDENT def readBinaryWatch ( self , num ) : NEW_LINE INDENT return [ ' % d : %02d ' % ( h , m ) for h in range ( 12 ) for m in range ( 60 ) if ( bin ( h ) + bin ( m ) ) . count ( '1' ) == num ] NEW_LINE DEDENT DEDENT


class Solution { public int fixedPoint ( int [ ] A ) { int l = 0 ; int h = A . length ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( A [ mid ] > mid ) h = mid - 1 ; else if ( A [ mid ] < mid ) l = mid + 1 ; else return mid ; } return - 1 ; } }

class Solution ( object ) : NEW_LINE INDENT def fixedPoint ( self , A ) : NEW_LINE INDENT l , h = 0 , len ( A ) - 1 NEW_LINE while l <= h : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if A [ mid ] < mid : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT elif A [ mid ] > mid : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return mid NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT DEDENT


import java . util . ArrayList ; import java . util . List ; class Solution { public List < String > fizzBuzz ( int n ) { List < String > res = new ArrayList < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { String tmp = " " ; if ( i % 3 == 0 ) tmp += " Fizz " ; if ( i % 5 == 0 ) tmp += " Buzz " ; if ( tmp . length ( ) == 0 ) tmp += String . valueOf ( i ) ; res . add ( tmp ) ; } return res ; } }

class Solution ( object ) : NEW_LINE INDENT def fizzBuzz ( self , n ) : NEW_LINE INDENT return [ str ( i ) * ( i % 3 != 0 and i % 5 != 0 ) + " Fizz " * ( i % 3 == 0 ) + " Buzz " * ( i % 5 == 0 ) for i in range ( 1 , n + 1 ) ] NEW_LINE DEDENT DEDENT


public class Solution { public String longestPalindrome ( String s ) { int start = 0 , end = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int len1 = expandAroundCenter ( s , i , i ) ; int len2 = expandAroundCenter ( s , i , i + 1 ) ; int len = Math . max ( len1 , len2 ) ; if ( len > end - start ) { start = i - ( len - 1 ) / 2 ; end = i + len / 2 ; } } return s . substring ( start , end + 1 ) ; } private int expandAroundCenter ( String s , int left , int right ) { int L = left , R = right ; while ( L >= 0 && R < s . length ( ) && s . charAt ( L ) == s . charAt ( R ) ) { L -- ; R ++ ; } return R - L - 1 ; } }

class Solution ( object ) : NEW_LINE INDENT def longestPalindrome ( self , s ) : NEW_LINE INDENT ls = len ( s ) NEW_LINE if ls <= 1 or len ( set ( s ) ) == 1 : NEW_LINE INDENT return s NEW_LINE DEDENT temp_s = ' # ' . join ( ' { } ' . format ( s ) ) NEW_LINE tls = len ( temp_s ) NEW_LINE seed = range ( 1 , tls - 1 ) NEW_LINE len_table = [ 0 ] * tls NEW_LINE for step in range ( 1 , tls / 2 + 1 ) : NEW_LINE INDENT final = [ ] NEW_LINE for pos in seed : NEW_LINE INDENT if pos - step < 0 or pos + step >= tls : NEW_LINE INDENT continue NEW_LINE DEDENT if temp_s [ pos - step ] != temp_s [ pos + step ] : NEW_LINE INDENT continue NEW_LINE DEDENT final . append ( pos ) NEW_LINE if temp_s [ pos - step ] == ' # ' : NEW_LINE INDENT continue NEW_LINE DEDENT len_table [ pos ] = step NEW_LINE DEDENT seed = final NEW_LINE DEDENT max_pos , max_step = 0 , 0 NEW_LINE for i , s in enumerate ( len_table ) : NEW_LINE INDENT if s >= max_step : NEW_LINE INDENT max_step = s NEW_LINE max_pos = i NEW_LINE DEDENT DEDENT return temp_s [ max_pos - max_step : max_pos + max_step + 1 ] . translate ( None , ' # ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . longestPalindrome ( " abcbe " ) NEW_LINE DEDENT


class Solution { public boolean isOneBitCharacter ( int [ ] bits ) { int pos = 0 ; while ( pos < bits . length - 1 ) { pos += bits [ pos ] + 1 ; } return pos == bits . length - 1 ; } }

class Solution : NEW_LINE INDENT def isOneBitCharacter ( self , bits : List [ int ] ) -> bool : NEW_LINE INDENT pos = 0 NEW_LINE while pos < len ( bits ) - 1 : NEW_LINE INDENT pos += bits [ pos ] + 1 NEW_LINE DEDENT return pos == len ( bits ) - 1 NEW_LINE DEDENT DEDENT


public class Solution { private static final int maxDiv10 = Integer . MAX_VALUE / 10 ; public int myAtoi ( String str ) { int i = 0 , n = str . length ( ) ; while ( i < n && Character . isWhitespace ( str . charAt ( i ) ) ) i ++ ; int sign = 1 ; if ( i < n && str . charAt ( i ) == ' + ' ) i ++ ; else if ( i < n && str . charAt ( i ) == ' - ' ) { sign = - 1 ; i ++ ; } int num = 0 ; while ( i < n && Character . isDigit ( str . charAt ( i ) ) ) { int digit = Character . getNumericValue ( str . charAt ( i ) ) ; if ( num > maxDiv10 || num == maxDiv10 && digit >= 8 ) return sign == 1 ? Integer . MAX_VALUE : Integer . MIN_VALUE ; num = num * 10 + digit ; i ++ ; } return sign * num ; } }

class Solution ( object ) : NEW_LINE INDENT def myAtoi ( self , str ) : NEW_LINE INDENT sign = 1 NEW_LINE max_int , min_int = 2147483647 , - 2147483648 NEW_LINE result , pos = 0 , 0 NEW_LINE ls = len ( str ) NEW_LINE while pos < ls and str [ pos ] == ' ▁ ' : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT if pos < ls and str [ pos ] == ' - ' : NEW_LINE INDENT sign = - 1 NEW_LINE pos += 1 NEW_LINE DEDENT elif pos < ls and str [ pos ] == ' + ' : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT while pos < ls and ord ( str [ pos ] ) >= ord ( '0' ) and ord ( str [ pos ] ) <= ord ( '9' ) : NEW_LINE INDENT num = ord ( str [ pos ] ) - ord ( '0' ) NEW_LINE if result > max_int / 10 or ( result == max_int / 10 and num >= 8 ) : NEW_LINE INDENT if sign == - 1 : NEW_LINE INDENT return min_int NEW_LINE DEDENT return max_int NEW_LINE DEDENT result = result * 10 + num NEW_LINE pos += 1 NEW_LINE DEDENT return sign * result NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . myAtoi ( " + - 2" ) NEW_LINE DEDENT


class Solution { public String mostCommonWord ( String paragraph , String [ ] banned ) { paragraph += " . " ; Set < String > banset = new HashSet ( ) ; for ( String word : banned ) banset . add ( word ) ; Map < String , Integer > count = new HashMap ( ) ; String ans = " " ; int ansfreq = 0 ; StringBuilder word = new StringBuilder ( ) ; for ( char c : paragraph . toCharArray ( ) ) { if ( Character . isLetter ( c ) ) { word . append ( Character . toLowerCase ( c ) ) ; } else if ( word . length ( ) > 0 ) { String finalword = word . toString ( ) ; if ( ! banset . contains ( finalword ) ) { count . put ( finalword , count . getOrDefault ( finalword , 0 ) + 1 ) ; if ( count . get ( finalword ) > ansfreq ) { ans = finalword ; ansfreq = count . get ( finalword ) ; } } word = new StringBuilder ( ) ; } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def mostCommonWord ( self , paragraph , banned ) : NEW_LINE INDENT banned = set ( banned ) NEW_LINE count = collections . Counter ( word for word in re . split ( ' [ ▁ ! ? \ ' , ; . ] ' , paragraph . lower ( ) ) if word ) NEW_LINE return max ( ( item for item in count . items ( ) if item [ 0 ] not in banned ) , key = operator . itemgetter ( 1 ) ) [ 0 ] NEW_LINE DEDENT DEDENT


public class Solution { HashSet < String > trees = new HashSet < > ( ) ; public boolean isSubtree ( TreeNode s , TreeNode t ) { String tree1 = preorder ( s , true ) ; String tree2 = preorder ( t , true ) ; return tree1 . indexOf ( tree2 ) >= 0 ; } public String preorder ( TreeNode t , boolean left ) { if ( t == null ) { if ( left ) return " lnull " ; else return " rnull " ; } return " # " + t . val + " ▁ " + preorder ( t . left , true ) + " ▁ " + preorder ( t . right , false ) ; } }

class Solution ( object ) : NEW_LINE INDENT def isSubtree ( self , s , t ) : NEW_LINE INDENT s_res = self . preorder ( s , True ) NEW_LINE t_res = self . preorder ( t , True ) NEW_LINE return t_res in s_res NEW_LINE DEDENT def preorder ( self , root , isLeft ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT if isLeft : NEW_LINE INDENT return " lnull " NEW_LINE DEDENT else : NEW_LINE INDENT return " rnull " NEW_LINE DEDENT DEDENT return " # " + str ( root . val ) + " ▁ " + self . preorder ( root . left , True ) + " ▁ " + self . preorder ( root . right , False ) NEW_LINE DEDENT DEDENT


class Solution { public List < Integer > topKFrequent ( int [ ] nums , int k ) { HashMap < Integer , Integer > count = new HashMap ( ) ; for ( int n : nums ) { count . put ( n , count . getOrDefault ( n , 0 ) + 1 ) ; } PriorityQueue < Integer > heap = new PriorityQueue < Integer > ( ( n1 , n2 ) -> count . get ( n1 ) - count . get ( n2 ) ) ; for ( int n : count . keySet ( ) ) { heap . add ( n ) ; if ( heap . size ( ) > k ) heap . poll ( ) ; } List < Integer > top_k = new LinkedList ( ) ; while ( ! heap . isEmpty ( ) ) top_k . add ( heap . poll ( ) ) ; Collections . reverse ( top_k ) ; return top_k ; } }

class Solution ( object ) : NEW_LINE INDENT def topKFrequent ( self , nums , k ) : NEW_LINE INDENT counter = collections . Counter ( nums ) NEW_LINE return [ k for k , v in counter . most_common ( k ) ] NEW_LINE DEDENT DEDENT


class Solution { public int poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { int n = minutesToTest / minutesToDie + 1 ; int pigs = 0 ; while ( Math . pow ( n , pigs ) < buckets ) pigs ++ ; return pigs ; } }

class Solution ( object ) : NEW_LINE INDENT def poorPigs ( self , buckets , minutesToDie , minutesToTest ) : NEW_LINE INDENT pigs = 0 NEW_LINE while ( minutesToTest / minutesToDie + 1 ) ** pigs < buckets : NEW_LINE INDENT pigs += 1 NEW_LINE DEDENT return pigs NEW_LINE DEDENT DEDENT


import java . util . List ; class Solution { public String [ ] reorderLogFiles ( String [ ] logs ) { Arrays . sort ( logs , ( log1 , log2 ) -> { String [ ] split1 = log1 . split ( " ▁ " , 2 ) ; String [ ] split2 = log2 . split ( " ▁ " , 2 ) ; boolean isDigit1 = Character . isDigit ( split1 [ 1 ] . charAt ( 0 ) ) ; boolean isDigit2 = Character . isDigit ( split2 [ 1 ] . charAt ( 0 ) ) ; if ( ! isDigit1 && ! isDigit2 ) { int cmp = split1 [ 1 ] . compareTo ( split2 [ 1 ] ) ; if ( cmp != 0 ) return cmp ; return split1 [ 0 ] . compareTo ( split2 [ 0 ] ) ; } return isDigit1 ? ( isDigit2 ? 0 : 1 ) : - 1 ; } ) ; return logs ; } }

class Solution ( object ) : NEW_LINE INDENT def reorderLogFiles ( self , logs ) : NEW_LINE INDENT letter_logs = [ ] NEW_LINE digit_logs = [ ] NEW_LINE for log in logs : NEW_LINE INDENT if log . split ( ' ▁ ' ) [ 1 ] . isnumeric ( ) : NEW_LINE INDENT digit_logs . append ( log ) NEW_LINE DEDENT else : NEW_LINE INDENT letter_logs . append ( log ) NEW_LINE DEDENT DEDENT return sorted ( letter_logs , key = lambda x : x . split ( ' ▁ ' ) [ 1 : ] + x . split ( ' ▁ ' ) [ 0 ] ) + digit_logs NEW_LINE DEDENT DEDENT


import java . util . Stack ; import javax . swing . tree . TreeNode ; class Solution { public int sumOfLeftLeaves ( TreeNode root ) { int res = 0 ; Stack < TreeNode > stack = new Stack < > ( ) ; stack . push ( root ) ; while ( ! stack . isEmpty ( ) ) { TreeNode node = stack . pop ( ) ; if ( node != null ) { if ( node . left != null && node . left . left == null && node . left . right == null ) res += node . left . val ; stack . push ( node . right ) ; stack . push ( node . left ) ; } } return res ; } }

class Solution ( object ) : NEW_LINE INDENT def sumOfLeftLeaves ( self , root ) : NEW_LINE INDENT stack = [ root ] NEW_LINE res = 0 NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT curr = stack . pop ( 0 ) NEW_LINE if curr is not None : NEW_LINE INDENT if curr . left is not None : NEW_LINE INDENT if curr . left . left is None and curr . left . right is None : NEW_LINE INDENT res += curr . left . val NEW_LINE DEDENT DEDENT stack . insert ( 0 , curr . right ) NEW_LINE stack . insert ( 0 , curr . left ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT DEDENT


class Solution { public int countPrimes ( int n ) { boolean [ ] isPrime = new boolean [ n ] ; int count = 0 ; Arrays . fill ( isPrime , true ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( i * i >= n ) break ; if ( ! isPrime [ i ] ) continue ; for ( int j = i * i ; j < n ; j += i ) isPrime [ j ] = false ; } for ( int i = 2 ; i < n ; i ++ ) if ( isPrime [ i ] ) count ++ ; return count ; } }

class Solution ( object ) : NEW_LINE INDENT def countPrimes ( self , n ) : NEW_LINE INDENT isPrime = [ True ] * n NEW_LINE for i in xrange ( 2 , n ) : NEW_LINE INDENT if i * i >= n : NEW_LINE INDENT break NEW_LINE DEDENT if not isPrime [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT for j in xrange ( i * i , n , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in xrange ( 2 , n ) : NEW_LINE INDENT if isPrime [ i ] : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT DEDENT


class Solution { public boolean backspaceCompare ( String S , String T ) { return trans ( S ) . equals ( trans ( T ) ) ; } private String trans ( String str ) { StringBuilder sb = new StringBuilder ( ) ; for ( char c : str . toCharArray ( ) ) { if ( c != ' # ' ) { sb . append ( c ) ; } else if ( sb . length ( ) > 0 ) { sb . deleteCharAt ( sb . length ( ) - 1 ) ; } } return sb . toString ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def backspaceCompare ( self , S , T ) : NEW_LINE INDENT if S == T : NEW_LINE INDENT return True NEW_LINE DEDENT s_stack = [ ] NEW_LINE t_stack = [ ] NEW_LINE for c in S : NEW_LINE INDENT if c != ' # ' : NEW_LINE INDENT s_stack . append ( c ) NEW_LINE DEDENT elif len ( s_stack ) != 0 : NEW_LINE INDENT s_stack . pop ( - 1 ) NEW_LINE DEDENT DEDENT for c in T : NEW_LINE INDENT if c != ' # ' : NEW_LINE INDENT t_stack . append ( c ) NEW_LINE DEDENT elif len ( t_stack ) != 0 : NEW_LINE INDENT t_stack . pop ( - 1 ) NEW_LINE DEDENT DEDENT return ' ' . join ( s_stack ) == ' ' . join ( t_stack ) NEW_LINE DEDENT DEDENT


class Solution { Map < Integer , Integer > dist ; public int networkDelayTime ( int [ ] [ ] times , int N , int K ) { Map < Integer , List < int [ ] > > graph = new HashMap ( ) ; for ( int [ ] edge : times ) { if ( ! graph . containsKey ( edge [ 0 ] ) ) graph . put ( edge [ 0 ] , new ArrayList < int [ ] > ( ) ) ; graph . get ( edge [ 0 ] ) . add ( new int [ ] { edge [ 1 ] , edge [ 2 ] } ) ; } dist = new HashMap ( ) ; for ( int node = 1 ; node <= N ; ++ node ) dist . put ( node , Integer . MAX_VALUE ) ; dist . put ( K , 0 ) ; boolean [ ] seen = new boolean [ N + 1 ] ; while ( true ) { int candNode = - 1 ; int candDist = Integer . MAX_VALUE ; for ( int i = 1 ; i <= N ; ++ i ) { if ( ! seen [ i ] && dist . get ( i ) < candDist ) { candDist = dist . get ( i ) ; candNode = i ; } } if ( candNode < 0 ) break ; seen [ candNode ] = true ; if ( graph . containsKey ( candNode ) ) for ( int [ ] info : graph . get ( candNode ) ) dist . put ( info [ 0 ] , Math . min ( dist . get ( info [ 0 ] ) , dist . get ( candNode ) + info [ 1 ] ) ) ; } int ans = 0 ; for ( int cand : dist . values ( ) ) { if ( cand == Integer . MAX_VALUE ) return - 1 ; ans = Math . max ( ans , cand ) ; } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def networkDelayTime ( self , times , N , K ) : NEW_LINE INDENT graph = collections . defaultdict ( list ) NEW_LINE for u , v , w in times : NEW_LINE INDENT graph [ u ] . append ( ( v , w ) ) NEW_LINE DEDENT dist = { node : float ( ' inf ' ) for node in xrange ( 1 , N + 1 ) } NEW_LINE seen = [ False ] * ( N + 1 ) NEW_LINE dist [ K ] = 0 NEW_LINE while True : NEW_LINE INDENT cand_node = - 1 NEW_LINE cand_dist = float ( ' inf ' ) NEW_LINE for i in xrange ( 1 , N + 1 ) : NEW_LINE INDENT if not seen [ i ] and dist [ i ] < cand_dist : NEW_LINE INDENT cand_dist = dist [ i ] NEW_LINE cand_node = i NEW_LINE DEDENT DEDENT if cand_node < 0 : break NEW_LINE seen [ cand_node ] = True NEW_LINE for nei , d in graph [ cand_node ] : NEW_LINE INDENT dist [ nei ] = min ( dist [ nei ] , dist [ cand_node ] + d ) NEW_LINE DEDENT DEDENT ans = max ( dist . values ( ) ) NEW_LINE return ans if ans < float ( ' inf ' ) else - 1 NEW_LINE DEDENT DEDENT


class Solution { private final String [ ] LESS_THAN_20 = { " " , " One " , " Two " , " Three " , " Four " , " Five " , " Six " , " Seven " , " Eight " , " Nine " , " Ten " , " Eleven " , " Twelve " , " Thirteen " , " Fourteen " , " Fifteen " , " Sixteen " , " Seventeen " , " Eighteen " , " Nineteen " } ; private final String [ ] TENS = { " " , " Ten " , " Twenty " , " Thirty " , " Forty " , " Fifty " , " Sixty " , " Seventy " , " Eighty " , " Ninety " } ; private final String [ ] THOUSANDS = { " " , " Thousand " , " Million " , " Billion " } ; public String numberToWords ( int num ) { if ( num == 0 ) return " Zero " ; int i = 0 ; String words = " " ; while ( num > 0 ) { if ( num % 1000 != 0 ) words = helper ( num % 1000 ) + THOUSANDS [ i ] + " ▁ " + words ; num /= 1000 ; i ++ ; } return words . trim ( ) ; } private String helper ( int num ) { if ( num == 0 ) return " " ; else if ( num < 20 ) return LESS_THAN_20 [ num ] + " ▁ " ; else if ( num < 100 ) return TENS [ num / 10 ] + " ▁ " + helper ( num % 10 ) ; else return LESS_THAN_20 [ num / 100 ] + " ▁ Hundred ▁ " + helper ( num % 100 ) ; } }

class Solution ( object ) : NEW_LINE INDENT def numberToWords ( self , num ) : NEW_LINE INDENT to19 = ' One ▁ Two ▁ Three ▁ Four ▁ Five ▁ Six ▁ Seven ▁ Eight ▁ Nine ▁ Ten ▁ Eleven ▁ Twelve ▁ ' ' Thirteen ▁ Fourteen ▁ Fifteen ▁ Sixteen ▁ Seventeen ▁ Eighteen ▁ Nineteen ' . split ( ) NEW_LINE tens = ' Twenty ▁ Thirty ▁ Forty ▁ Fifty ▁ Sixty ▁ Seventy ▁ Eighty ▁ Ninety ' . split ( ) NEW_LINE def words ( n ) : NEW_LINE INDENT if n < 20 : NEW_LINE INDENT return to19 [ n - 1 : n ] NEW_LINE DEDENT if n < 100 : NEW_LINE INDENT return [ tens [ n / 10 - 2 ] ] + words ( n % 10 ) NEW_LINE DEDENT if n < 1000 : NEW_LINE INDENT return [ to19 [ n / 100 - 1 ] ] + [ ' Hundred ' ] + words ( n % 100 ) NEW_LINE DEDENT for p , w in enumerate ( ( ' Thousand ' , ' Million ' , ' Billion ' ) , 1 ) : NEW_LINE INDENT if n < 1000 ** ( p + 1 ) : NEW_LINE INDENT return words ( n / 1000 ** p ) + [ w ] + words ( n % 1000 ** p ) NEW_LINE DEDENT DEDENT DEDENT return ' ▁ ' . join ( words ( num ) ) or ' Zero ' NEW_LINE DEDENT DEDENT


class Solution { public ListNode middleNode ( ListNode head ) { ListNode fast , slow ; fast = slow = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } return slow ; } }

class Solution ( object ) : NEW_LINE INDENT def middleNode ( self , head ) : NEW_LINE INDENT fast = slow = head NEW_LINE while fast and fast . next : NEW_LINE INDENT slow = slow . next NEW_LINE fast = fast . next . next NEW_LINE DEDENT return slow NEW_LINE DEDENT DEDENT


class Solution { public int islandPerimeter ( int [ ] [ ] grid ) { int islands = 0 , neighbours = 0 ; for ( int i = 0 ; i < grid . length ; i ++ ) { for ( int j = 0 ; j < grid [ i ] . length ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { islands ++ ; if ( i < grid . length - 1 && grid [ i + 1 ] [ j ] == 1 ) neighbours ++ ; if ( j < grid [ i ] . length - 1 && grid [ i ] [ j + 1 ] == 1 ) neighbours ++ ; } } } return islands * 4 - neighbours * 2 ; } }

class Solution ( object ) : NEW_LINE INDENT def islandPerimeter ( self , grid ) : NEW_LINE INDENT row_num = len ( grid ) NEW_LINE if row_num == 0 | | len ( grid [ 0 ] ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT islands , overlaps = 0 , 0 NEW_LINE col_num = len ( grid [ 0 ] ) NEW_LINE for i in range ( row_num ) : NEW_LINE INDENT for j in range ( col_num ) : NEW_LINE INDENT if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT islands += 1 NEW_LINE if ( i < row_num - 1 & & grid [ i + 1 ] [ j ] == 1 ) : NEW_LINE INDENT overlaps += 1 NEW_LINE DEDENT if ( j < col_num - 1 & & grid [ i ] [ j + 1 ] == 1 ) : NEW_LINE INDENT overlaps += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return islands * 4 - overlaps * 2 NEW_LINE DEDENT DEDENT


public class Solution { public int [ ] twoSum ( int [ ] nums , int target ) { Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { int x = nums [ i ] ; if ( map . containsKey ( target - x ) ) { return new int [ ] { map . get ( target - x ) , i } ; } map . put ( x , i ) ; } throw new IllegalArgumentException ( " No ▁ two ▁ sum ▁ solution " ) ; } }

class Solution ( object ) : NEW_LINE INDENT def twoSum ( self , nums , target ) : NEW_LINE INDENT nums_index = [ ( v , index ) for index , v in enumerate ( nums ) ] NEW_LINE nums_index . sort ( ) NEW_LINE begin , end = 0 , len ( nums ) - 1 NEW_LINE while begin < end : NEW_LINE INDENT curr = nums_index [ begin ] [ 0 ] + nums_index [ end ] [ 0 ] NEW_LINE if curr == target : NEW_LINE INDENT return [ nums_index [ begin ] [ 1 ] , nums_index [ end ] [ 1 ] ] NEW_LINE DEDENT elif curr < target : NEW_LINE INDENT begin += 1 NEW_LINE DEDENT else : NEW_LINE INDENT end -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . twoSum ( [ 3 , 2 , 4 ] , 6 ) NEW_LINE DEDENT


public class Solution { public int lengthOfLongestSubstring ( String s ) { int [ ] charMap = new int [ 256 ] ; Arrays . fill ( charMap , - 1 ) ; int i = 0 , maxLen = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( charMap [ s . charAt ( j ) ] >= i ) { i = charMap [ s . charAt ( j ) ] + 1 ; } charMap [ s . charAt ( j ) ] = j ; maxLen = Math . max ( j - i + 1 , maxLen ) ; } return maxLen ; } }

class Solution ( object ) : NEW_LINE INDENT def lengthOfLongestSubstring ( self , s ) : NEW_LINE INDENT charMap = { } NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT charMap [ i ] = - 1 NEW_LINE DEDENT ls = len ( s ) NEW_LINE i = max_len = 0 NEW_LINE for j in range ( ls ) : NEW_LINE INDENT if charMap [ ord ( s [ j ] ) ] >= i : NEW_LINE INDENT i = charMap [ ord ( s [ j ] ) ] + 1 NEW_LINE DEDENT charMap [ ord ( s [ j ] ) ] = j NEW_LINE max_len = max ( max_len , j - i + 1 ) NEW_LINE DEDENT return max_len NEW_LINE DEDENT DEDENT


import java . util . HashSet ; class Solution { public int numJewelsInStones ( String J , String S ) { int result = 0 ; HashSet jHash = new HashSet < > ( ) ; for ( int j = 0 ; j < J . length ( ) ; j ++ ) { jHash . add ( J . charAt ( j ) ) ; } for ( int s = 0 ; s < S . length ( ) ; s ++ ) { if ( jHash . contains ( S . charAt ( s ) ) ) { result ++ ; } } return result ; } }

class Solution ( object ) : NEW_LINE INDENT def numJewelsInStones ( self , J , S ) : NEW_LINE INDENT if len ( J ) == 0 or len ( S ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT j_set = set ( J ) NEW_LINE ans = 0 NEW_LINE for c in S : NEW_LINE INDENT if c in j_set : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public TreeNode convertBST ( TreeNode root ) { int sum = 0 ; TreeNode node = root ; Stack < TreeNode > stack = new Stack < TreeNode > ( ) ; while ( ! stack . isEmpty ( ) || node != null ) { while ( node != null ) { stack . add ( node ) ; node = node . right ; } node = stack . pop ( ) ; sum += node . val ; node . val = sum ; node = node . left ; } return root ; } }

class Solution ( object ) : NEW_LINE INDENT def convertBST ( self , root ) : NEW_LINE INDENT total = 0 NEW_LINE node = root NEW_LINE stack = [ ] NEW_LINE while stack or node is not None : NEW_LINE INDENT while node is not None : NEW_LINE INDENT stack . append ( node ) NEW_LINE node = node . right NEW_LINE DEDENT node = stack . pop ( ) NEW_LINE total += node . val NEW_LINE node . val = total NEW_LINE node = node . left NEW_LINE DEDENT return root NEW_LINE DEDENT DEDENT


public class Solution { public boolean canPlaceFlowers ( int [ ] flowerbed , int n ) { int count = 0 , curr ; for ( int i = 0 ; i < flowerbed . length ; i ++ ) { curr = flowerbed [ i ] ; if ( i - 1 >= 0 ) curr += flowerbed [ i - 1 ] ; if ( i + 1 < flowerbed . length ) curr += flowerbed [ i + 1 ] ; if ( curr == 0 ) { count ++ ; flowerbed [ i ] = 1 ; } if ( count >= n ) return true ; } return false ; } }

class Solution ( object ) : NEW_LINE INDENT def canPlaceFlowers ( self , flowerbed , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( flowerbed ) ) : NEW_LINE INDENT curr = flowerbed [ i ] NEW_LINE if i - 1 >= 0 : NEW_LINE INDENT curr += flowerbed [ i - 1 ] NEW_LINE DEDENT if i + 1 < len ( flowerbed ) : NEW_LINE INDENT curr += flowerbed [ i + 1 ] NEW_LINE DEDENT if curr == 0 : NEW_LINE INDENT count += 1 NEW_LINE flowerbed [ i ] = 1 NEW_LINE if count >= n : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT DEDENT


class Solution { public int [ ] xorQueries ( int [ ] arr , int [ ] [ ] queries ) { int [ ] res = new int [ queries . length ] , q ; for ( int i = 1 ; i < arr . length ; i ++ ) arr [ i ] ^= arr [ i - 1 ] ; for ( int i = 0 ; i < queries . length ; i ++ ) { q = queries [ i ] ; res [ i ] = q [ 0 ] > 0 ? arr [ q [ 0 ] - 1 ] ^ arr [ q [ 1 ] ] : arr [ q [ 1 ] ] ; } return res ; } }

class Solution : NEW_LINE INDENT def xorQueries ( self , arr : List [ int ] , queries : List [ List [ int ] ] ) -> List [ int ] : NEW_LINE INDENT pref = [ 0 ] NEW_LINE for e in arr : NEW_LINE INDENT pref . append ( e ^ pref [ - 1 ] ) NEW_LINE DEDENT ans = [ ] NEW_LINE for [ l , r ] in queries : NEW_LINE INDENT ans . append ( pref [ r + 1 ] ^ pref [ l ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { private int result ; private HashMap < Integer , Integer > cache ; public int pathSum ( TreeNode root , int sum ) { result = 0 ; cache = new HashMap < Integer , Integer > ( ) ; cache . put ( 0 , 1 ) ; pathSumHelper ( root , sum , 0 ) ; return result ; } private void pathSumHelper ( TreeNode root , int target , int soFar ) { if ( root != null ) { int complement = soFar + root . val - target ; if ( cache . containsKey ( complement ) ) result += cache . get ( complement ) ; cache . put ( soFar + root . val , cache . getOrDefault ( soFar + root . val , 0 ) + 1 ) ; pathSumHelper ( root . left , target , soFar + root . val ) ; pathSumHelper ( root . right , target , soFar + root . val ) ; cache . put ( soFar + root . val , cache . get ( soFar + root . val ) - 1 ) ; } } }

class Solution ( object ) : NEW_LINE INDENT def pathSumHelper ( self , root , target , so_far , cache ) : NEW_LINE INDENT if root : NEW_LINE INDENT complement = so_far + root . val - target NEW_LINE if complement in cache : NEW_LINE INDENT self . result += cache [ complement ] NEW_LINE DEDENT cache [ so_far + root . val ] = cache . get ( so_far + root . val , 0 ) + 1 NEW_LINE self . pathSumHelper ( root . left , target , so_far + root . val , cache ) NEW_LINE self . pathSumHelper ( root . right , target , so_far + root . val , cache ) NEW_LINE cache [ so_far + root . val ] -= 1 NEW_LINE DEDENT return NEW_LINE DEDENT def pathSum ( self , root , sum ) : NEW_LINE INDENT self . result = 0 NEW_LINE self . pathSumHelper ( root , sum , 0 , { 0 : 1 } ) NEW_LINE return self . result NEW_LINE DEDENT DEDENT


class Solution { public int maximum69Number ( int num ) { return Integer . valueOf ( String . valueOf ( num ) . replaceFirst ( "6" , "9" ) ) ; } }

class Solution : NEW_LINE INDENT def maximum69Number ( self , num : int ) -> int : NEW_LINE INDENT return ( str ( num ) . replace ( '6' , '9' , 1 ) ) NEW_LINE DEDENT DEDENT


import java . util . HashSet ; class Solution { public int numUniqueEmails ( String [ ] emails ) { HashSet < String > emailSet = new HashSet < > ( ) ; for ( String email : emails ) { String firstSplit [ ] = email . split ( " @ " ) ; String secondSplit [ ] = firstSplit [ 0 ] . replaceAll ( " . " , " " ) . split ( " [ + ] " ) ; emailSet . add ( secondSplit [ 0 ] + firstSplit [ 1 ] ) ; } return emailSet . size ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def numUniqueEmails ( self , emails ) : NEW_LINE INDENT email_set = set ( ) NEW_LINE for email in emails : NEW_LINE INDENT elements = email . split ( ' @ ' ) NEW_LINE email_set . add ( elements [ 0 ] . split ( ' + ' ) [ 0 ] . replace ( ' . ' , ' ' ) + elements [ 1 ] ) NEW_LINE DEDENT return len ( email_set ) NEW_LINE DEDENT DEDENT


class Solution { public boolean isRectangleOverlap ( int [ ] rec1 , int [ ] rec2 ) { return ( Math . min ( rec1 [ 2 ] , rec2 [ 2 ] ) > Math . max ( rec1 [ 0 ] , rec2 [ 0 ] ) && Math . min ( rec1 [ 3 ] , rec2 [ 3 ] ) > Math . max ( rec1 [ 1 ] , rec2 [ 1 ] ) ) ; } }

class Solution ( object ) : NEW_LINE INDENT def isRectangleOverlap ( self , rec1 , rec2 ) : NEW_LINE INDENT return not ( rec1 [ 2 ] <= rec2 [ 0 ] or rec1 [ 3 ] <= rec2 [ 1 ] or rec1 [ 0 ] >= rec2 [ 2 ] or rec1 [ 1 ] >= rec2 [ 3 ] ) NEW_LINE DEDENT DEDENT


class Solution { public int hammingDistance ( int x , int y ) { return Integer . bitCount ( x ^ y ) ; } }

class Solution ( object ) : NEW_LINE INDENT def hammingDistance ( self , x , y ) : NEW_LINE INDENT return bin ( x ^ y ) . count ( '1' ) NEW_LINE DEDENT DEDENT


class Solution { public void duplicateZeros ( int [ ] arr ) { int movePos = 0 ; int lastPos = arr . length - 1 ; for ( int i = 0 ; i <= lastPos - movePos ; i ++ ) { if ( arr [ i ] == 0 ) { if ( i == lastPos - movePos ) { arr [ lastPos ] = 0 ; lastPos -- ; break ; } movePos ++ ; } } lastPos = lastPos - movePos ; for ( int i = lastPos ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) { arr [ i + movePos ] = 0 ; movePos -- ; arr [ i + movePos ] = 0 ; } else { arr [ i + movePos ] = arr [ i ] ; } } } }

class Solution : NEW_LINE INDENT def duplicateZeros ( self , arr : List [ int ] ) -> None : NEW_LINE INDENT move_pos = 0 NEW_LINE last_pos = len ( arr ) - 1 NEW_LINE for i in range ( last_pos + 1 ) : NEW_LINE INDENT if i > last_pos - move_pos : NEW_LINE INDENT break NEW_LINE DEDENT if arr [ i ] == 0 : NEW_LINE INDENT if i == last_pos - move_pos : NEW_LINE INDENT arr [ last_pos ] = 0 NEW_LINE last_pos -= 1 NEW_LINE break NEW_LINE DEDENT move_pos += 1 NEW_LINE DEDENT DEDENT last_pos -= move_pos NEW_LINE for i in range ( last , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT arr [ i + move_pos ] = 0 NEW_LINE move_pos -= 1 NEW_LINE arr [ i + move_pos ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i + move_pos ] = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT DEDENT


class Solution { public String reverseStr ( String s , int k ) { char [ ] a = s . toCharArray ( ) ; for ( int start = 0 ; start < a . length ; start += 2 * k ) { int i = start , j = Math . min ( start + k - 1 , a . length - 1 ) ; while ( i < j ) { char tmp = a [ i ] ; a [ i ++ ] = a [ j ] ; a [ j -- ] = tmp ; } } return new String ( a ) ; } }

class Solution : NEW_LINE INDENT def reverseStr ( self , s : str , k : int ) -> str : NEW_LINE INDENT N = len ( s ) NEW_LINE ans = " " NEW_LINE position = 0 NEW_LINE while position < N : NEW_LINE INDENT nx = s [ position : position + k ] NEW_LINE ans = ans + nx [ : : - 1 ] + s [ position + k : position + 2 * k ] NEW_LINE position += 2 * k NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT s1 = Solution ( ) NEW_LINE s = " abcdefg " NEW_LINE k = 2 NEW_LINE print ( s1 . reverseStr ( s , k ) ) NEW_LINE


public class Solution { public double findMedianSortedArrays ( int [ ] nums1 , int [ ] nums2 ) { int p1 = 0 , p2 = 0 , pos = 0 ; int ls1 = nums1 . length , ls2 = nums2 . length ; int [ ] all_nums = new int [ ls1 + ls2 ] ; double median = 0.0 ; while ( p1 < ls1 && p2 < ls2 ) { if ( nums1 [ p1 ] <= nums2 [ p2 ] ) all_nums [ pos ++ ] = nums1 [ p1 ++ ] ; else all_nums [ pos ++ ] = nums2 [ p2 ++ ] ; } while ( p1 < ls1 ) all_nums [ pos ++ ] = nums1 [ p1 ++ ] ; while ( p2 < ls2 ) all_nums [ pos ++ ] = nums2 [ p2 ++ ] ; if ( ( ls1 + ls2 ) % 2 == 1 ) median = all_nums [ ( ls1 + ls2 ) / 2 ] ; else median = ( all_nums [ ( ls1 + ls2 ) / 2 ] + all_nums [ ( ls1 + ls2 ) / 2 - 1 ] ) / 2.0 ; return median ; } }

class Solution ( object ) : NEW_LINE INDENT def findMedianSortedArrays ( self , nums1 , nums2 ) : NEW_LINE INDENT ls1 , ls2 = len ( nums1 ) , len ( nums2 ) NEW_LINE if ls1 < ls2 : NEW_LINE INDENT return self . findMedianSortedArrays ( nums2 , nums1 ) NEW_LINE DEDENT l , r = 0 , ls2 * 2 NEW_LINE while l <= r : NEW_LINE INDENT mid2 = ( l + r ) >> 1 NEW_LINE mid1 = ls1 + ls2 - mid2 NEW_LINE L1 = - sys . maxint - 1 if mid1 == 0 else nums1 [ ( mid1 - 1 ) >> 1 ] NEW_LINE L2 = - sys . maxint - 1 if mid2 == 0 else nums2 [ ( mid2 - 1 ) >> 1 ] NEW_LINE R1 = sys . maxint if mid1 == 2 * ls1 else nums1 [ mid1 >> 1 ] NEW_LINE R2 = sys . maxint if mid2 == 2 * ls2 else nums2 [ mid2 >> 1 ] NEW_LINE if L1 > R2 : NEW_LINE INDENT l = mid2 + 1 NEW_LINE DEDENT elif L2 > R1 : NEW_LINE INDENT r = mid2 - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( max ( L1 , L2 ) + min ( R1 , R2 ) ) / 2.0 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . findMedianSortedArrays ( [ 1 , 1 ] , [ 1 , 2 ] ) NEW_LINE DEDENT


class Solution { public List < Integer > selfDividingNumbers ( int left , int right ) { LinkedList list = new LinkedList ( ) ; for ( int i = left ; i <= right ; i ++ ) { if ( isSelfDiving ( i ) ) list . add ( i ) ; } return list ; } public boolean isSelfDiving ( int num ) { int digit = num % 10 ; int temp = num ; boolean isTrue = true ; while ( temp != 0 ) { if ( digit == 0 || num % digit != 0 ) { isTrue = false ; break ; } else { temp /= 10 ; digit = temp % 10 ; } } return isTrue ; } }

class Solution : NEW_LINE INDENT def selfDividingNumbers ( self , left : int , right : int ) -> List [ int ] : NEW_LINE INDENT return [ x for x in range ( left , right + 1 ) if all ( [ int ( i ) != 0 and x % int ( i ) == 0 for i in str ( x ) ] ) ] NEW_LINE DEDENT DEDENT


class Solution { public String intToRoman ( int num ) { Map < Integer , String > map = new HashMap ( ) ; map . put ( 1 , " I " ) ; map . put ( 5 , " V " ) ; map . put ( 10 , " X " ) ; map . put ( 50 , " L " ) ; map . put ( 100 , " C " ) ; map . put ( 500 , " D " ) ; map . put ( 1000 , " M " ) ; map . put ( 4 , " IV " ) ; map . put ( 9 , " IX " ) ; map . put ( 40 , " XL " ) ; map . put ( 90 , " XC " ) ; map . put ( 400 , " CD " ) ; map . put ( 900 , " CM " ) ; int [ ] sequence = { 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 } ; StringBuffer sb = new StringBuffer ( ) ; for ( int i = 0 ; i < sequence . length ; i ++ ) { int base = sequence [ i ] ; while ( num >= base ) { sb . append ( map . get ( base ) ) ; num -= base ; } } return sb . toString ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def intToRoman ( self , num ) : NEW_LINE INDENT values = [ 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 ] NEW_LINE symbols = [ " M " , " CM " , " D " , " CD " , " C " , " XC " , " L " , " XL " , " X " , " IX " , " V " , " IV " , " I " ] NEW_LINE roman = ' ' NEW_LINE i = 0 NEW_LINE while num > 0 : NEW_LINE INDENT k = num / values [ i ] NEW_LINE for j in range ( k ) : NEW_LINE INDENT roman += symbols [ i ] NEW_LINE num -= values [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return roman NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . intToRoman ( 90 ) NEW_LINE DEDENT


class Solution { private int dist ( int [ ] point ) { return point [ 0 ] * point [ 0 ] + point [ 1 ] * point [ 1 ] ; } public int [ ] [ ] kClosest ( int [ ] [ ] points , int K ) { PriorityQueue < int [ ] > pq = new PriorityQueue < int [ ] > ( ( p1 , p2 ) -> dist ( p2 ) - dist ( p1 ) ) ; for ( int [ ] p : points ) { pq . offer ( p ) ; if ( pq . size ( ) > K ) { pq . poll ( ) ; } } int [ ] [ ] res = new int [ K ] [ 2 ] ; while ( K > 0 ) { res [ -- K ] = pq . poll ( ) ; } return res ; } }

class Solution ( object ) : NEW_LINE INDENT def kClosest ( self , points , K ) : NEW_LINE INDENT return heapq . nsmallest ( K , points , key = lambda x : x [ 0 ] ** 2 + x [ 1 ] ** 2 ) NEW_LINE DEDENT DEDENT


public int [ ] sumZero ( int n ) { int [ ] res = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = i ; res [ 0 ] -= i ; } return res ; }

class Solution : NEW_LINE INDENT def sumZero ( self , n : int ) -> List [ int ] : NEW_LINE INDENT prefix_sum = 0 NEW_LINE res = [ ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res . append ( i ) NEW_LINE prefix_sum = prefix_sum + i NEW_LINE DEDENT res . append ( - prefix_sum ) NEW_LINE return res NEW_LINE DEDENT DEDENT


class Solution { public boolean leafSimilar ( TreeNode root1 , TreeNode root2 ) { List < Integer > leaves1 = new ArrayList ( ) ; List < Integer > leaves2 = new ArrayList ( ) ; dfs ( root1 , leaves1 ) ; dfs ( root2 , leaves2 ) ; return leaves1 . equals ( leaves2 ) ; } public void dfs ( TreeNode node , List < Integer > leafValues ) { if ( node != null ) { if ( node . left == null && node . right == null ) leafValues . add ( node . val ) ; dfs ( node . left , leafValues ) ; dfs ( node . right , leafValues ) ; } } }

class Solution ( object ) : NEW_LINE INDENT def leafSimilar ( self , root1 , root2 ) : NEW_LINE INDENT if not root1 and not root2 : NEW_LINE INDENT return True NEW_LINE DEDENT leaf1 = [ ] NEW_LINE leaf2 = [ ] NEW_LINE self . dfs ( root1 , leaf1 ) NEW_LINE self . dfs ( root2 , leaf2 ) NEW_LINE if leaf1 == leaf2 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def dfs ( self , node , leavels ) : NEW_LINE INDENT if not node : NEW_LINE INDENT return NEW_LINE DEDENT if not node . left and not node . right : NEW_LINE INDENT leavels . append ( node . val ) NEW_LINE DEDENT self . dfs ( node . left , leavels ) NEW_LINE self . dfs ( node . right , leavels ) NEW_LINE DEDENT DEDENT


class Solution { public List < String > topKFrequent ( String [ ] words , int k ) { Map < String , Integer > count = new HashMap ( ) ; for ( String word : words ) { count . put ( word , count . getOrDefault ( word , 0 ) + 1 ) ; } PriorityQueue < String > heap = new PriorityQueue < String > ( ( w1 , w2 ) -> count . get ( w1 ) . equals ( count . get ( w2 ) ) ? w2 . compareTo ( w1 ) : count . get ( w1 ) - count . get ( w2 ) ) ; for ( String word : count . keySet ( ) ) { heap . offer ( word ) ; if ( heap . size ( ) > k ) heap . poll ( ) ; } List < String > ans = new ArrayList ( ) ; while ( ! heap . isEmpty ( ) ) ans . add ( heap . poll ( ) ) ; Collections . reverse ( ans ) ; return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def topKFrequent ( self , words , k ) : NEW_LINE INDENT count = collections . Counter ( words ) NEW_LINE heap = [ ( - freq , word ) for word , freq in count . items ( ) ] NEW_LINE heapq . heapify ( heap ) NEW_LINE return [ heapq . heappop ( heap ) [ 1 ] for _ in xrange ( k ) ] NEW_LINE DEDENT DEDENT


class Solution { public int maxArea ( int [ ] height ) { int maxArea = 0 ; int left = 0 ; int right = height . length - 1 ; while ( left < right ) { maxArea = Math . max ( maxArea , ( right - left ) * Math . min ( height [ left ] , height [ right ] ) ) ; if ( height [ left ] < height [ right ] ) left ++ ; else right -- ; } return maxArea ; } }

class Solution : NEW_LINE INDENT def maxArea ( self , height : List [ int ] ) -> int : NEW_LINE INDENT left , right = 0 , len ( height ) - 1 NEW_LINE result = 0 NEW_LINE while left < right : NEW_LINE INDENT result = max ( min ( height [ left ] , height [ right ] ) * ( right - left ) , result ) NEW_LINE if height [ left ] > height [ right ] : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT left += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT DEDENT


class Solution { public int findLengthOfLCIS ( int [ ] nums ) { if ( nums . length == 0 ) return 0 ; int curr = 1 , ans = 1 ; for ( int i = 0 ; i < nums . length - 1 ; i ++ ) { if ( nums [ i ] < nums [ i + 1 ] ) { curr ++ ; if ( curr >= ans ) ans = curr ; } else { curr = 1 ; } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def findLengthOfLCIS ( self , nums ) : NEW_LINE INDENT if not nums or len ( nums ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = curr = 1 NEW_LINE for i in range ( len ( nums ) - 1 ) : NEW_LINE INDENT if nums [ i ] < nums [ i + 1 ] : NEW_LINE INDENT curr += 1 NEW_LINE ans = max ( ans , curr ) NEW_LINE DEDENT else : NEW_LINE INDENT curr = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public int findMaxConsecutiveOnes ( int [ ] nums ) { int ans = 0 ; int curr = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { if ( nums [ i ] == 1 ) { curr ++ ; if ( curr > ans ) ans = curr ; } else { curr = 0 ; } } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def findMaxConsecutiveOnes ( self , nums ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE for n in nums : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT curr += 1 NEW_LINE if curr > ans : NEW_LINE INDENT ans = curr NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT


class Solution { public int reverse ( int x ) { if ( x == 0 ) return 0 ; long res = 0 ; while ( x != 0 ) { res = res * 10 + x % 10 ; if ( res > Integer . MAX_VALUE || res < Integer . MIN_VALUE ) return 0 ; x /= 10 ; } return ( int ) res ; } }

class Solution : NEW_LINE INDENT def reverse ( self , x ) : NEW_LINE INDENT res , isPos = 0 , 1 NEW_LINE if x < 0 : NEW_LINE INDENT isPos = - 1 NEW_LINE x = - 1 * x NEW_LINE DEDENT while x != 0 : NEW_LINE INDENT res = res * 10 + x % 10 NEW_LINE if res > 2147483647 : NEW_LINE INDENT return 0 NEW_LINE DEDENT x /= 10 NEW_LINE DEDENT return res * isPos NEW_LINE DEDENT DEDENT


public class Solution { public String reverseWords ( String s ) { String words [ ] = s . split ( " ▁ " ) ; StringBuilder ans = new StringBuilder ( ) ; for ( String word : words ) ans . append ( new StringBuffer ( word ) . reverse ( ) . toString ( ) + " ▁ " ) ; return ans . toString ( ) . trim ( ) ; } }

class Solution ( object ) : NEW_LINE INDENT def reverseWords ( self , s ) : NEW_LINE INDENT return ' ▁ ' . join ( [ word [ : : - 1 ] for word in s . split ( ' ▁ ' ) ] ) NEW_LINE DEDENT DEDENT


public class Solution { public int [ ] productExceptSelf ( int [ ] nums ) { int n = nums . length ; int [ ] res = new int [ n ] ; res [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i - 1 ] * nums [ i - 1 ] ; } int right = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { res [ i ] *= right ; right *= nums [ i ] ; } return res ; } }

class Solution ( object ) : NEW_LINE INDENT def productExceptSelf ( self , nums ) : NEW_LINE INDENT ans = [ 1 ] * len ( nums ) NEW_LINE for i in range ( 1 , len ( nums ) ) : NEW_LINE INDENT ans [ i ] = ans [ i - 1 ] * nums [ i - 1 ] NEW_LINE DEDENT right = 1 NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans [ i ] *= right NEW_LINE right *= nums [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT


import java . awt . Point ; class Solution { public int maxWidthRamp ( int [ ] A ) { int N = A . length ; Integer [ ] B = new Integer [ N ] ; for ( int i = 0 ; i < N ; ++ i ) B [ i ] = i ; Arrays . sort ( B , ( i , j ) -> ( ( Integer ) A [ i ] ) . compareTo ( A [ j ] ) ) ; int ans = 0 ; int m = N ; for ( int i : B ) { ans = Math . max ( ans , i - m ) ; m = Math . min ( m , i ) ; } return ans ; } }

class Solution ( object ) : NEW_LINE INDENT def maxWidthRamp ( self , A ) : NEW_LINE INDENT ans = 0 NEW_LINE m = float ( ' inf ' ) NEW_LINE for i in sorted ( range ( len ( A ) ) , key = A . __getitem__ ) : NEW_LINE INDENT ans = max ( ans , i - m ) NEW_LINE m = min ( m , i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . maxWidthRamp ( [ 6 , 0 , 8 , 2 , 1 , 5 ] ) NEW_LINE print s . maxWidthRamp ( [ 9 , 8 , 1 , 0 , 1 , 9 , 4 , 0 , 4 , 1 ] ) NEW_LINE DEDENT


public class Solution { public ListNode addTwoNumbers ( ListNode l1 , ListNode l2 ) { ListNode dummyHead = new ListNode ( 0 ) ; ListNode p = l1 , q = l2 , curr = dummyHead ; int carry = 0 ; while ( p != null || q != null ) { int x = ( p != null ) ? p . val : 0 ; int y = ( q != null ) ? q . val : 0 ; int digit = carry + x + y ; carry = digit / 10 ; curr . next = new ListNode ( digit % 10 ) ; curr = curr . next ; if ( p != null ) p = p . next ; if ( q != null ) q = q . next ; } if ( carry > 0 ) { curr . next = new ListNode ( carry ) ; } return dummyHead . next ; } }

class ListNode ( object ) : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class Solution ( object ) : NEW_LINE INDENT def addTwoNumbers ( self , l1 , l2 ) : NEW_LINE INDENT carry = 0 NEW_LINE head = curr = ListNode ( 0 ) NEW_LINE while l1 or l2 : NEW_LINE INDENT val = carry NEW_LINE if l1 : NEW_LINE INDENT val += l1 . val NEW_LINE l1 = l1 . next NEW_LINE DEDENT if l2 : NEW_LINE INDENT val += l2 . val NEW_LINE l2 = l2 . next NEW_LINE DEDENT curr . next = ListNode ( val % 10 ) NEW_LINE curr = curr . next NEW_LINE carry = val / 10 NEW_LINE DEDENT if carry > 0 : NEW_LINE INDENT curr . next = ListNode ( carry ) NEW_LINE DEDENT return head . next NEW_LINE DEDENT DEDENT


class Solution { public boolean checkPossibility ( int [ ] nums ) { int brokenPoint = 0 ; for ( int i = 0 ; i < nums . length - 1 ; i ++ ) { if ( nums [ i ] > nums [ i + 1 ] ) { brokenPoint ++ ; if ( brokenPoint >= 2 ) return false ; if ( i - 1 < 0 || nums [ i - 1 ] <= nums [ i + 1 ] ) nums [ i ] = nums [ i + 1 ] ; else nums [ i + 1 ] = nums [ i ] ; } } return true ; } }

class Solution ( object ) : NEW_LINE INDENT def checkPossibility ( self , nums ) : NEW_LINE INDENT broken_num = 0 NEW_LINE for i in range ( len ( nums ) - 1 ) : NEW_LINE INDENT if ( nums [ i ] > nums [ i + 1 ] ) : NEW_LINE INDENT broken_num += 1 NEW_LINE if broken_num >= 2 : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i - 1 < 0 or nums [ i - 1 ] <= nums [ i + 1 ] ) : NEW_LINE INDENT nums [ i ] = nums [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT nums [ i + 1 ] = nums [ i ] NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT DEDENT


class Solution { public int [ ] sortArrayByParity ( int [ ] A ) { int lo = 0 , hi = A . length - 1 ; while ( lo < hi ) { if ( A [ lo ] % 2 > A [ hi ] % 2 ) { int tmp = A [ hi ] ; A [ hi ] = A [ lo ] ; A [ lo ] = tmp ; } if ( A [ lo ] % 2 == 0 ) lo ++ ; if ( A [ hi ] % 2 == 1 ) hi -- ; } return A ; } }

class Solution ( object ) : NEW_LINE INDENT def sortArrayByParity ( self , A ) : NEW_LINE INDENT lo , hi = 0 , len ( A ) - 1 NEW_LINE while lo < hi : NEW_LINE INDENT if A [ lo ] % 2 > A [ hi ] % 2 : NEW_LINE INDENT A [ lo ] , A [ hi ] = A [ hi ] , A [ lo ] NEW_LINE DEDENT if A [ lo ] % 2 == 0 : lo += 1 NEW_LINE if A [ hi ] % 2 == 1 : hi -= 1 NEW_LINE DEDENT return A NEW_LINE DEDENT DEDENT


public class Solution { public void dfs ( int [ ] [ ] M , int [ ] visited , int i ) { for ( int j = 0 ; j < M . length ; j ++ ) { if ( M [ i ] [ j ] == 1 && visited [ j ] == 0 ) { visited [ j ] = 1 ; dfs ( M , visited , j ) ; } } } public int findCircleNum ( int [ ] [ ] M ) { int [ ] visited = new int [ M . length ] ; int count = 0 ; for ( int i = 0 ; i < M . length ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( M , visited , i ) ; count ++ ; } } return count ; } }

class Solution ( object ) : NEW_LINE INDENT def findCircleNum ( self , M ) : NEW_LINE INDENT visited = [ 0 ] * len ( M ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( M ) ) : NEW_LINE INDENT if visited [ i ] == 0 : NEW_LINE INDENT self . dfs ( M , visited , i ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def dfs ( self , M , visited , i ) : NEW_LINE INDENT for j in range ( len ( M ) ) : NEW_LINE INDENT if M [ i ] [ j ] == 1 and visited [ j ] == 0 : NEW_LINE INDENT visited [ j ] = 1 NEW_LINE self . dfs ( M , visited , j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT


class Solution { public List < List < Integer > > shiftGrid ( int [ ] [ ] grid , int k ) { int [ ] [ ] newGrid = new int [ grid . length ] [ grid [ 0 ] . length ] ; int m = grid . length ; int n = grid [ 0 ] . length ; int true_k = k % ( m * n ) ; int move_i = true_k / n ; int move_j = true_k % n ; for ( int i = 0 ; i < grid . length ; i ++ ) { for ( int j = 0 ; j < grid [ i ] . length ; j ++ ) { int new_i = i + move_i ; int new_j = ( j + move_j ) % n ; if ( move_j + j >= n ) new_i ++ ; new_i %= m ; newGrid [ new_i ] [ new_j ] = grid [ i ] [ j ] ; } } List < List < Integer > > result = new ArrayList < > ( ) ; for ( int [ ] row : newGrid ) { List < Integer > listRow = new ArrayList < > ( ) ; result . add ( listRow ) ; for ( int v : row ) listRow . add ( v ) ; } return result ; } }

class Solution ( object ) : NEW_LINE INDENT def shiftGrid ( self , grid , k ) : NEW_LINE INDENT new_grid = [ [ 0 ] * len ( grid [ 0 ] ) for _ in range ( len ( grid ) ) ] NEW_LINE m = len ( grid ) NEW_LINE n = len ( grid [ 0 ] ) NEW_LINE true_k = k % ( m * n ) NEW_LINE move_i = true_k / n NEW_LINE move_j = true_k % n NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT new_i = i + move_i NEW_LINE if move_j + j >= n : NEW_LINE INDENT new_i += 1 NEW_LINE DEDENT new_i %= m NEW_LINE new_j = ( j + move_j ) % n NEW_LINE new_grid [ new_i ] [ new_j ] = grid [ i ] [ j ] NEW_LINE DEDENT DEDENT return new_grid NEW_LINE DEDENT DEDENT


public class Solution { public int findUnsortedSubarray ( int [ ] nums ) { Stack < Integer > stack = new Stack < Integer > ( ) ; int l = nums . length , r = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { while ( ! stack . isEmpty ( ) && nums [ stack . peek ( ) ] > nums [ i ] ) l = Math . min ( l , stack . pop ( ) ) ; stack . push ( i ) ; } stack . clear ( ) ; for ( int i = nums . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && nums [ stack . peek ( ) ] < nums [ i ] ) r = Math . max ( r , stack . pop ( ) ) ; stack . push ( i ) ; } return r - l > 0 ? r - l + 1 : 0 ; } }

class Solution ( object ) : NEW_LINE INDENT def findUnsortedSubarray ( self , nums ) : NEW_LINE INDENT stack = [ ] NEW_LINE l , r = len ( nums ) , 0 NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT while len ( stack ) != 0 and nums [ stack [ - 1 ] ] > nums [ i ] : NEW_LINE INDENT l = min ( l , stack . pop ( ) ) NEW_LINE DEDENT stack . append ( i ) NEW_LINE DEDENT stack = [ ] NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT while len ( stack ) != 0 and nums [ stack [ - 1 ] ] < nums [ i ] : NEW_LINE INDENT r = max ( r , stack . pop ( ) ) NEW_LINE DEDENT stack . append ( i ) NEW_LINE DEDENT if r > l : NEW_LINE INDENT return r - l + 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT DEDENT