Problem Statement
Cat Noku has just finished writing his first computer program. Noku's computer has m memory cells. The cells have addresses 0 through m-1. Noku's program consists of n instructions. The instructions have mutually independent effects and therefore they may be executed in any order. The instructions must be executed sequentially (i.e., one after another) and each instruction must be executed exactly once.
You are given a description of the n instructions as a
Noku's computer uses caching, which influences the time needed to execute an instruction. More precisely, executing an instruction takes k^2 units of time, where k is the number of new memory cells this instruction accesses. (I.e., k is the number of memory cells that are accessed by this instruction but have not been accessed by any previously executed instruction. Note that k may be zero, in which case the current instruction is indeed executed in 0 units of time.)
Noku's instructions can be executed in many different orders. Clearly, different orders may lead to a different total time of execution. Find and return the shortest amount of time in which it is possible to execute all instructions.
Definition
- Class:
- OrderOfOperationsDiv2
- Method:
- minTime
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int minTime(String[] s)
- (be sure your method is public)
Constraints
- n,m will be between 1 and 20, inclusive.
- s will have exactly n elements.
- Each element of s will have exactly m characters.
- Each character of s[i] will be either '0' or '1' for all valid i.
Examples
{ "111", "001", "010" }
Returns: 3
Cat Noku has 3 instructions. The first instruction ("111") accesses all three memory cells. The second instruction ("001") accesses only memory cell 2. The third instruction ("010") accesses only memory cell 1. If Noku executes these three instructions in the given order, it will take 3^2 + 0^2 + 0^2 = 9 units of time. However, if he executes them in the order "second, third, first", it will take only 1^2 + 1^2 + 1^2 = 3 units of time. This is one optimal solution. Another optimal solution is to execute the instructions in the order "third, second, first".
{ "11101", "00111", "10101", "00000", "11000" }
Returns: 9
{ "11111111111111111111" }
Returns: 400
A single instruction that accesses all 20 memory cells.
{ "1000", "1100", "1110" }
Returns: 3
{ "111", "111", "110", "100" }
Returns: 3
{"0"}
Returns: 0
{"1"}
Returns: 1
{"1000","1101","0001","0100","0101","1011","0000","0011","1011","1111","0100","0010","0000","0010","0010","0000","0100","0101","0001","1100"}
Returns: 4
{"1010","0011","0000","1011","0000","1101","0000","1001","0000","0010","0001","0110","0100","0000","1100","0000","0010","1011","0101","0000"}
Returns: 4
{"111","110","111","101","110","101","101","111","111","010","111","101","110","000","100","011","111","110","011","100"}
Returns: 3
{"1010100111","1111111101","1111111111","1101101110","0111111011","1111010111","0101111101","1011111111","0111111011","1111111110","0111111110","1010111111","1111110111","1111111111","1111111111","1110111111","1111110101","1101111111","0111011111","0111101111"}
Returns: 42
{"11011","00101","11001","01011","01111","01111","00011","10111","01001","11101","01011","11110","01001","11011","11110","11011","01111","01001","10111","11110"}
Returns: 7
{"101","000","011","110","101","010","100","101","101","110","110","011","111","110","110","101","110","111","111","110"}
Returns: 3
{"11","01","11","11","11","10","10","11","11","11","10","11","01","11","11","11","11","11","11","11"}
Returns: 2
{"1111111","1111111","1111111","1111111","1110111","1111111","1111111","1111111","1111111","1111111","1111110","1111111","1101111","1111111","1111111","1111111","1111111","1111111","1111111","0111111"}
Returns: 37
{"00001001001000000","00000000000000000","01001001000000100","01000000010000000","00000010011000000","00100000000000100","00100000000000000","01000011000000000","10000000000000000","00000101000000000","00100000000010000","00010010000000010","00000000000000000","00100000000000000","00000010000000010","00000000000100000","10000000000010000","00010000000000001","00100000000100000","00001000000000000"}
Returns: 17
{"00","00","11","00","00","01","00","00","00","11","00","00","00","00","10","00","00","00","10","01"}
Returns: 2
{"11111111111111111111","11011101111111111111","00011101111111100111","11111011111110111000","11111110111111111111","11111011111101111011","11101111111111111111","11111111011111111111","11011111110111111111","11001111001101010111","11111111111110111111","11110111011111111110","11111101111011111111","11101101111111111111","10111111111011111111","11111101101111111000","11111100111111111111","11110001110111111111","11111111110001101011","11111101111111110110"}
Returns: 196
{"01111100010000100000","00101100111111110000","10001010100101110010","10110101011000001101","11001111111000101010","00101001010111010110","11111111010000010010","01011001110110101110","11011101110110100010","00010001101000001000","00110001001000010010","11000000010001000101","00001110000000011100","01100010101000100011","10011011010000101000","01101010111100101111","11001100001001001100","10101101111000010010","01010011100001100011","00110000100111110110"}
Returns: 60
{"01000100000000001101","00000000000000010001","00000000000000000010","01100000010000000000","00000010000100110000","00010101000001000000","00000010010000010000","00010010000001000000","10000000000000000000","00001011001001010001","00000000000111000000","00101001000000000010","01000000000001100000","00101001000100000000","01100101100010000000","00000100000101011110","00010001000001001011","00100000100000010100","00000100000010000010","00000000010010000000"}
Returns: 26
{"11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111011111111111","11111111111111111111","11111111111011111111","11110111111111111111","11111111111111111111","11111011111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111111111","11111111111111110111"}
Returns: 362
{"11101100111101011001","00111111101111111111","01111101000011101110","10110100110000011111","01111111110101110011","11011010101110100011","11010011101101010100","11110111100011010100","11011110011010111001","11100011101010101011","00101010010111000011","01110100001011011110","01110011000000001111","11100101000011011100","11000100001101111111","11110101011101011010","11011010101011110010","01010100011001111010","00001100111100010010","01111101101111101000"}
Returns: 102
{"11011111011100011101","01010111111101110101","11000101110011110110","00000011010010101111","00011011110110110010","11101111010011100110","00011101110101101111","11100111100111011111","00010101111110111111","11011111101101100100","11001010001101110111","10010110101111001101","11011110101111110111","10101101111110101100","11110101111111111110","10101000101111111011","01111011111100001101","01001110010101011110","11101111101001001010","11101011000110011001"}
Returns: 114
{"11001010010111000110","11111010000101101001","00100000011001111111","11101011100100111101","00101011111111010010","01111111100000001110","10100111001111101101","00000001011011011001","01010101101011111111","11000110100101110100","10000100011010111101","00101100111100001011","10101110111111000101","00110001001101001011","11010100011110110111","10000111111011101111","00010001010111011011","11010011101100110011","00010111001010011111","10100101000110000100"}
Returns: 86
{"00010000101001000001","10010010010001011010","10010001000000110000","10011000100100011100","01110001100010100001","01000010100001100110","00100100011101110101","00011010011001000001","01001010010111010111","01111110001001010010","10110110001100010001","10011100001000010101","11100101111111110100","00011000011011101010","01000000101110000101","00100011101011010110","00001000101100000100","00010000001001100011","11000100111100101010","00111011001101000101"}
Returns: 50
{"11111110011111011010","11011011010011011111","10100110111011111110","00001111111010011010","11111011011011111111","10111111101011110111","10110111110111111110","01111111011100011010","11111111101111111111","10011111111100110100","11111010100111011010","01110110101110111010","11111011110111011111","00111111011101111101","11111000100110001010","11101110110010011101","11110101111111111110","11111011110100111001","11011101001011001010","10111111101111111001"}
Returns: 122
{"11001101111101110011","00011111011111001110","10010011111110110110","10110111111000000101","11110000110111011111","01001100111001100111","10001110101101111011","10101100001101001100","10010111000110100100","10100001011110111110","01011001001110101110","11111110000110111100","01101110101110101111","00010011110101111010","11110001000001010110","01101110010100111100","11110110110111010100","00111001011110101000","00101000001111001110","00101010100001001000"}
Returns: 78
{"0111110111101","0111111111111","1111111111111","1111111111111","1111111111111","1111111111111","1111111101111","1111101111110","1111111111111","1111111111111","1101111110111","1111111111011"}
Returns: 105
{"111011111111111","111111111111111","111111101111011","111111111111111","111111101111111","111111111110111","011110111111111"}
Returns: 171
{"10000000","00100000","00000000","00000000","00001001","00000000","00000000","00000000","00010000","00000100","00010000","00000000","00100000","00000000","00000100","00000000","00000000","00001000"}
Returns: 6
{"000000000000","000000001000","000000000000","000000000000","010000000000","000000000000","010000000000","000000000000","000000000000","001000000000","000000000000","000000000000","000000100000","010000000000","000000001000","000000000000","000000000000","100000000000"}
Returns: 5
{"1010001000010101110","0101110011001101100","0111011110000001001","1010110111111011010","1010101000101100100","1111001000001110011","0101010101000000111","0011011110011100101","0001100011001010010","1010110011000110100","1110000110010011101","1100001001110001100","0011000111010001011","0100000000011000000","0000011101001111001","1011100101001111100","1101101000111100001","0001011110110000100","0110011111000010101"}
Returns: 63
{"0","0","0","0","0","0","0","0","0"}
Returns: 0
{"0000001000000000","0000000000000000","0000000000000000","0001000000100000","0000000000000100","0000000101000000","0000000100000000","0010000000000000","0000000010000010","0000100010000000","0000000011000010","0001000000000000"}
Returns: 12
{"0101011011","1111111111","1011111111","1111101110","1111111110","0111111101"}
Returns: 46
{"11111001111","01000111110","10001111110","10011011110","01101110011","10110111111","00101111111","11110011111","11011111111","10111001011","01110100111","01011111101","01011111101","00000100111","11110001101"}
Returns: 31
{"1011110111101","0101101101010","1110011111111","0101110101001","1111101101111","1010110111010","1111010100011","1111100101111","1110011111111","0001100111001","0110011111110","1100111111111","1011110101111"}
Returns: 49
{"00100011","00011111","00000010","10101011","11000110","10100101","01111100","11101001","11110100","01110110","00011001","00111100","01011100"}
Returns: 12
{"00010000011","00001100000","10000101010","00100000001","11111100100","00010000010","00010001000","11100000100","11000000001","00001000000","10100001100","10100000111"}
Returns: 12
{"0","0","1","0","0","0","0","0"}
Returns: 1
{"1001","0100","1110","0110","0100","0000"}
Returns: 4
{"10000011110011","10001011010011","00001010000100","01010001000101","01000000100100","10010000001000","10010111010001","01110010000000","11000001001000","10101000001000"}
Returns: 26
{"00000000","00100000","00000000"}
Returns: 1
{"1111111","1111101","1111110"}
Returns: 37
{"11111111111111","11111111111111","10111111111111","11111101111111","11111111111111","11010011111111","11111111111111"}
Returns: 130
{"00000","00010","10000","11010"}
Returns: 3
{"11111101111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111","11111111111"}
Returns: 101
{ "110000", "111000", "001100", "001110", "000011", "000111" }
Returns: 8
{ "11000", "00111", "10111", "01111" }
Returns: 11
{"10010011100101001001", "11011011011100001001", "11111011111101000011", "00100111101000001101", "00010101000010100101", "01110011010101001001", "00111001010000111111", "10011111101100001100", "10000100101011101100", "10100100100100001100", "10110011110000111001", "10110101100110111111", "11000000110111101111", "11011010011110011000", "00001000011001101110", "10010101001000110011", "11001000011111101111", "11011011110110110100", "01100001111011001100", "10100010010100000000" }
Returns: 60
{"1100", "0011", "0111" }
Returns: 6
{"1101" }
Returns: 9
{"111", "001", "010" }
Returns: 3
{"111", "000", "000" }
Returns: 9
{"0001111111111", "1111100000000", "0000000111111" }
Returns: 61
{"11000000000000000000", "00111000000000000000", "11100000000000000000", "11111100000000000000", "00011111100000000000", "11111111100000000000", "11111111111111111111", "11111111111111111100", "11111111111111111000", "11111111111111100000", "11111111111111111111", "11111111111111111111", "11111111111111111111", "00000000000000000000", "11111111111111111111", "11111111111111110000", "11111111111000000000", "11111111111111111111", "11111111111111111101", "11111111111110000000" }
Returns: 36
{"00111", "01010" }
Returns: 8
{"11101", "00111", "10101", "00000", "11000" }
Returns: 9
{"11000100010101111010", "00001100011011011110", "11011001001110101010", "00011011110000101100", "11110111001101110101", "11110010010101011011", "01110110010011100100", "00001101001100010110", "01100101110001111000", "00000010000111001101", "11010000011010101001", "11000101110001100000", "01000011100010100000", "10111001001001101110", "01110001010001100010", "00011100011011101001", "11001110111110100010", "10101111001000101000", "10110100111100101111", "00101100011100110100" }
Returns: 70
{"00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "10000", "10001", "10010", "10011", "10111", "11000", "11001", "11111" }
Returns: 5
{"10111111001000000001", "10000000000000000001", "10000000010000000001", "10110010000000000001", "10000000000000000001", "10001101000000000001", "10000000010000000001", "10000000000101100001", "10000000000000000001", "10000000001100000001", "10000000000000000001", "10000000000000000001", "10000000000001110001", "10000000000000000001", "10000000000000000001", "10000000010000001001", "10000000000000001001", "10000000000000000001", "10000000000000000011", "00000000000000000010" }
Returns: 32
{"0101010110", "1010100100", "0101001010", "1000001000" }
Returns: 21
{"11111111111111111111" }
Returns: 400
{"10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "10101010101010101010", "01010101010101010101" }
Returns: 200
{"1000", "1001", "1010", "0110", "0110", "1010", "1010", "0101", "1010", "0001", "1111", "1111", "0101", "1010", "1010", "0101", "1010", "0101" }
Returns: 4
{"00011", "11000", "01101", "10100", "10100" }
Returns: 7
{"00111000001010100111", "11101110001000001010", "11011100010000010101", "10111000100000101011", "01110001000001010111", "11100010000010101111", "11000100000101011111", "10001000001010111111", "00010000010101111111", "00100000101011111110", "01000001010111111100", "10000010101111111000", "00000101011111110001", "00001010111111100010", "00010101111111000100", "00101011111110001000", "01010111111100010000", "10101111111000100000", "01011111110001000001", "10111111100010000010" }
Returns: 100
{"00101100110101000101", "11111010101111000011", "10101010001011101010", "10000111111100001010", "11010101011111011010", "11000000000001111111", "00000000000000000000", "11110101011010101010", "10101000101010111101", "10101000000111110010", "00000000000011111111", "10101011110111010101", "10101010000010101000", "11111111111111000001", "00000000000001111110", "10100000111110100000", "11111111111111111111", "00000001111111000000", "11100000000011100000", "00000011111111100000" }
Returns: 56
{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1" }
Returns: 1
{"001100", "111100", "010011" }
Returns: 12
{"10000000000000000000", "10000000000000000001", "10000000000000000010", "10000000000000000100", "10000000000000010000", "10000000000000001000", "10000000000000100000", "10000000000010000000", "10000000001000000000", "10000000100000000000", "10001000000000000000", "10100000000000000000", "10000100000000000000", "11000000000000000000", "10000001000000000000", "10000000000100000000", "10000000001000000000" }
Returns: 16
{"01011001101100101000", "01011001101100101111", "00001001101100101000", "01011001101111101111", "01000001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101000", "01011001101100101111", "01011001101101101000" }
Returns: 62
{"0001", "0010", "1100", "0111" }
Returns: 4
{"00000", "00000" }
Returns: 0
{"100", "011", "110" }
Returns: 3
{"10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000", "10101010000000000000" }
Returns: 16
{"1111", "0101", "1011", "0001" }
Returns: 6
{"11111", "11110", "01111", "10111", "11011", "11101", "00001", "10000", "01000", "00100", "00010", "00001" }
Returns: 5
{"010", "101", "110", "111" }
Returns: 3
{"11100110101000101101", "01100011110001001101", "10010110000110010011", "11001110000100101100", "10001010001110110000", "11111111001110101001", "01010010001010010001", "00110111000110101010", "10010100111110000111", "10110011001111100001", "01011101000110101101", "11010101111010110111", "01001111100000101000", "11011111011110001110", "11000111100101010100", "01100010100001111010", "00111110000000001111", "01000011010001100000", "00101010000100000000", "00011111111001001000" }
Returns: 60
{"00011", "11100", "11011" }
Returns: 9
{"0011", "1100", "1110" }
Returns: 6
{"11111111111111101111", "11000000101100001011", "00100000001000011000", "00000000000011111101", "11101111001001000101", "00101001010100011110", "11101111110001011000", "11101110000001111101", "11100010000000111110", "11111111000100011000", "11100000100110111110", "11100000010001000110", "11100111111100111110", "11101111110100111110", "11101111000010111110", "11100011100001111110", "11100001000001111110", "11111111100001111101", "11110000000001111101", "11111111111111111111" }
Returns: 60
{"1110000000", "1110000001", "1111000001", "1111100001", "1111110001", "1111111001", "1111111111", "1111111101", "0001100000", "0000011000", "0000000110" }
Returns: 16
{"01100100000001010011", "01101101001101011000", "11001011111011000110", "01110011110010000100", "10001001011010010110", "10110110000100110000", "01011010011000110001", "10101000100101110100", "00110111101101001110", "01010100101110110001" }
Returns: 90