package treesandgraphs

private var xIndex = mapOf<Int, List<Int>>()
private var yIndex = mapOf<Int, List<Int>>()
private var visited = booleanArrayOf()

private fun dfs(stones: Array<IntArray>, index: Int): Int {
    visited[index] = true
    var sum = 0
    for (i in xIndex[stones[index][0]]!!) {
        if (!visited[i]) {
            sum += dfs(stones, i) + 1
        }
    }
    for (i in yIndex[stones[index][1]]!!) {
        if (!visited[i]) {
            sum += dfs(stones, i) + 1
        }
    }
    return sum
}

fun removeStones(stones: Array<IntArray>): Int {
    xIndex = stones.indices.groupBy({stones[it][0]},{it})
    yIndex = stones.indices.groupBy({stones[it][1]},{it})
    visited = BooleanArray(stones.size) { false }
    var stonesToRemove = 0
    for (i in stones.indices) {
        if (!visited[i]) {
            stonesToRemove += dfs(stones, i)
        }
    }
    return stonesToRemove
}

fun main() {
    println(removeStones(arrayOf(intArrayOf(0, 0), intArrayOf(0, 1), intArrayOf(1, 0),
        intArrayOf(1, 2), intArrayOf(2, 1), intArrayOf(2, 2))))
}

Compiled from "MostStonesRemovedWithSameRowOrColumn.kt"
public final class treesandgraphs.MostStonesRemovedWithSameRowOrColumnKt {
  private static java.util.Map<java.lang.Integer, ? extends java.util.List<java.lang.Integer>> xIndex;

  private static java.util.Map<java.lang.Integer, ? extends java.util.List<java.lang.Integer>> yIndex;

  private static boolean[] visited;

  private static final int dfs(int[][], int);
    Code:
       0: getstatic     #10                 // Field visited:[Z
       3: iload_1
       4: iconst_1
       5: bastore
       6: iconst_0
       7: istore_2
       8: getstatic     #14                 // Field xIndex:Ljava/util/Map;
      11: aload_0
      12: iload_1
      13: aaload
      14: iconst_0
      15: iaload
      16: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: invokeinterface #26,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      24: dup
      25: invokestatic  #32                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      28: checkcast     #34                 // class java/util/List
      31: invokeinterface #38,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
      36: astore_3
      37: aload_3
      38: invokeinterface #44,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      43: ifeq          83
      46: aload_3
      47: invokeinterface #48,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
      52: checkcast     #50                 // class java/lang/Number
      55: invokevirtual #54                 // Method java/lang/Number.intValue:()I
      58: istore        4
      60: getstatic     #10                 // Field visited:[Z
      63: iload         4
      65: baload
      66: ifne          37
      69: iload_2
      70: aload_0
      71: iload         4
      73: invokestatic  #56                 // Method dfs:([[II)I
      76: iconst_1
      77: iadd
      78: iadd
      79: istore_2
      80: goto          37
      83: getstatic     #59                 // Field yIndex:Ljava/util/Map;
      86: aload_0
      87: iload_1
      88: aaload
      89: iconst_1
      90: iaload
      91: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      94: invokeinterface #26,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      99: dup
     100: invokestatic  #32                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     103: checkcast     #34                 // class java/util/List
     106: invokeinterface #38,  1           // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator;
     111: astore_3
     112: aload_3
     113: invokeinterface #44,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     118: ifeq          158
     121: aload_3
     122: invokeinterface #48,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     127: checkcast     #50                 // class java/lang/Number
     130: invokevirtual #54                 // Method java/lang/Number.intValue:()I
     133: istore        4
     135: getstatic     #10                 // Field visited:[Z
     138: iload         4
     140: baload
     141: ifne          112
     144: iload_2
     145: aload_0
     146: iload         4
     148: invokestatic  #56                 // Method dfs:([[II)I
     151: iconst_1
     152: iadd
     153: iadd
     154: istore_2
     155: goto          112
     158: iload_2
     159: ireturn

  public static final int removeStones(int[][]);
    Code:
       0: aload_0
       1: ldc           #69                 // String stones
       3: invokestatic  #73                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_0
       7: checkcast     #75                 // class "[Ljava/lang/Object;"
      10: invokestatic  #81                 // Method kotlin/collections/ArraysKt.getIndices:([Ljava/lang/Object;)Lkotlin/ranges/IntRange;
      13: checkcast     #83                 // class java/lang/Iterable
      16: astore_1
      17: iconst_0
      18: istore_2
      19: aload_1
      20: astore_3
      21: new           #85                 // class java/util/LinkedHashMap
      24: dup
      25: invokespecial #89                 // Method java/util/LinkedHashMap."<init>":()V
      28: checkcast     #22                 // class java/util/Map
      31: astore        4
      33: iconst_0
      34: istore        5
      36: aload_3
      37: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      42: astore        6
      44: aload         6
      46: invokeinterface #44,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      51: ifeq          173
      54: aload         6
      56: checkcast     #92                 // class kotlin/collections/IntIterator
      59: invokevirtual #95                 // Method kotlin/collections/IntIterator.nextInt:()I
      62: istore        7
      64: iload         7
      66: istore        8
      68: iconst_0
      69: istore        9
      71: aload_0
      72: iload         8
      74: aaload
      75: iconst_0
      76: iaload
      77: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      80: astore        10
      82: aload         4
      84: astore        11
      86: iconst_0
      87: istore        12
      89: aload         11
      91: aload         10
      93: invokeinterface #26,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      98: astore        13
     100: aload         13
     102: ifnonnull     137
     105: iconst_0
     106: istore        14
     108: new           #97                 // class java/util/ArrayList
     111: dup
     112: invokespecial #98                 // Method java/util/ArrayList."<init>":()V
     115: checkcast     #34                 // class java/util/List
     118: astore        14
     120: aload         11
     122: aload         10
     124: aload         14
     126: invokeinterface #102,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     131: pop
     132: aload         14
     134: goto          139
     137: aload         13
     139: nop
     140: checkcast     #34                 // class java/util/List
     143: astore        15
     145: aload         15
     147: iload         7
     149: istore        8
     151: astore        16
     153: iconst_0
     154: istore        9
     156: iload         8
     158: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     161: aload         16
     163: swap
     164: invokeinterface #106,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     169: pop
     170: goto          44
     173: aload         4
     175: nop
     176: putstatic     #14                 // Field xIndex:Ljava/util/Map;
     179: aload_0
     180: checkcast     #75                 // class "[Ljava/lang/Object;"
     183: invokestatic  #81                 // Method kotlin/collections/ArraysKt.getIndices:([Ljava/lang/Object;)Lkotlin/ranges/IntRange;
     186: checkcast     #83                 // class java/lang/Iterable
     189: astore_1
     190: iconst_0
     191: istore_2
     192: aload_1
     193: astore_3
     194: new           #85                 // class java/util/LinkedHashMap
     197: dup
     198: invokespecial #89                 // Method java/util/LinkedHashMap."<init>":()V
     201: checkcast     #22                 // class java/util/Map
     204: astore        4
     206: iconst_0
     207: istore        5
     209: aload_3
     210: invokeinterface #90,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     215: astore        6
     217: aload         6
     219: invokeinterface #44,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     224: ifeq          346
     227: aload         6
     229: checkcast     #92                 // class kotlin/collections/IntIterator
     232: invokevirtual #95                 // Method kotlin/collections/IntIterator.nextInt:()I
     235: istore        7
     237: iload         7
     239: istore        8
     241: iconst_0
     242: istore        9
     244: aload_0
     245: iload         8
     247: aaload
     248: iconst_1
     249: iaload
     250: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     253: astore        10
     255: aload         4
     257: astore        11
     259: iconst_0
     260: istore        12
     262: aload         11
     264: aload         10
     266: invokeinterface #26,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     271: astore        13
     273: aload         13
     275: ifnonnull     310
     278: iconst_0
     279: istore        14
     281: new           #97                 // class java/util/ArrayList
     284: dup
     285: invokespecial #98                 // Method java/util/ArrayList."<init>":()V
     288: checkcast     #34                 // class java/util/List
     291: astore        14
     293: aload         11
     295: aload         10
     297: aload         14
     299: invokeinterface #102,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     304: pop
     305: aload         14
     307: goto          312
     310: aload         13
     312: nop
     313: checkcast     #34                 // class java/util/List
     316: astore        15
     318: aload         15
     320: iload         7
     322: istore        8
     324: astore        16
     326: iconst_0
     327: istore        9
     329: iload         8
     331: invokestatic  #20                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     334: aload         16
     336: swap
     337: invokeinterface #106,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     342: pop
     343: goto          217
     346: aload         4
     348: nop
     349: putstatic     #59                 // Field yIndex:Ljava/util/Map;
     352: iconst_0
     353: istore_1
     354: aload_0
     355: checkcast     #75                 // class "[Ljava/lang/Object;"
     358: arraylength
     359: istore_2
     360: iload_2
     361: newarray       boolean
     363: astore_3
     364: iload_1
     365: iload_2
     366: if_icmpge     383
     369: iload_1
     370: istore        4
     372: aload_3
     373: iload         4
     375: iconst_0
     376: bastore
     377: iinc          1, 1
     380: goto          364
     383: aload_3
     384: putstatic     #10                 // Field visited:[Z
     387: iconst_0
     388: istore_1
     389: iconst_0
     390: istore_2
     391: aload_0
     392: checkcast     #75                 // class "[Ljava/lang/Object;"
     395: arraylength
     396: istore_3
     397: iload_2
     398: iload_3
     399: if_icmpge     424
     402: getstatic     #10                 // Field visited:[Z
     405: iload_2
     406: baload
     407: ifne          418
     410: iload_1
     411: aload_0
     412: iload_2
     413: invokestatic  #56                 // Method dfs:([[II)I
     416: iadd
     417: istore_1
     418: iinc          2, 1
     421: goto          397
     424: iload_1
     425: ireturn

  public static final void main();
    Code:
       0: bipush        6
       2: anewarray     #133                // class "[I"
       5: astore_0
       6: aload_0
       7: iconst_0
       8: iconst_2
       9: newarray       int
      11: astore_1
      12: aload_1
      13: iconst_0
      14: iconst_0
      15: iastore
      16: aload_1
      17: iconst_1
      18: iconst_0
      19: iastore
      20: aload_1
      21: aastore
      22: aload_0
      23: iconst_1
      24: iconst_2
      25: newarray       int
      27: astore_1
      28: aload_1
      29: iconst_0
      30: iconst_0
      31: iastore
      32: aload_1
      33: iconst_1
      34: iconst_1
      35: iastore
      36: aload_1
      37: aastore
      38: aload_0
      39: iconst_2
      40: iconst_2
      41: newarray       int
      43: astore_1
      44: aload_1
      45: iconst_0
      46: iconst_1
      47: iastore
      48: aload_1
      49: iconst_1
      50: iconst_0
      51: iastore
      52: aload_1
      53: aastore
      54: aload_0
      55: iconst_3
      56: iconst_2
      57: newarray       int
      59: astore_1
      60: aload_1
      61: iconst_0
      62: iconst_1
      63: iastore
      64: aload_1
      65: iconst_1
      66: iconst_2
      67: iastore
      68: aload_1
      69: aastore
      70: aload_0
      71: iconst_4
      72: iconst_2
      73: newarray       int
      75: astore_1
      76: aload_1
      77: iconst_0
      78: iconst_2
      79: iastore
      80: aload_1
      81: iconst_1
      82: iconst_1
      83: iastore
      84: aload_1
      85: aastore
      86: aload_0
      87: iconst_5
      88: iconst_2
      89: newarray       int
      91: astore_1
      92: aload_1
      93: iconst_0
      94: iconst_2
      95: iastore
      96: aload_1
      97: iconst_1
      98: iconst_2
      99: iastore
     100: aload_1
     101: aastore
     102: aload_0
     103: invokestatic  #135                // Method removeStones:([[I)I
     106: istore_0
     107: getstatic     #141                // Field java/lang/System.out:Ljava/io/PrintStream;
     110: iload_0
     111: invokevirtual #147                // Method java/io/PrintStream.println:(I)V
     114: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #150                // Method main:()V
       3: return

  static {};
    Code:
       0: invokestatic  #159                // Method kotlin/collections/MapsKt.emptyMap:()Ljava/util/Map;
       3: putstatic     #14                 // Field xIndex:Ljava/util/Map;
       6: invokestatic  #159                // Method kotlin/collections/MapsKt.emptyMap:()Ljava/util/Map;
       9: putstatic     #59                 // Field yIndex:Ljava/util/Map;
      12: iconst_0
      13: newarray       boolean
      15: putstatic     #10                 // Field visited:[Z
      18: return
}


package treesandgraphs

private var memo: Array<IntArray>? = null

private fun dfs(matrix: Array<IntArray>, i: Int, j: Int): Int {

    if (memo!![i][j] != -1) {
        return memo!![i][j]
    }

    var maxLength = 0

    if (i > 0 && matrix[i - 1][j] > matrix[i][j])
        maxLength = maxOf(maxLength, dfs(matrix, i - 1, j))
    if (j > 0 && matrix[i][j - 1] > matrix[i][j])
        maxLength = maxOf(maxLength, dfs(matrix, i, j - 1))
    if (i < matrix.lastIndex && matrix[i + 1][j] > matrix[i][j])
        maxLength = maxOf(maxLength, dfs(matrix, i + 1, j))
    if (j < matrix[i].lastIndex && matrix[i][j + 1] > matrix[i][j])
        maxLength = maxOf(maxLength, dfs(matrix, i, j + 1))

    memo!![i][j] = maxLength + 1
    return maxLength + 1
}

fun longestIncreasingPath(matrix: Array<IntArray>): Int {

    memo = Array<IntArray>(matrix.size) { IntArray(matrix[it].size) { -1 } }

    var maxLength = 0

    for (i in matrix.indices) {
        for (j in matrix[i].indices) {
            maxLength = maxOf(maxLength, dfs(matrix, i, j))
        }
    }

    return maxLength
}

Compiled from "LongestIncreasingPathInMatrix.kt"
public final class treesandgraphs.LongestIncreasingPathInMatrixKt {
  private static int[][] memo;

  private static final int dfs(int[][], int, int);
    Code:
       0: getstatic     #10                 // Field memo:[[I
       3: dup
       4: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
       7: iload_1
       8: aaload
       9: iload_2
      10: iaload
      11: iconst_m1
      12: if_icmpeq     27
      15: getstatic     #10                 // Field memo:[[I
      18: dup
      19: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      22: iload_1
      23: aaload
      24: iload_2
      25: iaload
      26: ireturn
      27: iconst_0
      28: istore_3
      29: iload_1
      30: ifle          61
      33: aload_0
      34: iload_1
      35: iconst_1
      36: isub
      37: aaload
      38: iload_2
      39: iaload
      40: aload_0
      41: iload_1
      42: aaload
      43: iload_2
      44: iaload
      45: if_icmple     61
      48: iload_3
      49: aload_0
      50: iload_1
      51: iconst_1
      52: isub
      53: iload_2
      54: invokestatic  #18                 // Method dfs:([[III)I
      57: invokestatic  #24                 // Method java/lang/Math.max:(II)I
      60: istore_3
      61: iload_2
      62: ifle          93
      65: aload_0
      66: iload_1
      67: aaload
      68: iload_2
      69: iconst_1
      70: isub
      71: iaload
      72: aload_0
      73: iload_1
      74: aaload
      75: iload_2
      76: iaload
      77: if_icmple     93
      80: iload_3
      81: aload_0
      82: iload_1
      83: iload_2
      84: iconst_1
      85: isub
      86: invokestatic  #18                 // Method dfs:([[III)I
      89: invokestatic  #24                 // Method java/lang/Math.max:(II)I
      92: istore_3
      93: iload_1
      94: aload_0
      95: checkcast     #26                 // class "[Ljava/lang/Object;"
      98: invokestatic  #32                 // Method kotlin/collections/ArraysKt.getLastIndex:([Ljava/lang/Object;)I
     101: if_icmpge     132
     104: aload_0
     105: iload_1
     106: iconst_1
     107: iadd
     108: aaload
     109: iload_2
     110: iaload
     111: aload_0
     112: iload_1
     113: aaload
     114: iload_2
     115: iaload
     116: if_icmple     132
     119: iload_3
     120: aload_0
     121: iload_1
     122: iconst_1
     123: iadd
     124: iload_2
     125: invokestatic  #18                 // Method dfs:([[III)I
     128: invokestatic  #24                 // Method java/lang/Math.max:(II)I
     131: istore_3
     132: iload_2
     133: aload_0
     134: iload_1
     135: aaload
     136: invokestatic  #35                 // Method kotlin/collections/ArraysKt.getLastIndex:([I)I
     139: if_icmpge     170
     142: aload_0
     143: iload_1
     144: aaload
     145: iload_2
     146: iconst_1
     147: iadd
     148: iaload
     149: aload_0
     150: iload_1
     151: aaload
     152: iload_2
     153: iaload
     154: if_icmple     170
     157: iload_3
     158: aload_0
     159: iload_1
     160: iload_2
     161: iconst_1
     162: iadd
     163: invokestatic  #18                 // Method dfs:([[III)I
     166: invokestatic  #24                 // Method java/lang/Math.max:(II)I
     169: istore_3
     170: getstatic     #10                 // Field memo:[[I
     173: dup
     174: invokestatic  #16                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     177: iload_1
     178: aaload
     179: iload_2
     180: iload_3
     181: iconst_1
     182: iadd
     183: iastore
     184: iload_3
     185: iconst_1
     186: iadd
     187: ireturn

  public static final int longestIncreasingPath(int[][]);
    Code:
       0: aload_0
       1: ldc           #44                 // String matrix
       3: invokestatic  #48                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: checkcast     #26                 // class "[Ljava/lang/Object;"
      12: arraylength
      13: istore_2
      14: iload_2
      15: anewarray     #50                 // class "[I"
      18: astore_3
      19: iload_1
      20: iload_2
      21: if_icmpge     86
      24: iload_1
      25: istore        4
      27: aload_3
      28: iload         4
      30: iconst_0
      31: istore        5
      33: aload_0
      34: iload         4
      36: aaload
      37: arraylength
      38: istore        6
      40: iload         6
      42: newarray       int
      44: astore        7
      46: istore        10
      48: astore        9
      50: iload         5
      52: iload         6
      54: if_icmpge     73
      57: iload         5
      59: istore        8
      61: aload         7
      63: iload         8
      65: iconst_m1
      66: iastore
      67: iinc          5, 1
      70: goto          50
      73: aload         9
      75: iload         10
      77: aload         7
      79: aastore
      80: iinc          1, 1
      83: goto          19
      86: aload_3
      87: putstatic     #10                 // Field memo:[[I
      90: iconst_0
      91: istore_1
      92: iconst_0
      93: istore_2
      94: aload_0
      95: checkcast     #26                 // class "[Ljava/lang/Object;"
      98: arraylength
      99: istore_3
     100: iload_2
     101: iload_3
     102: if_icmpge     145
     105: iconst_0
     106: istore        4
     108: aload_0
     109: iload_2
     110: aaload
     111: arraylength
     112: istore        5
     114: iload         4
     116: iload         5
     118: if_icmpge     139
     121: iload_1
     122: aload_0
     123: iload_2
     124: iload         4
     126: invokestatic  #18                 // Method dfs:([[III)I
     129: invokestatic  #24                 // Method java/lang/Math.max:(II)I
     132: istore_1
     133: iinc          4, 1
     136: goto          114
     139: iinc          2, 1
     142: goto          100
     145: iload_1
     146: ireturn

  static {};
    Code:
       0: return
}


package design

class AutocompleteSystem(sentences: Array<String>, times: IntArray) {

    private class TrieNode {
        var leafTimes = 0
        val children = mutableMapOf<Char, TrieNode>()
    }

    private val trieRoot = TrieNode()
    private var currentNode: TrieNode
    private val prefix = StringBuilder()

    init {
        for (i in sentences.indices) {
            var cur = trieRoot
            for (c in sentences[i]) {
                if (c !in cur.children) {
                    cur.children[c] = TrieNode()
                }
                cur = cur.children[c]!!
            }
            cur.leafTimes = times[i]
        }
        currentNode = trieRoot
    }

    private fun dfsTraverse(prefix: StringBuilder, currentNode: TrieNode, sentencesList: MutableList<Pair<String, Int>>) {

        if (currentNode.leafTimes > 0) {
            sentencesList.add(prefix.toString() to currentNode.leafTimes)
        }

        for (entry in currentNode.children.entries.iterator()) {
            prefix.append(entry.key)
            dfsTraverse(prefix, entry.value, sentencesList)
            prefix.deleteCharAt(prefix.lastIndex)
        }
    }

    fun input(c: Char): List<String> {
        if (c == '#') {
            currentNode.leafTimes++
            currentNode = trieRoot
            prefix.delete(0, prefix.length)
            return listOf<String>()
        }

        if (c !in currentNode.children) {
            currentNode.children[c] = TrieNode()
        }

        currentNode = currentNode.children[c]!!
        prefix.append(c)
        val sentencesList = mutableListOf<Pair<String, Int>>()
        dfsTraverse(prefix, currentNode, sentencesList)

        sentencesList.sortWith(object: Comparator<Pair<String, Int>> {
            override fun compare(a: Pair<String, Int>, b: Pair<String, Int>): Int = when {
                a.second != b.second -> -a.second + b.second
                else -> a.first.compareTo(b.first)
            }
        })

        return if (sentencesList.size < 3) {
            sentencesList.map {it.first}
        } else {
            sentencesList.subList(0, 3).map {it.first}
        }
    }

}

fun main() {
    val obj = AutocompleteSystem(arrayOf("i love you", "island", "iroman", "i love leetcode"), intArrayOf(5, 3, 2, 2))
    println(obj.input('i')) // return ["i love you", "island", "i love leetcode"]. There are four sentences that have prefix "i". Among them, "ironman" and "i love leetcode" have same hot degree. Since ' ' has ASCII code 32 and 'r' has ASCII code 114, "i love leetcode" should be in front of "ironman". Also we only need to output top 3 hot sentences, so "ironman" will be ignored.
    println(obj.input(' ')) // return ["i love you", "i love leetcode"]. There are only two sentences that have prefix "i ".
    println(obj.input('a')) // return []. There are no sentences that have prefix "i a".
    println(obj.input('#')) // return []. The user finished the input, the sentence "i a" should be saved as a historical sentence in system. And the following input will be counted as a new search.
}

Compiled from "SearchAutocompleteSystem.kt"
public final class design.SearchAutocompleteSystemKt {
  public static final void main();
    Code:
       0: new           #8                  // class design/AutocompleteSystem
       3: dup
       4: iconst_4
       5: anewarray     #10                 // class java/lang/String
       8: astore_1
       9: aload_1
      10: iconst_0
      11: ldc           #12                 // String i love you
      13: aastore
      14: aload_1
      15: iconst_1
      16: ldc           #14                 // String island
      18: aastore
      19: aload_1
      20: iconst_2
      21: ldc           #16                 // String iroman
      23: aastore
      24: aload_1
      25: iconst_3
      26: ldc           #18                 // String i love leetcode
      28: aastore
      29: aload_1
      30: iconst_4
      31: newarray       int
      33: astore_1
      34: aload_1
      35: iconst_0
      36: iconst_5
      37: iastore
      38: aload_1
      39: iconst_1
      40: iconst_3
      41: iastore
      42: aload_1
      43: iconst_2
      44: iconst_2
      45: iastore
      46: aload_1
      47: iconst_3
      48: iconst_2
      49: iastore
      50: aload_1
      51: invokespecial #22                 // Method design/AutocompleteSystem."<init>":([Ljava/lang/String;[I)V
      54: astore_0
      55: aload_0
      56: bipush        105
      58: invokevirtual #26                 // Method design/AutocompleteSystem.input:(C)Ljava/util/List;
      61: getstatic     #32                 // Field java/lang/System.out:Ljava/io/PrintStream;
      64: swap
      65: invokevirtual #38                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      68: aload_0
      69: bipush        32
      71: invokevirtual #26                 // Method design/AutocompleteSystem.input:(C)Ljava/util/List;
      74: getstatic     #32                 // Field java/lang/System.out:Ljava/io/PrintStream;
      77: swap
      78: invokevirtual #38                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      81: aload_0
      82: bipush        97
      84: invokevirtual #26                 // Method design/AutocompleteSystem.input:(C)Ljava/util/List;
      87: getstatic     #32                 // Field java/lang/System.out:Ljava/io/PrintStream;
      90: swap
      91: invokevirtual #38                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      94: aload_0
      95: bipush        35
      97: invokevirtual #26                 // Method design/AutocompleteSystem.input:(C)Ljava/util/List;
     100: getstatic     #32                 // Field java/lang/System.out:Ljava/io/PrintStream;
     103: swap
     104: invokevirtual #38                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     107: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #43                 // Method main:()V
       3: return
}


package recursion

class WordSearchII {
    class TrieNode {
        val children = HashMap<Char, TrieNode>()
        var isLeaf = false
    }

    private val ret = mutableSetOf<String>()
    private var board: Array<CharArray> = arrayOf<CharArray>()

    private var m: Int = -1
    private var n: Int = -1
    private var visited: BooleanArray = booleanArrayOf()

    private fun find(i: Int, j: Int, cur: TrieNode, sb: StringBuilder) {
        if (board[i][j] in cur.children) {
            visited[i * n + j] = true
            sb.append(board[i][j])
            val next = cur.children[board[i][j]]!!
            if (next.isLeaf) {
                ret.add(sb.toString())
            }

            if (i > 0 && !visited[(i - 1) * n + j]) find(i - 1, j, next, sb)
            if (i < m - 1 && !visited[(i + 1) * n + j]) find(i + 1, j, next, sb)
            if (j > 0 && !visited[i * n + j - 1]) find(i, j - 1, next, sb)
            if (j < n - 1 && !visited[i * n + j + 1]) find(i, j + 1, next, sb)

            sb.deleteCharAt(sb.lastIndex)
            visited[i * n + j] = false
        }
    }

    // O(m * n * max(word length))
    fun findWords(board: Array<CharArray>, words: Array<String>): List<String> {

        this.board = board

        val trieRoot = TrieNode()

        for (word in words) {
            var cur = trieRoot
            for (ch in word) {
                if (ch !in cur.children) {
                    cur.children[ch] = TrieNode()
                }
                cur = cur.children[ch]!!
            }
            cur.isLeaf = true
        }

        m = board.size
        n = board[0].size

        for (i in 0 until m) {
            for (j in 0 until n) {
                visited = BooleanArray(m * n) {false}
                val sb = StringBuilder()
                find(i, j, trieRoot, sb)
            }
        }

        return ret.toList()
    }

}

fun main() {
    println(WordSearchII().findWords(arrayOf(
        charArrayOf('a','b','c','e'),
        charArrayOf('x','x','c','d'),
        charArrayOf('x','x','b','a')),
        arrayOf("abc","abcd")))
}

Compiled from "WordSearchII.kt"
public final class recursion.WordSearchIIKt {
  public static final void main();
    Code:
       0: new           #8                  // class recursion/WordSearchII
       3: dup
       4: invokespecial #11                 // Method recursion/WordSearchII."<init>":()V
       7: iconst_3
       8: anewarray     #13                 // class "[C"
      11: astore_0
      12: aload_0
      13: iconst_0
      14: iconst_4
      15: newarray       char
      17: astore_1
      18: aload_1
      19: iconst_0
      20: bipush        97
      22: castore
      23: aload_1
      24: iconst_1
      25: bipush        98
      27: castore
      28: aload_1
      29: iconst_2
      30: bipush        99
      32: castore
      33: aload_1
      34: iconst_3
      35: bipush        101
      37: castore
      38: aload_1
      39: aastore
      40: aload_0
      41: iconst_1
      42: iconst_4
      43: newarray       char
      45: astore_1
      46: aload_1
      47: iconst_0
      48: bipush        120
      50: castore
      51: aload_1
      52: iconst_1
      53: bipush        120
      55: castore
      56: aload_1
      57: iconst_2
      58: bipush        99
      60: castore
      61: aload_1
      62: iconst_3
      63: bipush        100
      65: castore
      66: aload_1
      67: aastore
      68: aload_0
      69: iconst_2
      70: iconst_4
      71: newarray       char
      73: astore_1
      74: aload_1
      75: iconst_0
      76: bipush        120
      78: castore
      79: aload_1
      80: iconst_1
      81: bipush        120
      83: castore
      84: aload_1
      85: iconst_2
      86: bipush        98
      88: castore
      89: aload_1
      90: iconst_3
      91: bipush        97
      93: castore
      94: aload_1
      95: aastore
      96: aload_0
      97: iconst_2
      98: anewarray     #15                 // class java/lang/String
     101: astore_0
     102: aload_0
     103: iconst_0
     104: ldc           #17                 // String abc
     106: aastore
     107: aload_0
     108: iconst_1
     109: ldc           #19                 // String abcd
     111: aastore
     112: aload_0
     113: invokevirtual #23                 // Method recursion/WordSearchII.findWords:([[C[Ljava/lang/String;)Ljava/util/List;
     116: getstatic     #29                 // Field java/lang/System.out:Ljava/io/PrintStream;
     119: swap
     120: invokevirtual #35                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
     123: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #38                 // Method main:()V
       3: return
}

Compiled from "WordSearchII.kt"
public final class recursion.WordSearchII$TrieNode {
  private final java.util.HashMap<java.lang.Character, recursion.WordSearchII$TrieNode> children;

  private boolean isLeaf;

  public recursion.WordSearchII$TrieNode();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #10                 // class java/util/HashMap
       8: dup
       9: invokespecial #11                 // Method java/util/HashMap."<init>":()V
      12: putfield      #15                 // Field children:Ljava/util/HashMap;
      15: return

  public final java.util.HashMap<java.lang.Character, recursion.WordSearchII$TrieNode> getChildren();
    Code:
       0: aload_0
       1: getfield      #15                 // Field children:Ljava/util/HashMap;
       4: areturn

  public final boolean isLeaf();
    Code:
       0: aload_0
       1: getfield      #26                 // Field isLeaf:Z
       4: ireturn

  public final void setLeaf(boolean);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #26                 // Field isLeaf:Z
       5: return
}

Compiled from "WordSearchII.kt"
public final class recursion.WordSearchII {
  private final java.util.Set<java.lang.String> ret;

  private char[][] board;

  private int m;

  private int n;

  private boolean[] visited;

  public recursion.WordSearchII();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #10                 // class java/util/LinkedHashSet
       8: dup
       9: invokespecial #11                 // Method java/util/LinkedHashSet."<init>":()V
      12: checkcast     #13                 // class java/util/Set
      15: putfield      #17                 // Field ret:Ljava/util/Set;
      18: aload_0
      19: iconst_0
      20: anewarray     #19                 // class "[C"
      23: putfield      #23                 // Field board:[[C
      26: aload_0
      27: iconst_m1
      28: putfield      #27                 // Field m:I
      31: aload_0
      32: iconst_m1
      33: putfield      #30                 // Field n:I
      36: aload_0
      37: iconst_0
      38: newarray       boolean
      40: putfield      #34                 // Field visited:[Z
      43: return

  private final void find(int, int, recursion.WordSearchII$TrieNode, java.lang.StringBuilder);
    Code:
       0: aload_3
       1: invokevirtual #44                 // Method recursion/WordSearchII$TrieNode.getChildren:()Ljava/util/HashMap;
       4: checkcast     #46                 // class java/util/Map
       7: aload_0
       8: getfield      #23                 // Field board:[[C
      11: iload_1
      12: aaload
      13: iload_2
      14: caload
      15: invokestatic  #52                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      18: invokeinterface #56,  2           // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
      23: ifeq          286
      26: aload_0
      27: getfield      #34                 // Field visited:[Z
      30: iload_1
      31: aload_0
      32: getfield      #30                 // Field n:I
      35: imul
      36: iload_2
      37: iadd
      38: iconst_1
      39: bastore
      40: aload         4
      42: aload_0
      43: getfield      #23                 // Field board:[[C
      46: iload_1
      47: aaload
      48: iload_2
      49: caload
      50: invokevirtual #62                 // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
      53: pop
      54: aload_3
      55: invokevirtual #44                 // Method recursion/WordSearchII$TrieNode.getChildren:()Ljava/util/HashMap;
      58: aload_0
      59: getfield      #23                 // Field board:[[C
      62: iload_1
      63: aaload
      64: iload_2
      65: caload
      66: invokestatic  #52                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      69: invokevirtual #68                 // Method java/util/HashMap.get:(Ljava/lang/Object;)Ljava/lang/Object;
      72: dup
      73: invokestatic  #74                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      76: checkcast     #40                 // class recursion/WordSearchII$TrieNode
      79: astore        5
      81: aload         5
      83: invokevirtual #78                 // Method recursion/WordSearchII$TrieNode.isLeaf:()Z
      86: ifeq          110
      89: aload_0
      90: getfield      #17                 // Field ret:Ljava/util/Set;
      93: aload         4
      95: invokevirtual #82                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      98: dup
      99: ldc           #84                 // String toString(...)
     101: invokestatic  #88                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     104: invokeinterface #91,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     109: pop
     110: iload_1
     111: ifle          144
     114: aload_0
     115: getfield      #34                 // Field visited:[Z
     118: iload_1
     119: iconst_1
     120: isub
     121: aload_0
     122: getfield      #30                 // Field n:I
     125: imul
     126: iload_2
     127: iadd
     128: baload
     129: ifne          144
     132: aload_0
     133: iload_1
     134: iconst_1
     135: isub
     136: iload_2
     137: aload         5
     139: aload         4
     141: invokespecial #93                 // Method find:(IILrecursion/WordSearchII$TrieNode;Ljava/lang/StringBuilder;)V
     144: iload_1
     145: aload_0
     146: getfield      #27                 // Field m:I
     149: iconst_1
     150: isub
     151: if_icmpge     184
     154: aload_0
     155: getfield      #34                 // Field visited:[Z
     158: iload_1
     159: iconst_1
     160: iadd
     161: aload_0
     162: getfield      #30                 // Field n:I
     165: imul
     166: iload_2
     167: iadd
     168: baload
     169: ifne          184
     172: aload_0
     173: iload_1
     174: iconst_1
     175: iadd
     176: iload_2
     177: aload         5
     179: aload         4
     181: invokespecial #93                 // Method find:(IILrecursion/WordSearchII$TrieNode;Ljava/lang/StringBuilder;)V
     184: iload_2
     185: ifle          218
     188: aload_0
     189: getfield      #34                 // Field visited:[Z
     192: iload_1
     193: aload_0
     194: getfield      #30                 // Field n:I
     197: imul
     198: iload_2
     199: iadd
     200: iconst_1
     201: isub
     202: baload
     203: ifne          218
     206: aload_0
     207: iload_1
     208: iload_2
     209: iconst_1
     210: isub
     211: aload         5
     213: aload         4
     215: invokespecial #93                 // Method find:(IILrecursion/WordSearchII$TrieNode;Ljava/lang/StringBuilder;)V
     218: iload_2
     219: aload_0
     220: getfield      #30                 // Field n:I
     223: iconst_1
     224: isub
     225: if_icmpge     258
     228: aload_0
     229: getfield      #34                 // Field visited:[Z
     232: iload_1
     233: aload_0
     234: getfield      #30                 // Field n:I
     237: imul
     238: iload_2
     239: iadd
     240: iconst_1
     241: iadd
     242: baload
     243: ifne          258
     246: aload_0
     247: iload_1
     248: iload_2
     249: iconst_1
     250: iadd
     251: aload         5
     253: aload         4
     255: invokespecial #93                 // Method find:(IILrecursion/WordSearchII$TrieNode;Ljava/lang/StringBuilder;)V
     258: aload         4
     260: aload         4
     262: checkcast     #95                 // class java/lang/CharSequence
     265: invokestatic  #101                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     268: invokevirtual #105                // Method java/lang/StringBuilder.deleteCharAt:(I)Ljava/lang/StringBuilder;
     271: pop
     272: aload_0
     273: getfield      #34                 // Field visited:[Z
     276: iload_1
     277: aload_0
     278: getfield      #30                 // Field n:I
     281: imul
     282: iload_2
     283: iadd
     284: iconst_0
     285: bastore
     286: return

  public final java.util.List<java.lang.String> findWords(char[][], java.lang.String[]);
    Code:
       0: aload_1
       1: ldc           #117                // String board
       3: invokestatic  #120                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #122                // String words
       9: invokestatic  #120                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: aload_1
      14: putfield      #23                 // Field board:[[C
      17: new           #40                 // class recursion/WordSearchII$TrieNode
      20: dup
      21: invokespecial #123                // Method recursion/WordSearchII$TrieNode."<init>":()V
      24: astore_3
      25: iconst_0
      26: istore        4
      28: aload_2
      29: arraylength
      30: istore        5
      32: iload         4
      34: iload         5
      36: if_icmpge     169
      39: aload_2
      40: iload         4
      42: aaload
      43: astore        6
      45: aload_3
      46: astore        7
      48: iconst_0
      49: istore        8
      51: aload         6
      53: invokevirtual #129                // Method java/lang/String.length:()I
      56: istore        9
      58: iload         8
      60: iload         9
      62: if_icmpge     157
      65: aload         6
      67: iload         8
      69: invokevirtual #133                // Method java/lang/String.charAt:(I)C
      72: istore        10
      74: iload         10
      76: invokestatic  #52                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      79: astore        12
      81: aload         7
      83: invokevirtual #44                 // Method recursion/WordSearchII$TrieNode.getChildren:()Ljava/util/HashMap;
      86: checkcast     #46                 // class java/util/Map
      89: aload         12
      91: invokeinterface #56,  2           // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
      96: ifne          129
      99: iload         10
     101: invokestatic  #52                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     104: astore        12
     106: aload         7
     108: invokevirtual #44                 // Method recursion/WordSearchII$TrieNode.getChildren:()Ljava/util/HashMap;
     111: checkcast     #46                 // class java/util/Map
     114: aload         12
     116: new           #40                 // class recursion/WordSearchII$TrieNode
     119: dup
     120: invokespecial #123                // Method recursion/WordSearchII$TrieNode."<init>":()V
     123: invokeinterface #137,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     128: pop
     129: aload         7
     131: invokevirtual #44                 // Method recursion/WordSearchII$TrieNode.getChildren:()Ljava/util/HashMap;
     134: iload         10
     136: invokestatic  #52                 // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     139: invokevirtual #68                 // Method java/util/HashMap.get:(Ljava/lang/Object;)Ljava/lang/Object;
     142: dup
     143: invokestatic  #74                 // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     146: checkcast     #40                 // class recursion/WordSearchII$TrieNode
     149: astore        7
     151: iinc          8, 1
     154: goto          58
     157: aload         7
     159: iconst_1
     160: invokevirtual #141                // Method recursion/WordSearchII$TrieNode.setLeaf:(Z)V
     163: iinc          4, 1
     166: goto          32
     169: aload_0
     170: aload_1
     171: checkcast     #143                // class "[Ljava/lang/Object;"
     174: arraylength
     175: putfield      #27                 // Field m:I
     178: aload_0
     179: aload_1
     180: iconst_0
     181: aaload
     182: arraylength
     183: putfield      #30                 // Field n:I
     186: iconst_0
     187: istore        4
     189: aload_0
     190: getfield      #27                 // Field m:I
     193: istore        5
     195: iload         4
     197: iload         5
     199: if_icmpge     303
     202: iconst_0
     203: istore        6
     205: aload_0
     206: getfield      #30                 // Field n:I
     209: istore        7
     211: iload         6
     213: iload         7
     215: if_icmpge     297
     218: aload_0
     219: iconst_0
     220: istore        8
     222: aload_0
     223: getfield      #27                 // Field m:I
     226: aload_0
     227: getfield      #30                 // Field n:I
     230: imul
     231: istore        9
     233: iload         9
     235: newarray       boolean
     237: astore        10
     239: astore        13
     241: iload         8
     243: iload         9
     245: if_icmpge     264
     248: iload         8
     250: istore        11
     252: aload         10
     254: iload         11
     256: iconst_0
     257: bastore
     258: iinc          8, 1
     261: goto          241
     264: aload         13
     266: aload         10
     268: putfield      #34                 // Field visited:[Z
     271: new           #58                 // class java/lang/StringBuilder
     274: dup
     275: invokespecial #144                // Method java/lang/StringBuilder."<init>":()V
     278: astore        8
     280: aload_0
     281: iload         4
     283: iload         6
     285: aload_3
     286: aload         8
     288: invokespecial #93                 // Method find:(IILrecursion/WordSearchII$TrieNode;Ljava/lang/StringBuilder;)V
     291: iinc          6, 1
     294: goto          211
     297: iinc          4, 1
     300: goto          195
     303: aload_0
     304: getfield      #17                 // Field ret:Ljava/util/Set;
     307: checkcast     #146                // class java/lang/Iterable
     310: invokestatic  #152                // Method kotlin/collections/CollectionsKt.toList:(Ljava/lang/Iterable;)Ljava/util/List;
     313: areturn
}


package recursion

private val visited = BooleanArray(9) {false}

fun valid(i: Int, j: Int, ni: Int, nj: Int): Boolean {

    val di = Math.abs(i - ni)
    val dj = Math.abs(j - nj)

    return if ((di == 2 && dj == 2) || (di == 0 && dj == 2) || (di == 2 && dj == 0)) {
        val mi = (i + ni) / 2
        val mj = (j + nj) / 2
        visited[3 * mi + mj]
    } else {
        true
    }
}

fun traverse(i: Int, j: Int, m: Int, n: Int, length: Int): Int {
    visited[3 * i + j] = true
    var counter = 0
    if (length >= m && length <= n) {
        counter++
    }

    for (ni in 0..2) {
        for (nj in 0..2) {
            if (!visited[3 * ni + nj] && valid(i, j, ni, nj)) {
                counter += traverse(ni, nj, m, n, length + 1)
            }
        }
    }

    visited[3 * i + j] = false
    return counter
}

fun numberOfPatterns(m: Int, n: Int): Int {
    var counter = 0
    for (i in 0..2) {
        for (j in 0..2) {
            counter += traverse(i, j, m, n, 1)
        }
    }
    return counter
}

fun main() {
    println(numberOfPatterns(1, 2))
}

Compiled from "AndroidUnlockPatterns.kt"
public final class recursion.AndroidUnlockPatternsKt {
  private static final boolean[] visited;

  public static final boolean valid(int, int, int, int);
    Code:
       0: iload_0
       1: iload_2
       2: isub
       3: invokestatic  #12                 // Method java/lang/Math.abs:(I)I
       6: istore        4
       8: iload_1
       9: iload_3
      10: isub
      11: invokestatic  #12                 // Method java/lang/Math.abs:(I)I
      14: istore        5
      16: iload         4
      18: iconst_2
      19: if_icmpne     28
      22: iload         5
      24: iconst_2
      25: if_icmpeq     50
      28: iload         4
      30: ifne          39
      33: iload         5
      35: iconst_2
      36: if_icmpeq     50
      39: iload         4
      41: iconst_2
      42: if_icmpne     78
      45: iload         5
      47: ifne          78
      50: iload_0
      51: iload_2
      52: iadd
      53: iconst_2
      54: idiv
      55: istore        6
      57: iload_1
      58: iload_3
      59: iadd
      60: iconst_2
      61: idiv
      62: istore        7
      64: getstatic     #16                 // Field visited:[Z
      67: iconst_3
      68: iload         6
      70: imul
      71: iload         7
      73: iadd
      74: baload
      75: goto          79
      78: iconst_1
      79: ireturn

  public static final int traverse(int, int, int, int, int);
    Code:
       0: getstatic     #16                 // Field visited:[Z
       3: iconst_3
       4: iload_0
       5: imul
       6: iload_1
       7: iadd
       8: iconst_1
       9: bastore
      10: iconst_0
      11: istore        5
      13: iload         4
      15: iload_2
      16: if_icmplt     28
      19: iload         4
      21: iload_3
      22: if_icmpgt     28
      25: iinc          5, 1
      28: iconst_0
      29: istore        6
      31: iload         6
      33: iconst_3
      34: if_icmpge     102
      37: iconst_0
      38: istore        7
      40: iload         7
      42: iconst_3
      43: if_icmpge     96
      46: getstatic     #16                 // Field visited:[Z
      49: iconst_3
      50: iload         6
      52: imul
      53: iload         7
      55: iadd
      56: baload
      57: ifne          90
      60: iload_0
      61: iload_1
      62: iload         6
      64: iload         7
      66: invokestatic  #29                 // Method valid:(IIII)Z
      69: ifeq          90
      72: iload         5
      74: iload         6
      76: iload         7
      78: iload_2
      79: iload_3
      80: iload         4
      82: iconst_1
      83: iadd
      84: invokestatic  #31                 // Method traverse:(IIIII)I
      87: iadd
      88: istore        5
      90: iinc          7, 1
      93: goto          40
      96: iinc          6, 1
      99: goto          31
     102: getstatic     #16                 // Field visited:[Z
     105: iconst_3
     106: iload_0
     107: imul
     108: iload_1
     109: iadd
     110: iconst_0
     111: bastore
     112: iload         5
     114: ireturn

  public static final int numberOfPatterns(int, int);
    Code:
       0: iconst_0
       1: istore_2
       2: iconst_0
       3: istore_3
       4: iload_3
       5: iconst_3
       6: if_icmpge     42
       9: iconst_0
      10: istore        4
      12: iload         4
      14: iconst_3
      15: if_icmpge     36
      18: iload_2
      19: iload_3
      20: iload         4
      22: iload_0
      23: iload_1
      24: iconst_1
      25: invokestatic  #31                 // Method traverse:(IIIII)I
      28: iadd
      29: istore_2
      30: iinc          4, 1
      33: goto          12
      36: iinc          3, 1
      39: goto          4
      42: iload_2
      43: ireturn

  public static final void main();
    Code:
       0: iconst_1
       1: iconst_2
       2: invokestatic  #41                 // Method numberOfPatterns:(II)I
       5: istore_0
       6: getstatic     #47                 // Field java/lang/System.out:Ljava/io/PrintStream;
       9: iload_0
      10: invokevirtual #53                 // Method java/io/PrintStream.println:(I)V
      13: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #56                 // Method main:()V
       3: return

  static {};
    Code:
       0: iconst_0
       1: istore_0
       2: bipush        9
       4: newarray       boolean
       6: astore_1
       7: iload_0
       8: bipush        9
      10: if_icmpge     25
      13: iload_0
      14: istore_2
      15: aload_1
      16: iload_2
      17: iconst_0
      18: bastore
      19: iinc          0, 1
      22: goto          7
      25: aload_1
      26: putstatic     #16                 // Field visited:[Z
      29: return
}


package recursion

val squares: MutableList<MutableList<String>> = mutableListOf()

data class TrieNode(val children: MutableMap<Char, TrieNode>)
val root = TrieNode(mutableMapOf())

fun traverse(square: MutableList<String>, cur: TrieNode, word: StringBuilder, words: Array<String>) {
    if (word.length == words[0].length) {
        square.add(word.toString())
        search(square, words)
        square.removeAt(square.lastIndex)
    }

    for (entry in cur.children.entries) {
        word.append(entry.key)
        traverse(square, entry.value, word, words)
        word.deleteCharAt(word.lastIndex)
    }
}

fun search(square: MutableList<String>, words: Array<String>) {
    val n = square.size

    if (square.size == words[0].length) {
        squares.add(ArrayList(square))
        return
    }

    // following tries for finding the given prefix of n characters
    var cur = root
    val sb = StringBuilder()
    for (i in 0 until n) {
        if (square[i][n] in cur!!.children) {
            sb.append(square[i][n])
            cur = cur.children[square[i][n]]!!
        } else {
            break
        }
    }

    // traversing the rest of the trie since all words fit
    if (sb.length == n) {
        traverse(square, cur, sb, words)
    }
}

//O(m^n)
fun wordSquares(words: Array<String>): List<List<String>> {

    for(word in words) {
        var cur = root
        for (ch in word) {
            if (ch !in cur.children) {
                cur.children[ch] = TrieNode(mutableMapOf())
            }
            cur = cur.children[ch]!!
        }
    }

    search(ArrayList(words[0].length), words)
    return squares
}

fun main() {
    println(wordSquares(arrayOf("area","lead","wall","lady","ball")))
}

Compiled from "WordSquares.kt"
public final class recursion.WordSquaresKt {
  private static final java.util.List<java.util.List<java.lang.String>> squares;

  private static final recursion.TrieNode root;

  public static final java.util.List<java.util.List<java.lang.String>> getSquares();
    Code:
       0: getstatic     #12                 // Field squares:Ljava/util/List;
       3: areturn

  public static final recursion.TrieNode getRoot();
    Code:
       0: getstatic     #18                 // Field root:Lrecursion/TrieNode;
       3: areturn

