K-Distinct Groups - Google Top Interview Questions
Problem Statement :
You are given a list of integers counts where counts[i] represents the number of items that exist of type i. You are also given an integer k. Return the maximum number of groups of size k we can have given that each group must have items of distinct types. Constraints n ≤ 100,000 where n is the length of counts k ≤ n Example 1 Input counts = [3, 3, 2, 5] k = 2 Output 6 Explanation Let's name the four item types [a, b, c, d] respectively. We can have the following groups of two where all elements are distinct types: (a, d) (b, d) (a, b) (a, b) (c, d) (c, d) Example 2 Input counts = [3, 2, 4] k = 3 Output 2 Explanation Let's name the three item types [a, b, c] respectively. We can only have these groups of three where all elements are distinct types: (a, b, c) (a, b, c) We cannot use (a, c, c) since there are two cs.
Solution :
Solution in C++ :
bool can(vector<int>& v, int take, int k) {
long long total = 0;
for (int val : v) {
total += min(val, take);
}
return total / k >= take;
}
int solve(vector<int>& counts, int k) {
int lhs = 0;
int rhs = 2e9;
while (lhs < rhs) {
int mid = (lhs + rhs + 1) / 2;
if (can(counts, mid, k)) {
lhs = mid;
} else {
rhs = mid - 1;
}
}
return lhs;
}
Solution in Java :
import java.util.*;
class Solution {
// returns whether we can make num_groups of size k
public boolean possible(int[] count, int k, long num_groups) {
long required = num_groups * k;
for (int i = 0; i < count.length; i++) {
long use_this_much = Math.min(count[i], num_groups);
use_this_much = Math.min(use_this_much, required);
required -= use_this_much;
if (required == 0)
return true;
}
return false;
}
public int solve(int[] counts, int k) {
long l = 1;
long r = 0;
for (int c : counts) r += c;
int res = 0;
while (l <= r) {
long num_groups = l + (r - l) / 2;
if (possible(counts, k, num_groups)) {
res = (int) Math.max(res, num_groups);
l = num_groups + 1;
} else {
r = num_groups - 1;
}
}
return (int) res;
}
}
Solution in Python :
class Solution:
def possible(self, counts, groups, k):
required = groups * k
for i in range(len(counts)):
use_this_much = min(counts[i], groups, required)
required -= use_this_much
if required == 0:
return True
return False
def solve(self, counts, k):
res = 0
l = 0
r = sum(counts)
while l <= r:
m = l + (r - l) // 2
if self.possible(counts, m, k):
l = m + 1
res = max(res, m)
else:
r = m - 1
return res
View More Similar Problems
Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →