Akuna OA Coding

nbviewer is an open source project under the larger Project Jupyter. If you see warning message on phone, please change to Chrome browser / or view it on desktop.

Below code is an update for the chunk problem. The bin function returns the binary representation so we can count how many 1s are there and we know at least how many power of 2s are needed.

Another problem states that given two arrays, you could swap items between them no more than k operations. Return how many distinct items you could get in the first array at max.

def minChunks(totalPackets, uploadedChunks):
    if len(uploadedChunks) == 0:
        return bin(totalPackets).count("1")
    
    uploadedChunks.sort(key = lambda x: x[0])
    remainingChunk = []
    for i in range(1, len(uploadedChunks)):
        remainingChunk.append(uploadedChunks[i][0] - 1 - uploadedChunks[i-1][1])
    if uploadedChunks[0][0] != 1:
        remainingChunk.append(uploadedChunks[0][0] - 1)
    if uploadedChunks[-1][1] != totalPackets:
        remainingChunk.append(totalPackets - uploadedChunks[-1][1])
        
    minChunks = 0
    for i in remainingChunk:
        minChunks += bin(i).count("1")
        
    return minChunks
# minChunks(10, [[1, 2], [9, 10]])
# minChunks(18, [[9, 17]])
from collections import Counter

def maximumDistinct(a, b, k):
    n_redundant = sum([i-1 for i in Counter(a).values() if i >= 1])
    n_candidates = len(set(b) - set(a))
    return len(set(a)) + min((n_redundant, n_candidates, k))
# maximumDistinct([2, 2, 3, 4, 2], [1, 3, 2, 4, 1], 1)
Next