  public static final void traverse(java.util.List<java.lang.String>, recursion.TrieNode, java.lang.StringBuilder, java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #23                 // String square
       3: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #31                 // String cur
       9: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_2
      13: ldc           #33                 // String word
      15: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      18: aload_3
      19: ldc           #35                 // String words
      21: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      24: aload_2
      25: invokevirtual #41                 // Method java/lang/StringBuilder.length:()I
      28: aload_3
      29: iconst_0
      30: aaload
      31: invokevirtual #44                 // Method java/lang/String.length:()I
      34: if_icmpne     70
      37: aload_0
      38: aload_2
      39: invokevirtual #48                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      42: dup
      43: ldc           #50                 // String toString(...)
      45: invokestatic  #53                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
      48: invokeinterface #59,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      53: pop
      54: aload_0
      55: aload_3
      56: invokestatic  #63                 // Method search:(Ljava/util/List;[Ljava/lang/String;)V
      59: aload_0
      60: aload_0
      61: invokestatic  #69                 // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
      64: invokeinterface #73,  2           // InterfaceMethod java/util/List.remove:(I)Ljava/lang/Object;
      69: pop
      70: aload_1
      71: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
      74: invokeinterface #85,  1           // InterfaceMethod java/util/Map.entrySet:()Ljava/util/Set;
      79: invokeinterface #91,  1           // InterfaceMethod java/util/Set.iterator:()Ljava/util/Iterator;
      84: astore        4
      86: aload         4
      88: invokeinterface #97,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      93: ifeq          157
      96: aload         4
      98: invokeinterface #101,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     103: checkcast     #103                // class java/util/Map$Entry
     106: astore        5
     108: aload_2
     109: aload         5
     111: invokeinterface #106,  1          // InterfaceMethod java/util/Map$Entry.getKey:()Ljava/lang/Object;
     116: checkcast     #108                // class java/lang/Character
     119: invokevirtual #112                // Method java/lang/Character.charValue:()C
     122: invokevirtual #116                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     125: pop
     126: aload_0
     127: aload         5
     129: invokeinterface #119,  1          // InterfaceMethod java/util/Map$Entry.getValue:()Ljava/lang/Object;
     134: checkcast     #75                 // class recursion/TrieNode
     137: aload_2
     138: aload_3
     139: invokestatic  #121                // Method traverse:(Ljava/util/List;Lrecursion/TrieNode;Ljava/lang/StringBuilder;[Ljava/lang/String;)V
     142: aload_2
     143: aload_2
     144: checkcast     #123                // class java/lang/CharSequence
     147: invokestatic  #128                // Method kotlin/text/StringsKt.getLastIndex:(Ljava/lang/CharSequence;)I
     150: invokevirtual #132                // Method java/lang/StringBuilder.deleteCharAt:(I)Ljava/lang/StringBuilder;
     153: pop
     154: goto          86
     157: return

  public static final void search(java.util.List<java.lang.String>, java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #23                 // String square
       3: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #35                 // String words
       9: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: invokeinterface #140,  1          // InterfaceMethod java/util/List.size:()I
      18: istore_2
      19: aload_0
      20: invokeinterface #140,  1          // InterfaceMethod java/util/List.size:()I
      25: aload_1
      26: iconst_0
      27: aaload
      28: invokevirtual #44                 // Method java/lang/String.length:()I
      31: if_icmpne     55
      34: getstatic     #12                 // Field squares:Ljava/util/List;
      37: new           #142                // class java/util/ArrayList
      40: dup
      41: aload_0
      42: checkcast     #144                // class java/util/Collection
      45: invokespecial #148                // Method java/util/ArrayList."<init>":(Ljava/util/Collection;)V
      48: invokeinterface #59,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      53: pop
      54: return
      55: getstatic     #18                 // Field root:Lrecursion/TrieNode;
      58: astore_3
      59: new           #37                 // class java/lang/StringBuilder
      62: dup
      63: invokespecial #151                // Method java/lang/StringBuilder."<init>":()V
      66: astore        4
      68: iconst_0
      69: istore        5
      71: iload         5
      73: iload_2
      74: if_icmpge     179
      77: aload_3
      78: dup
      79: invokestatic  #155                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
      82: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
      85: aload_0
      86: iload         5
      88: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      93: checkcast     #43                 // class java/lang/String
      96: iload_2
      97: invokevirtual #162                // Method java/lang/String.charAt:(I)C
     100: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     103: invokeinterface #169,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
     108: ifeq          170
     111: aload         4
     113: aload_0
     114: iload         5
     116: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     121: checkcast     #43                 // class java/lang/String
     124: iload_2
     125: invokevirtual #162                // Method java/lang/String.charAt:(I)C
     128: invokevirtual #116                // Method java/lang/StringBuilder.append:(C)Ljava/lang/StringBuilder;
     131: pop
     132: aload_3
     133: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
     136: aload_0
     137: iload         5
     139: invokeinterface #158,  2          // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     144: checkcast     #43                 // class java/lang/String
     147: iload_2
     148: invokevirtual #162                // Method java/lang/String.charAt:(I)C
     151: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     154: invokeinterface #172,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     159: dup
     160: invokestatic  #155                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     163: checkcast     #75                 // class recursion/TrieNode
     166: astore_3
     167: goto          173
     170: goto          179
     173: iinc          5, 1
     176: goto          71
     179: aload         4
     181: invokevirtual #41                 // Method java/lang/StringBuilder.length:()I
     184: iload_2
     185: if_icmpne     196
     188: aload_0
     189: aload_3
     190: aload         4
     192: aload_1
     193: invokestatic  #121                // Method traverse:(Ljava/util/List;Lrecursion/TrieNode;Ljava/lang/StringBuilder;[Ljava/lang/String;)V
     196: return

  public static final java.util.List<java.util.List<java.lang.String>> wordSquares(java.lang.String[]);
    Code:
       0: aload_0
       1: ldc           #35                 // String words
       3: invokestatic  #29                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_1
       8: aload_0
       9: arraylength
      10: istore_2
      11: iload_1
      12: iload_2
      13: if_icmpge     144
      16: aload_0
      17: iload_1
      18: aaload
      19: astore_3
      20: getstatic     #18                 // Field root:Lrecursion/TrieNode;
      23: astore        4
      25: iconst_0
      26: istore        5
      28: aload_3
      29: invokevirtual #44                 // Method java/lang/String.length:()I
      32: istore        6
      34: iload         5
      36: iload         6
      38: if_icmpge     138
      41: aload_3
      42: iload         5
      44: invokevirtual #162                // Method java/lang/String.charAt:(I)C
      47: istore        7
      49: iload         7
      51: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      54: astore        8
      56: aload         4
      58: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
      61: aload         8
      63: invokeinterface #169,  2          // InterfaceMethod java/util/Map.containsKey:(Ljava/lang/Object;)Z
      68: ifne          108
      71: iload         7
      73: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
      76: astore        8
      78: aload         4
      80: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
      83: aload         8
      85: new           #75                 // class recursion/TrieNode
      88: dup
      89: new           #181                // class java/util/LinkedHashMap
      92: dup
      93: invokespecial #182                // Method java/util/LinkedHashMap."<init>":()V
      96: checkcast     #81                 // class java/util/Map
      99: invokespecial #185                // Method recursion/TrieNode."<init>":(Ljava/util/Map;)V
     102: invokeinterface #189,  3          // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
     107: pop
     108: aload         4
     110: invokevirtual #79                 // Method recursion/TrieNode.getChildren:()Ljava/util/Map;
     113: iload         7
     115: invokestatic  #166                // Method java/lang/Character.valueOf:(C)Ljava/lang/Character;
     118: invokeinterface #172,  2          // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
     123: dup
     124: invokestatic  #155                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     127: checkcast     #75                 // class recursion/TrieNode
     130: astore        4
     132: iinc          5, 1
     135: goto          34
     138: iinc          1, 1
     141: goto          11
     144: new           #142                // class java/util/ArrayList
     147: dup
     148: aload_0
     149: iconst_0
     150: aaload
     151: invokevirtual #44                 // Method java/lang/String.length:()I
     154: invokespecial #192                // Method java/util/ArrayList."<init>":(I)V
     157: checkcast     #55                 // class java/util/List
     160: aload_0
     161: invokestatic  #63                 // Method search:(Ljava/util/List;[Ljava/lang/String;)V
     164: getstatic     #12                 // Field squares:Ljava/util/List;
     167: areturn

  public static final void main();
    Code:
       0: iconst_5
       1: anewarray     #43                 // class java/lang/String
       4: astore_0
       5: aload_0
       6: iconst_0
       7: ldc           #199                // String area
       9: aastore
      10: aload_0
      11: iconst_1
      12: ldc           #201                // String lead
      14: aastore
      15: aload_0
      16: iconst_2
      17: ldc           #203                // String wall
      19: aastore
      20: aload_0
      21: iconst_3
      22: ldc           #205                // String lady
      24: aastore
      25: aload_0
      26: iconst_4
      27: ldc           #207                // String ball
      29: aastore
      30: aload_0
      31: invokestatic  #209                // Method wordSquares:([Ljava/lang/String;)Ljava/util/List;
      34: getstatic     #215                // Field java/lang/System.out:Ljava/io/PrintStream;
      37: swap
      38: invokevirtual #220                // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      41: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #223                // Method main:()V
       3: return

  static {};
    Code:
       0: new           #142                // class java/util/ArrayList
       3: dup
       4: invokespecial #226                // Method java/util/ArrayList."<init>":()V
       7: checkcast     #55                 // class java/util/List
      10: putstatic     #12                 // Field squares:Ljava/util/List;
      13: new           #75                 // class recursion/TrieNode
      16: dup
      17: new           #181                // class java/util/LinkedHashMap
      20: dup
      21: invokespecial #182                // Method java/util/LinkedHashMap."<init>":()V
      24: checkcast     #81                 // class java/util/Map
      27: invokespecial #185                // Method recursion/TrieNode."<init>":(Ljava/util/Map;)V
      30: putstatic     #18                 // Field root:Lrecursion/TrieNode;
      33: return
}


package sortingandsearching

// O(log(min(m, n)))
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
    val a = if (nums1.size < nums2.size) nums1 else nums2
    val b = if (nums1.size < nums2.size) nums2 else nums1

    var l = 0
    var r = a.size
    var i: Int
    var j: Int
    var found: Boolean
    do {
        i = (l + r) / 2
        j = (a.size + b.size) / 2 - i
        found = true

        if (i > 0 && i <= a.size && j >= 0 && j < b.size && a[i - 1] > b[j]) {
            r = i - 1
            found = false
        }

        if (i >= 0 && i < a.size && j > 0 && j <= b.size && b[j - 1] > a[i]) {
            l = i + 1
            found = false
        }
    } while (!found)

    val left = maxOf(
        if (i > 0 && i <= a.size) a[i - 1] else Int.MIN_VALUE,
        if (j > 0 && j <= b.size) b[j - 1] else Int.MIN_VALUE)

    val right =  minOf(
        if (i >= 0 && i < a.size) a[i] else Int.MAX_VALUE,
        if (j >= 0 && j < b.size) b[j] else Int.MAX_VALUE)

    return if ((a.size + b.size) % 2 == 0)
        (left + right) / 2.0
    else
        right / 1.0
}

fun main() {
    println(findMedianSortedArrays(intArrayOf(2,3,4,5,6,7), intArrayOf(1)))
}

Compiled from "MedianOfTwoSortedArrays.kt"
public final class sortingandsearching.MedianOfTwoSortedArraysKt {
  public static final double findMedianSortedArrays(int[], int[]);
    Code:
       0: aload_0
       1: ldc           #9                  // String nums1
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_1
       7: ldc           #17                 // String nums2
       9: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: aload_0
      13: arraylength
      14: aload_1
      15: arraylength
      16: if_icmpge     23
      19: aload_0
      20: goto          24
      23: aload_1
      24: astore_2
      25: aload_0
      26: arraylength
      27: aload_1
      28: arraylength
      29: if_icmpge     36
      32: aload_1
      33: goto          37
      36: aload_0
      37: astore_3
      38: iconst_0
      39: istore        4
      41: aload_2
      42: arraylength
      43: istore        5
      45: iconst_0
      46: istore        6
      48: iconst_0
      49: istore        7
      51: iconst_0
      52: istore        8
      54: iload         4
      56: iload         5
      58: iadd
      59: iconst_2
      60: idiv
      61: istore        6
      63: aload_2
      64: arraylength
      65: aload_3
      66: arraylength
      67: iadd
      68: iconst_2
      69: idiv
      70: iload         6
      72: isub
      73: istore        7
      75: iconst_1
      76: istore        8
      78: iload         6
      80: ifle          124
      83: iload         6
      85: aload_2
      86: arraylength
      87: if_icmpgt     124
      90: iload         7
      92: iflt          124
      95: iload         7
      97: aload_3
      98: arraylength
      99: if_icmpge     124
     102: aload_2
     103: iload         6
     105: iconst_1
     106: isub
     107: iaload
     108: aload_3
     109: iload         7
     111: iaload
     112: if_icmple     124
     115: iload         6
     117: iconst_1
     118: isub
     119: istore        5
     121: iconst_0
     122: istore        8
     124: iload         6
     126: iflt          170
     129: iload         6
     131: aload_2
     132: arraylength
     133: if_icmpge     170
     136: iload         7
     138: ifle          170
     141: iload         7
     143: aload_3
     144: arraylength
     145: if_icmpgt     170
     148: aload_3
     149: iload         7
     151: iconst_1
     152: isub
     153: iaload
     154: aload_2
     155: iload         6
     157: iaload
     158: if_icmple     170
     161: iload         6
     163: iconst_1
     164: iadd
     165: istore        4
     167: iconst_0
     168: istore        8
     170: iload         8
     172: ifeq          54
     175: nop
     176: iload         6
     178: ifle          197
     181: iload         6
     183: aload_2
     184: arraylength
     185: if_icmpgt     197
     188: aload_2
     189: iload         6
     191: iconst_1
     192: isub
     193: iaload
     194: goto          199
     197: ldc           #18                 // int -2147483648
     199: iload         7
     201: ifle          220
     204: iload         7
     206: aload_3
     207: arraylength
     208: if_icmpgt     220
     211: aload_3
     212: iload         7
     214: iconst_1
     215: isub
     216: iaload
     217: goto          222
     220: ldc           #18                 // int -2147483648
     222: invokestatic  #24                 // Method java/lang/Math.max:(II)I
     225: istore        9
     227: nop
     228: iload         6
     230: iflt          247
     233: iload         6
     235: aload_2
     236: arraylength
     237: if_icmpge     247
     240: aload_2
     241: iload         6
     243: iaload
     244: goto          249
     247: ldc           #25                 // int 2147483647
     249: iload         7
     251: iflt          268
     254: iload         7
     256: aload_3
     257: arraylength
     258: if_icmpge     268
     261: aload_3
     262: iload         7
     264: iaload
     265: goto          270
     268: ldc           #25                 // int 2147483647
     270: invokestatic  #28                 // Method java/lang/Math.min:(II)I
     273: istore        10
     275: aload_2
     276: arraylength
     277: aload_3
     278: arraylength
     279: iadd
     280: iconst_2
     281: irem
     282: ifne          298
     285: iload         9
     287: iload         10
     289: iadd
     290: i2d
     291: ldc2_w        #29                 // double 2.0d
     294: ddiv
     295: goto          303
     298: iload         10
     300: i2d
     301: dconst_1
     302: ddiv
     303: dreturn

  public static final void main();
    Code:
       0: bipush        6
       2: newarray       int
       4: astore_0
       5: aload_0
       6: iconst_0
       7: iconst_2
       8: iastore
       9: aload_0
      10: iconst_1
      11: iconst_3
      12: iastore
      13: aload_0
      14: iconst_2
      15: iconst_4
      16: iastore
      17: aload_0
      18: iconst_3
      19: iconst_5
      20: iastore
      21: aload_0
      22: iconst_4
      23: bipush        6
      25: iastore
      26: aload_0
      27: iconst_5
      28: bipush        7
      30: iastore
      31: aload_0
      32: iconst_1
      33: newarray       int
      35: astore_0
      36: aload_0
      37: iconst_0
      38: iconst_1
      39: iastore
      40: aload_0
      41: invokestatic  #47                 // Method findMedianSortedArrays:([I[I)D
      44: dstore_0
      45: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
      48: dload_0
      49: invokevirtual #59                 // Method java/io/PrintStream.println:(D)V
      52: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #62                 // Method main:()V
       3: return
}


package sortingandsearching

private const val OFFSET = 10000
private const val SIZE = 2 * 10000 + 1

private val segmentTree = IntArray(4 * SIZE) { 0 }

private fun update(vertex: Int, left: Int, right: Int, pos: Int, value: Int) {
    if (left == right) {
        segmentTree[vertex] += value
        return
    }
    val middle = (left + right) / 2
    if (pos <= middle) {
        update(2 * vertex, left, middle, pos, value)
    } else {
        update(2 * vertex + 1,middle + 1, right, pos, value)
    }
    segmentTree[vertex] = segmentTree[2 * vertex] + segmentTree[2 * vertex + 1]
}

private fun query(vertex: Int, left: Int, right: Int, queryLeft: Int, queryRight: Int): Int {
    if (left == queryLeft && right == queryRight) {
        return segmentTree[vertex]
    }
    val middle = (left + right) / 2
    return when {
        queryRight <= middle -> query(2 * vertex, left, middle, queryLeft, queryRight)
        queryLeft > middle -> query(2 * vertex + 1, middle + 1, right, queryLeft, queryRight)
        else -> query(2 * vertex, left, middle, queryLeft, middle) +
                query(2 * vertex + 1, middle + 1, right, middle + 1, queryRight)
    }
}

// O(N*logN)
private fun countSmaller(nums: IntArray): List<Int> {
    val ret = mutableListOf<Int>()
    // O(N * logN)
    for (num in nums.reversed()) {
        // O(logN)
        update(1, 0, SIZE, num + OFFSET, 1)
        // O(logN)
        ret.add(query(1, 0, SIZE, 0, num + OFFSET - 1))
    }
    return ret.reversed()
}

fun main() {
    println(countSmaller(intArrayOf(5,2,6,1)))
}

Compiled from "CountOfSmallerNumbersAfterSelf.kt"
public final class sortingandsearching.CountOfSmallerNumbersAfterSelfKt {
  private static final int OFFSET;

  private static final int SIZE;

  private static final int[] segmentTree;

  private static final void update(int, int, int, int, int);
    Code:
       0: iload_1
       1: iload_2
       2: if_icmpne     22
       5: getstatic     #10                 // Field segmentTree:[I
       8: astore        5
      10: aload         5
      12: iload_0
      13: aload         5
      15: iload_0
      16: iaload
      17: iload         4
      19: iadd
      20: iastore
      21: return
      22: iload_1
      23: iload_2
      24: iadd
      25: iconst_2
      26: idiv
      27: istore        5
      29: iload_3
      30: iload         5
      32: if_icmpgt     50
      35: iconst_2
      36: iload_0
      37: imul
      38: iload_1
      39: iload         5
      41: iload_3
      42: iload         4
      44: invokestatic  #12                 // Method update:(IIIII)V
      47: goto          66
      50: iconst_2
      51: iload_0
      52: imul
      53: iconst_1
      54: iadd
      55: iload         5
      57: iconst_1
      58: iadd
      59: iload_2
      60: iload_3
      61: iload         4
      63: invokestatic  #12                 // Method update:(IIIII)V
      66: getstatic     #10                 // Field segmentTree:[I
      69: iload_0
      70: getstatic     #10                 // Field segmentTree:[I
      73: iconst_2
      74: iload_0
      75: imul
      76: iaload
      77: getstatic     #10                 // Field segmentTree:[I
      80: iconst_2
      81: iload_0
      82: imul
      83: iconst_1
      84: iadd
      85: iaload
      86: iadd
      87: iastore
      88: return

  private static final int query(int, int, int, int, int);
    Code:
       0: iload_1
       1: iload_3
       2: if_icmpne     17
       5: iload_2
       6: iload         4
       8: if_icmpne     17
      11: getstatic     #10                 // Field segmentTree:[I
      14: iload_0
      15: iaload
      16: ireturn
      17: iload_1
      18: iload_2
      19: iadd
      20: iconst_2
      21: idiv
      22: istore        5
      24: nop
      25: iload         4
      27: iload         5
      29: if_icmpgt     47
      32: iconst_2
      33: iload_0
      34: imul
      35: iload_1
      36: iload         5
      38: iload_3
      39: iload         4
      41: invokestatic  #23                 // Method query:(IIIII)I
      44: goto          104
      47: iload_3
      48: iload         5
      50: if_icmple     72
      53: iconst_2
      54: iload_0
      55: imul
      56: iconst_1
      57: iadd
      58: iload         5
      60: iconst_1
      61: iadd
      62: iload_2
      63: iload_3
      64: iload         4
      66: invokestatic  #23                 // Method query:(IIIII)I
      69: goto          104
      72: iconst_2
      73: iload_0
      74: imul
      75: iload_1
      76: iload         5
      78: iload_3
      79: iload         5
      81: invokestatic  #23                 // Method query:(IIIII)I
      84: iconst_2
      85: iload_0
      86: imul
      87: iconst_1
      88: iadd
      89: iload         5
      91: iconst_1
      92: iadd
      93: iload_2
      94: iload         5
      96: iconst_1
      97: iadd
      98: iload         4
     100: invokestatic  #23                 // Method query:(IIIII)I
     103: iadd
     104: ireturn

  private static final java.util.List<java.lang.Integer> countSmaller(int[]);
    Code:
       0: new           #30                 // class java/util/ArrayList
       3: dup
       4: invokespecial #34                 // Method java/util/ArrayList."<init>":()V
       7: checkcast     #36                 // class java/util/List
      10: astore_1
      11: aload_0
      12: invokestatic  #41                 // Method kotlin/collections/ArraysKt.reversed:([I)Ljava/util/List;
      15: astore_2
      16: iconst_0
      17: istore_3
      18: aload_2
      19: invokeinterface #45,  1           // InterfaceMethod java/util/List.size:()I
      24: istore        4
      26: iload_3
      27: iload         4
      29: if_icmpge     95
      32: aload_2
      33: iload_3
      34: invokeinterface #49,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
      39: checkcast     #51                 // class java/lang/Number
      42: invokevirtual #54                 // Method java/lang/Number.intValue:()I
      45: istore        5
      47: iconst_1
      48: iconst_0
      49: sipush        20001
      52: iload         5
      54: sipush        10000
      57: iadd
      58: iconst_1
      59: invokestatic  #12                 // Method update:(IIIII)V
      62: aload_1
      63: iconst_1
      64: iconst_0
      65: sipush        20001
      68: iconst_0
      69: iload         5
      71: sipush        10000
      74: iadd
      75: iconst_1
      76: isub
      77: invokestatic  #23                 // Method query:(IIIII)I
      80: invokestatic  #60                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      83: invokeinterface #64,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      88: pop
      89: iinc          3, 1
      92: goto          26
      95: aload_1
      96: checkcast     #66                 // class java/lang/Iterable
      99: invokestatic  #71                 // Method kotlin/collections/CollectionsKt.reversed:(Ljava/lang/Iterable;)Ljava/util/List;
     102: areturn

  public static final void main();
    Code:
       0: iconst_4
       1: newarray       int
       3: astore_0
       4: aload_0
       5: iconst_0
       6: iconst_5
       7: iastore
       8: aload_0
       9: iconst_1
      10: iconst_2
      11: iastore
      12: aload_0
      13: iconst_2
      14: bipush        6
      16: iastore
      17: aload_0
      18: iconst_3
      19: iconst_1
      20: iastore
      21: aload_0
      22: invokestatic  #79                 // Method countSmaller:([I)Ljava/util/List;
      25: getstatic     #85                 // Field java/lang/System.out:Ljava/io/PrintStream;
      28: swap
      29: invokevirtual #91                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
      32: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #94                 // Method main:()V
       3: return

  static {};
    Code:
       0: iconst_0
       1: istore_0
       2: ldc           #98                 // int 80004
       4: newarray       int
       6: astore_1
       7: iload_0
       8: ldc           #98                 // int 80004
      10: if_icmpge     25
      13: iload_0
      14: istore_2
      15: aload_1
      16: iload_2
      17: iconst_0
      18: iastore
      19: iinc          0, 1
      22: goto          7
      25: aload_1
      26: putstatic     #10                 // Field segmentTree:[I
      29: return
}


package dynamicprogramming

private var memo = arrayOf<IntArray>()

fun dp(nums: IntArray, m: Int, index: Int): Int {
    if (memo[m][index] != -1) {
        return memo[m][index]
    }
    if (m == 1) {
        var sum = 0
        for (i in index .. nums.lastIndex) {
            sum += nums[i]
        }
        memo[m][index] = sum
        return sum
    }
    var sum = nums[index]
    var bestDivide = Int.MAX_VALUE
    for (i in index + 1 .. nums.lastIndex) {
        val divide = maxOf(sum, dp(nums, m - 1, i))
        bestDivide = minOf(divide, bestDivide)
        sum += nums[i]
    }
    memo[m][index] = bestDivide
    return bestDivide
}

fun splitArray(nums: IntArray, m: Int): Int {
    memo = Array(m + 1) { IntArray(nums.size) { -1 } }
    return dp(nums, m, 0)
}

fun main() {
    println(splitArray(intArrayOf(7,2,5,10,8), 2))
}

Compiled from "SplitArrayLargestSum.kt"
public final class dynamicprogramming.SplitArrayLargestSumKt {
  private static int[][] memo;

  public static final int dp(int[], int, int);
    Code:
       0: aload_0
       1: ldc           #9                  // String nums
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: getstatic     #19                 // Field memo:[[I
       9: iload_1
      10: aaload
      11: iload_2
      12: iaload
      13: iconst_m1
      14: if_icmpeq     25
      17: getstatic     #19                 // Field memo:[[I
      20: iload_1
      21: aaload
      22: iload_2
      23: iaload
      24: ireturn
      25: iload_1
      26: iconst_1
      27: if_icmpne     78
      30: iconst_0
      31: istore_3
      32: iload_2
      33: istore        4
      35: aload_0
      36: invokestatic  #25                 // Method kotlin/collections/ArraysKt.getLastIndex:([I)I
      39: istore        5
      41: iload         4
      43: iload         5
      45: if_icmpgt     68
      48: iload_3
      49: aload_0
      50: iload         4
      52: iaload
      53: iadd
      54: istore_3
      55: iload         4
      57: iload         5
      59: if_icmpeq     68
      62: iinc          4, 1
      65: goto          48
      68: getstatic     #19                 // Field memo:[[I
      71: iload_1
      72: aaload
      73: iload_2
      74: iload_3
      75: iastore
      76: iload_3
      77: ireturn
      78: aload_0
      79: iload_2
      80: iaload
      81: istore_3
      82: ldc           #26                 // int 2147483647
      84: istore        4
      86: iload_2
      87: iconst_1
      88: iadd
      89: istore        5
      91: aload_0
      92: invokestatic  #25                 // Method kotlin/collections/ArraysKt.getLastIndex:([I)I
      95: istore        6
      97: iload         5
      99: iload         6
     101: if_icmpgt     148
     104: iload_3
     105: aload_0
     106: iload_1
     107: iconst_1
     108: isub
     109: iload         5
     111: invokestatic  #28                 // Method dp:([III)I
     114: invokestatic  #34                 // Method java/lang/Math.max:(II)I
     117: istore        7
     119: iload         7
     121: iload         4
     123: invokestatic  #37                 // Method java/lang/Math.min:(II)I
     126: istore        4
     128: iload_3
     129: aload_0
     130: iload         5
     132: iaload
     133: iadd
     134: istore_3
     135: iload         5
     137: iload         6
     139: if_icmpeq     148
     142: iinc          5, 1
     145: goto          104
     148: getstatic     #19                 // Field memo:[[I
     151: iload_1
     152: aaload
     153: iload_2
     154: iload         4
     156: iastore
     157: iload         4
     159: ireturn

  public static final int splitArray(int[], int);
    Code:
       0: aload_0
       1: ldc           #9                  // String nums
       3: invokestatic  #15                 // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: iconst_0
       7: istore_2
       8: iload_1
       9: iconst_1
      10: iadd
      11: istore_3
      12: iload_3
      13: anewarray     #46                 // class "[I"
      16: astore        4
      18: iload_2
      19: iload_3
      20: if_icmpge     83
      23: iload_2
      24: istore        5
      26: aload         4
      28: iload         5
      30: iconst_0
      31: istore        6
      33: aload_0
      34: arraylength
      35: istore        7
      37: iload         7
      39: newarray       int
      41: astore        8
      43: istore        11
      45: astore        10
      47: iload         6
      49: iload         7
      51: if_icmpge     70
      54: iload         6
      56: istore        9
      58: aload         8
      60: iload         9
      62: iconst_m1
      63: iastore
      64: iinc          6, 1
      67: goto          47
      70: aload         10
      72: iload         11
      74: aload         8
      76: aastore
      77: iinc          2, 1
      80: goto          18
      83: aload         4
      85: putstatic     #19                 // Field memo:[[I
      88: aload_0
      89: iload_1
      90: iconst_0
      91: invokestatic  #28                 // Method dp:([III)I
      94: ireturn

  public static final void main();
    Code:
       0: iconst_5
       1: newarray       int
       3: astore_0
       4: aload_0
       5: iconst_0
       6: bipush        7
       8: iastore
       9: aload_0
      10: iconst_1
      11: iconst_2
      12: iastore
      13: aload_0
      14: iconst_2
      15: iconst_5
      16: iastore
      17: aload_0
      18: iconst_3
      19: bipush        10
      21: iastore
      22: aload_0
      23: iconst_4
      24: bipush        8
      26: iastore
      27: aload_0
      28: iconst_2
      29: invokestatic  #53                 // Method splitArray:([II)I
      32: istore_0
      33: getstatic     #59                 // Field java/lang/System.out:Ljava/io/PrintStream;
      36: iload_0
      37: invokevirtual #65                 // Method java/io/PrintStream.println:(I)V
      40: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #68                 // Method main:()V
       3: return

  static {};
    Code:
       0: iconst_0
       1: anewarray     #46                 // class "[I"
       4: putstatic     #19                 // Field memo:[[I
       7: return
}