description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
N, skill_dict = len(req_skills), {}
for i in range(N):
skill_dict[req_skills[i]] = i
dp = [list(range(len(people))) for _ in range(1 << N)]
dp[0] = []
for i in range(len(people)):
skill = 0
for s in people[i]:
skill |= 1 << skill_dict[s]
for k, v in enumerate(dp):
new_skill = k | skill
if new_skill != k and len(v) + 1 < len(dp[new_skill]):
dp[new_skill] = v + [i]
return dp[(1 << N) - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
allneed = (1 << len(req_skills)) - 1
stoi = {}
for i, s in enumerate(req_skills):
stoi[s] = 1 << i
def serialize(arr):
res = 0
for s in arr:
res = res | stoi[s]
return res
ppl = []
for i, p in enumerate(people):
ppl.append(serialize(p))
tempmax = [0] * (len(req_skills) + 1)
@lru_cache(None)
def helper(needed):
if needed == 0:
return []
shortest = tempmax
for i, person in enumerate(ppl):
if person & needed > 0:
sofar = [i] + helper((person ^ allneed) & needed)
if len(sofar) < len(shortest):
shortest = sofar
return shortest
return helper(allneed) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN LIST ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
pmasks = []
for p in people:
mask = 0
for skill in p:
idx = req_skills.index(skill)
mask ^= 1 << idx
pmasks.append(mask)
M = pow(2, len(req_skills)) - 1
@functools.lru_cache(maxsize=None)
def dfs(mask):
if mask == M:
return []
ret = None
for i, p in enumerate(pmasks):
if mask | p != mask:
ppl = dfs(mask | p)
if ppl != None and (not ret or len(ret) > len(ppl) + 1):
ret = [i] + ppl
return ret
return dfs(0) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN LIST ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NONE VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(self, req_skills, people):
n, m = len(req_skills), len(people)
key = {v: i for i, v in enumerate(req_skills)}
dp = {(0): []}
for i, p in enumerate(people):
his_skill = 0
for skill in p:
his_skill |= 1 << key[skill]
if his_skill == 0 or his_skill in dp and len(dp[his_skill]) == 1:
continue
sn = copy.deepcopy(list(dp.items()))
for skill_set, need in sn:
with_him = skill_set | his_skill
if with_him == skill_set:
continue
if with_him not in dp or len(dp[with_him]) > len(need) + 1:
dp[with_him] = need + [i]
return dp[(1 << n) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
l = len(req_skills)
skill_to_idx = {s: i for i, s in enumerate(req_skills)}
@functools.lru_cache(None)
def dfs(cur_skill, idx):
if cur_skill == 2**l - 1:
return ()
if idx == len(people):
return None
cur = 0
for skill in people[idx]:
if skill in skill_to_idx and 1 << skill_to_idx[skill] & cur_skill == 0:
cur |= 1 << skill_to_idx[skill]
ret = dfs(cur_skill, idx + 1)
if cur != 0:
tmp = dfs(cur_skill | cur, idx + 1)
if tmp != None and ret != None:
ret = min(ret, tuple([idx, *tmp]), key=len)
elif tmp != None:
ret = tuple([idx, *tmp])
return ret
ret = dfs(0, 0)
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NONE VAR NONE ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR LIST VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, skills: List[str], people: List[List[str]]
) -> List[int]:
skill2idx = {skill: i for i, skill in enumerate(skills)}
for i in range(len(people)):
mask = 0
for skill in people[i]:
mask |= 1 << skill2idx[skill]
people[i] = mask
stop = (1 << len(skills)) - 1
@lru_cache(None)
def dfs(i, mask):
if mask == stop:
return ()
if i == len(people):
return None
ans1 = dfs(i + 1, mask)
ans2 = dfs(i + 1, mask | people[i])
if ans2 is not None:
ans2 = (i,) + ans2
if ans1 is None:
return ans2
elif ans2 is None:
return ans1
else:
return min(ans1, ans2, key=len)
return dfs(0, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN IF VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR NONE ASSIGN VAR BIN_OP VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n = len(req_skills)
target = (1 << n) - 1
dic = {x: index for index, x in enumerate(req_skills)}
skillsMap = []
for i in people:
mask = 0
for sk in i:
mask |= 1 << dic[sk]
skillsMap.append(mask)
dp = [float("inf")] * (1 << n)
par = [(0, 0)] * (1 << n)
dp[0] = 0
for i, k in enumerate(skillsMap):
if k != 0:
for j in range(target, -1, -1):
if dp[j] + 1 < dp[j | k]:
dp[j | k] = dp[j] + 1
par[j | k] = j, i
res = []
while target:
res.append(par[target][1])
target = par[target][0]
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
p_skills = [set() for _ in people]
for i, p in enumerate(people):
p_skills[i] = set(p)
people.sort(key=lambda x: -len(set(x) & set(req_skills)))
res = [0] * 17
def dfs(idx=0, path=[], has=set()):
nonlocal res
if idx == len(req_skills):
res = path
elif req_skills[idx] in has:
dfs(idx + 1, path, has)
elif len(path) + 1 < len(res):
for i, p in enumerate(people):
if req_skills[idx] in p_skills[i]:
intersection = has & p_skills[i]
has |= p_skills[i]
dfs(idx + 1, path + [i], has)
has -= {x for x in p_skills[i] if x not in intersection}
dfs()
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF NUMBER LIST FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
skill_id = {skill: i for i, skill in enumerate(req_skills)}
def getSkillSetId(skills):
res = 0
for skill in skills:
if skill in skill_id:
res |= 1 << skill_id[skill]
return res
req_skills = getSkillSetId(req_skills)
people = list(map(getSkillSetId, people))
@functools.lru_cache(None)
def dfs(req_skills, pid):
if req_skills == 0:
return [0, []]
elif pid == -1:
return [float("inf"), []]
else:
sol1 = dfs(req_skills, pid - 1)
sol2 = dfs(req_skills & ~people[pid], pid - 1)
if sol2[0] + 1 < sol1[0]:
return [sol2[0] + 1, sol2[1] + [pid]]
else:
return sol1
return dfs(req_skills, len(people) - 1)[1]
def smallestSufficientTeam_II(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
skill_id = {skill: idx for idx, skill in enumerate(req_skills)}
def getSkillId(skill_set):
res = 0
for skill in skill_set:
res = res | (1 << skill_id[skill] if skill in skill_id else 0)
return res
people = [getSkillId(skills) for skills in people]
req_skills = getSkillId(req_skills)
@functools.lru_cache(maxsize=None)
def dfs(req_skills, pid):
if req_skills == 0:
return [0, []]
elif pid == len(people):
return [float("inf"), []]
else:
sol1 = dfs(req_skills, pid + 1)
sol2 = dfs(req_skills & ~people[pid], pid + 1)
if sol2[0] + 1 < sol1[0]:
return [sol2[0] + 1, [pid] + sol2[1]]
else:
return sol1
return dfs(req_skills, 0)[1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER LIST IF VAR NUMBER RETURN LIST FUNC_CALL VAR STRING LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN LIST BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER LIST VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER LIST IF VAR FUNC_CALL VAR VAR RETURN LIST FUNC_CALL VAR STRING LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN LIST BIN_OP VAR NUMBER NUMBER BIN_OP LIST VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
skills = []
for p in people:
cur = 0
for skill in p:
for i, req_skill in enumerate(req_skills):
if skill == req_skill:
cur |= 1 << i
skills.append(cur)
n = len(req_skills)
target = (1 << n) - 1
dp = [(1000000) for i in range(target + 1)]
dp[0] = 0
parent = [0] * (target + 1)
for i in range(len(people)):
k = skills[i]
if k == 0:
continue
for j in reversed(list(range(target))):
if dp[j | k] > dp[j] + 1:
dp[j | k] = dp[j] + 1
parent[j | k] = j, i
t = target
result = []
while t:
result.append(parent[t][1])
t = parent[t][0]
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n = len(req_skills)
target = (1 << n) - 1
s_i = {s: i for i, s in enumerate(req_skills)}
dp = {(0): []}
for j, row in enumerate(people):
mask = 0
new_dp = dp.copy()
for s in row:
mask |= 1 << s_i[s]
for curr, cost in dp.items():
new_mask = curr | mask
if new_mask not in new_dp:
new_dp[new_mask] = cost + [j]
else:
new_dp[new_mask] = min(new_dp[new_mask], cost + [j], key=len)
dp = new_dp
return dp[target] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST VAR VAR ASSIGN VAR VAR RETURN VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n = len(req_skills)
res = {}
res[0] = set()
skill_map = {v: i for i, v in enumerate(req_skills)}
print(skill_map)
for p, skills in enumerate(people):
curr_skill = 0
for skill in skills:
curr_skill |= 1 << skill_map[skill]
nr = {}
for s, t in res.items():
k = curr_skill | s
nt = t | {p}
if k not in res:
if k not in nr or len(nr[k]) > len(nt):
nr[k] = nt
elif len(res[k]) > len(nt):
res[k] = nt
res.update(nr)
return list(res[(1 << n) - 1]) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
d = {}
for i, s in enumerate(req_skills):
d[s] = i
p = []
for s in people:
c = 0
for t in s:
c |= 1 << d[t]
p.append(c)
self.tar = 2 ** len(req_skills) - 1
self.r_len = len(people)
self.r = []
def dfs(l, t, cur):
if cur == self.tar and len(l) < self.r_len:
self.r_len = len(l)
self.r = l.copy()
return
elif len(l) >= self.r_len or t > len(req_skills):
return
if cur & 1 << t:
dfs(l, t + 1, cur)
for i, k in enumerate(p):
if k & 1 << t and not i in l:
tmp = cur
l.append(i)
cur |= k
dfs(l, t + 1, cur)
cur = tmp
l.pop()
dfs([], 0, 0)
return self.r | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
peopleLen = len(people)
skillsLen = len(req_skills)
maskFull = (1 << skillsLen) - 1
newPeople = []
for p in people:
newPeople.append(set(p))
dp = {}
def findPeople(m0, i0):
if m0 == maskFull:
return []
if i0 == peopleLen:
return None
if (m0, i0) in dp:
return dp[m0, i0]
nextM = m0
for j in range(skillsLen):
if m0 >> j & 1 == 1:
continue
if req_skills[j] in newPeople[i0]:
nextM |= 1 << j
ans = None
if nextM != m0:
subAns = findPeople(nextM, i0 + 1)
if subAns != None:
ans = subAns + [i0]
subAns = findPeople(m0, i0 + 1)
if ans == None or subAns != None and len(subAns) < len(ans):
ans = subAns
dp[m0, i0] = ans
return ans
return findPeople(0, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN LIST IF VAR VAR RETURN NONE IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NONE IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE VAR NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
N, M = len(req_skills), len(people)
NN = 1 << N
skill2id = {jj: ii for ii, jj in enumerate(req_skills)}
team = {ii: [] for ii in range(NN)}
dp = [-1] * NN
dp[0] = 0
for ii in range(M):
idx = 0
for s in people[ii]:
if s in skill2id:
idx = idx | 1 << skill2id[s]
for jj in range(NN):
if dp[jj] < 0:
continue
x = jj | idx
if dp[x] == -1 or dp[x] > dp[jj] + 1:
dp[x] = dp[jj] + 1
team[x] = team[jj].copy()
team[x].append(ii)
return team[NN - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
dp = {(0): []}
skill_to_index = {skill: i for i, skill in enumerate(req_skills)}
for i, skills in enumerate(people):
mask = 0
for skill in skills:
mask |= 1 << skill_to_index[skill]
keys = list(dp.keys())
for filled_req in keys:
new_filled_req = filled_req | mask
if (
new_filled_req not in dp
or len(dp[new_filled_req]) > len(dp[filled_req]) + 1
):
dp[new_filled_req] = dp[filled_req] + [i]
return dp[(1 << len(req_skills)) - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT NUMBER LIST ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n, m = len(req_skills), len(people)
key = {v: i for i, v in enumerate(req_skills)}
dp = [[]] + [None] * ((1 << n) - 1)
for i, p in enumerate(people):
his_skill = 0
for skill in p:
if skill in key:
his_skill |= 1 << key[skill]
for k in range(len(dp) - 1, -1, -1):
with_him = k | his_skill
if with_him == k or dp[k] is None:
continue
if not dp[with_him] or len(dp[k]) + 1 < len(dp[with_him]):
dp[with_him] = dp[k] + [i]
return dp[(1 << n) - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST BIN_OP LIST NONE BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NONE IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
def convertToNum(people: List[str]) -> int:
ans = 0
for skill in people:
ans |= 1 << dic[skill]
return ans
dic = {v: i for i, v in enumerate(req_skills)}
target = (1 << len(req_skills)) - 1
dp = {v: [] for v in range(1, target + 1)}
for i, people_skill in enumerate(people):
skill = convertToNum(people_skill)
dp[skill] = [i]
for key in list(dp.keys()):
if key != skill and len(dp[key]) > 0 and i not in dp[key]:
new_skill = key | skill
if len(dp[new_skill]) > len(dp[key]) + 1 or len(dp[new_skill]) == 0:
temp = [v for v in dp[key]]
temp.append(i)
dp[new_skill] = temp
return dp[target] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n = len(req_skills)
target = (1 << n) - 1
skills = []
for p in people:
mask = 0
for s in p:
mask |= 1 << req_skills.index(s)
skills.append(mask)
dp = [float("inf") for _ in range(1 << n)]
pt = [(0) for _ in range(1 << n)]
dp[0] = 0
for i in range(len(people)):
k = skills[i]
if k == 0:
continue
for j in range(target + 1)[::-1]:
if dp[j] + 1 < dp[j | k]:
dp[j | k] = dp[j] + 1
pt[j | k] = j, i
t = target
ans = []
while t:
ans.append(pt[t][1])
t = pt[t][0]
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
m = {val: i for i, val in enumerate(req_skills)}
n = len(req_skills)
skills = [
reduce(lambda a, b: a + b, [(1 << m[s]) for s in row] or [0])
for row in people
]
final_state = reduce(lambda a, b: a + b, [(1 << i) for i in range(n)])
dp = {(0): []}
for i, skill in enumerate(skills):
for prev_state, team in dp.copy().items():
new_state = prev_state | skill
if new_state == prev_state:
continue
if new_state not in dp or len(team) + 1 < len(dp[new_state]):
dp[new_state] = team + [i]
return dp[final_state] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR LIST NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
s2i = {}
for i, skill in enumerate(req_skills):
s2i[skill] = i
n = len(people)
p2n = [0] * n
for i, p in enumerate(people):
temp = 0
for skill in p:
temp |= 1 << s2i[skill]
p2n[i] = temp
goal = (1 << len(req_skills)) - 1
dp = [sys.maxsize] * (goal + 1)
dp[0] = 0
saves = [[] for i in range(goal + 1)]
for i in range(n):
skill = p2n[i]
for skillset in range(goal + 1):
new_skill = skillset | skill
if dp[new_skill] > dp[skillset] + 1:
dp[new_skill] = dp[skillset] + 1
saves[new_skill] = list(saves[skillset])
saves[new_skill].append(i)
return saves[goal] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
dic = {req_skills[rs]: (2**rs) for rs in range(len(req_skills))}
dp = [[] for i in range(2 ** len(req_skills))]
m_pp = []
for pp in range(len(people)):
res = 0
for itr_pp in people[pp]:
res += dic[itr_pp]
if res != 0:
dp[res] = [pp]
m_pp.append(res)
for i in range(len(m_pp)):
if m_pp[i] == 0:
continue
for j in range(len(dp)):
if len(dp[j]) > 0:
sum_ = j | m_pp[i]
if len(dp[sum_]) == 0 or len(dp[sum_]) > len(dp[j] + [i]):
dp[sum_] = dp[j] + [i]
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR LIST VAR ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR RETURN VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def rec(self, rs, rdic, people, ret, rett, unis):
if len(set(unis)) == len(rs):
if len(ret) < len(rett):
return ret
if len(ret) >= len(rett):
return rett
for r, v in list(rs.items()):
if r not in set(unis):
for p in rdic[r]:
rett = self.rec(rs, rdic, people, ret + [p], rett, unis + people[p])
break
return rett
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
rdic = {}
rs = {}
for r in req_skills:
rdic[r] = []
rs[r] = 0
for i in range(len(people)):
for r in range(len(people[i])):
rdic[people[i][r]].append(i)
rett = [i for i in range(len(people))]
return self.rec(rs, rdic, people, [], rett, []) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR LIST VAR LIST VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
skill_idx = {s: i for i, s in enumerate(req_skills)}
n = len(req_skills)
def encode(p):
res = 0
for sk in p:
res |= 1 << skill_idx[sk]
return res
people_code = [encode(p) for p in people]
target = (1 << n) - 1
people_arr = 0
m = len(people)
covers = {(0): []}
for i, pcode in enumerate(people_code):
for code, people_arr in list(covers.copy().items()):
new_code = pcode | code
if (
new_code == code
or new_code in covers
and len(people_arr) >= len(covers[new_code])
):
continue
covers[new_code] = people_arr + [i]
return covers[target] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
n = len(req_skills)
skill_to_idx = {s: idx for idx, s in enumerate(req_skills)}
skills = []
for p in people:
mask = 0
for s in p:
mask |= 1 << skill_to_idx[s]
skills.append(mask)
dp = {(0): []}
for i in range(len(people)):
tmp = dp.copy()
for s, p in list(dp.items()):
ns = s | skills[i]
if ns not in tmp or len(p) + 1 < len(tmp[ns]):
tmp[ns] = p + [i]
dp = tmp
return dp[(1 << n) - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR ASSIGN VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(
self, req_skills: List[str], people: List[List[str]]
) -> List[int]:
p_skills = [set() for _ in people]
for i, p in enumerate(people):
p_skills[i] = set(p)
res = [0] * 17
def dfs(idx=0, path=[], has=set()):
nonlocal res
if idx == len(req_skills):
res = path
elif req_skills[idx] in has:
dfs(idx + 1, path, has)
elif len(path) + 1 < len(res):
for i, p in enumerate(people):
if req_skills[idx] in p_skills[i]:
dfs(idx + 1, path + [i], has | p_skills[i])
dfs()
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF NUMBER LIST FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR |
In a project, you have a list of required skills req_skills, and a list of people. The i-th person people[i] contains a list of skills that person has.
Consider a sufficient team: a set of people such that for every required skill in req_skills, there is at least one person in the team who has that skill. We can represent these teams by the index of each person: for example, team = [0, 1, 3] represents the people with skills people[0], people[1], and people[3].
Return any sufficient team of the smallest possible size, represented by the index of each person.
You may return the answer in any order. It is guaranteed an answer exists.
Example 1:
Input: req_skills = ["java","nodejs","reactjs"], people = [["java"],["nodejs"],["nodejs","reactjs"]]
Output: [0,2]
Example 2:
Input: req_skills = ["algorithms","math","java","reactjs","csharp","aws"], people = [["algorithms","math","java"],["algorithms","math","reactjs"],["java","csharp","aws"],["reactjs","csharp"],["csharp","math"],["aws","java"]]
Output: [1,2]
Constraints:
1 <= req_skills.length <= 16
1 <= people.length <= 60
1 <= people[i].length, req_skills[i].length, people[i][j].length <= 16
Elements of req_skills and people[i] are (respectively) distinct.
req_skills[i][j], people[i][j][k] are lowercase English letters.
Every skill in people[i] is a skill in req_skills.
It is guaranteed a sufficient team exists. | class Solution:
def smallestSufficientTeam(self, req_skills, people):
countReqSkills, memo = len(req_skills), {(0): []}
skillToIndex = {v: i for i, v in enumerate(req_skills)}
for i, ithSkills in enumerate(people):
ithSkillsBitmap = 0
for skill in ithSkills:
ithSkillsBitmap |= 1 << skillToIndex[skill]
for skillsBitmap, reqPpl in list(memo.items()):
skillsBitmapAddI = skillsBitmap | ithSkillsBitmap
if skillsBitmapAddI == skillsBitmap:
continue
if skillsBitmapAddI not in memo or len(reqPpl) + 1 < len(
memo[skillsBitmapAddI]
):
memo[skillsBitmapAddI] = reqPpl + [i]
return memo[(1 << countReqSkills) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR DICT NUMBER LIST ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR LIST VAR RETURN VAR BIN_OP BIN_OP NUMBER VAR NUMBER |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | import sys
input = sys.stdin.readline
def fmax(a):
if len(a) <= 2:
return len(a)
l = []
i = 1
cur = -1
ind = 0
if a[0] == 0:
l.append([0])
ind += 1
cur = 0
while ind < len(a):
if a[ind] < i:
l[cur].append(a[ind] - i // 2)
else:
cur += 1
while a[ind] >= i:
i *= 2
l.append([a[ind] - i // 2])
ind += 1
if cur == 0:
return fmax(l[0])
mx = 0
m = len(l)
for i in range(m):
mx = max(mx, fmax(l[i]) + m - i)
if i == 0:
mx -= 1
return mx
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(n - fmax(a)) | IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | see = [1]
for i in range(41):
see.append(see[-1] * 2)
def func(arr, num):
if num == 0:
return len(arr)
if len(arr) == 1:
return 1
arr1, arr2 = [], []
for i in arr:
if i ^ see[num] == i - see[num]:
arr2.append(i)
else:
arr1.append(i)
if len(arr1) == 0 or len(arr2) == 0:
return func(arr, num - 1)
return 1 + max(func(arr1, num - 1), func(arr2, num - 1))
def solve():
n = int(input())
arr = [int(x) for x in input().split()]
print(len(arr) - func(arr, 41))
t = 1
while t > 0:
solve()
t = t - 1 | ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | n = int(input())
a = list(map(int, input().split()))
def rec(s, i):
if len(s) in (0, 1, 2):
return len(s)
mask = 1 << i
a = []
b = []
for one in s:
if one & mask:
a.append(one)
else:
b.append(one)
if not a or not b:
return rec(s, i - 1)
return 1 + max(rec(a, i - 1), rec(b, i - 1))
print(len(a) - rec(a, 31)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | n = int(input())
l = list(map(int, input().split()))
def fun(a):
if len(a) <= 3:
return len(a)
maxE = max(a)
if maxE == 0:
return len(a)
msb = 1
while 2 * msb <= maxE:
msb *= 2
l1 = []
l2 = []
for x in a:
if x >= msb:
l1.append(x - msb)
else:
l2.append(x)
max1 = fun(l1)
max2 = fun(l2)
if max1 == 0:
return max2
if max2 == 0:
return max1
return max(1 + max1, 1 + max2)
print(n - fun(l)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
dp = {a[i]: (1) for i in range(n)}
for i in range(30):
next = {}
for val in dp:
if not val >> 1 in next:
next[val >> 1] = -dp[val]
elif next[val >> 1] < 0:
next[val >> 1] = max(-next[val >> 1], dp[val])
else:
next[val >> 1] = max(next[val >> 1], abs(dp[val]))
dp = next
for d in dp:
if dp[d] > 0:
dp[d] = dp[d] + 1
else:
dp[d] = -dp[d]
print(n - dp[0]) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | import sys
from sys import stdin
def solve(a):
if len(a) <= 1:
return len(a)
lis = [[]]
for i in range(len(a)):
if i == 0 or a[i - 1].bit_length() == a[i].bit_length():
if a[i] != 0:
lis[-1].append(a[i] ^ 1 << a[i].bit_length() - 1)
else:
lis[-1].append(0)
else:
lis.append([])
if a[i] != 0:
lis[-1].append(a[i] ^ 1 << a[i].bit_length() - 1)
else:
lis[-1].append(0)
ans = 0
tmp = 0
for i in range(len(lis)):
if i == 0:
ans = max(ans, len(lis) - 1 + solve(lis[0]))
else:
ans = max(ans, len(lis) - i + solve(lis[i]))
return ans
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
a.sort()
print(n - solve(a)) | IMPORT FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | def main():
n = int(input())
a = [int(word) for word in input().rstrip().split()]
x = max(a)
arr = {}
for num in a:
arr[num] = 1
ans = 0
while True:
x >>= 1
dp = {}
for i in arr:
p = i >> 1
if p not in dp:
dp[p] = arr[i]
else:
dp[p] = max(dp[p] + 1, arr[i] + 1)
ans = max(ans, dp[p])
arr = dp
if x == 0:
break
print(n - ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**4)
n = int(input())
A = list(map(int, input().split()))
def calc(A):
if len(A) == 1:
return 1
C = [[] for i in range(32)]
for a in A:
if a == 0:
C[0].append(0)
else:
k = a.bit_length()
C[a.bit_length()].append(a - (1 << k - 1))
S = [0] * 32
for i in range(30, -1, -1):
if len(C[i]) != 0:
S[i] = S[i + 1] + 1
else:
S[i] = S[i + 1]
flag = 0
ANS = 0
for i in range(32):
if C[i] == []:
continue
elif flag == 0:
ANS = max(ANS, S[i + 1] + calc(C[i]))
flag = 1
else:
ANS = max(ANS, S[i + 1] + calc(C[i]) + 1)
return ANS
print(n - calc(A)) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
For a given sequence of distinct non-negative integers $(b_1, b_2, \dots, b_k)$ we determine if it is good in the following way:
Consider a graph on $k$ nodes, with numbers from $b_1$ to $b_k$ written on them.
For every $i$ from $1$ to $k$: find such $j$ ($1 \le j \le k$, $j\neq i$), for which $(b_i \oplus b_j)$ is the smallest among all such $j$, where $\oplus$ denotes the operation of bitwise XOR ( https://en.wikipedia.org/wiki/Bitwise_operation#XOR ). Next, draw an undirected edge between vertices with numbers $b_i$ and $b_j$ in this graph.
We say that the sequence is good if and only if the resulting graph forms a tree (is connected and doesn't have any simple cycles).
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once.
You can find an example below (the picture corresponding to the first test case).
Sequence $(0, 1, 5, 2, 6)$ is not good as we cannot reach $1$ from $5$.
However, sequence $(0, 1, 5, 2)$ is good.
You are given a sequence $(a_1, a_2, \dots, a_n)$ of distinct non-negative integers. You would like to remove some of the elements (possibly none) to make the remaining sequence good. What is the minimum possible number of removals required to achieve this goal?
It can be shown that for any sequence, we can remove some number of elements, leaving at least $2$, so that the remaining sequence is good.
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 200,000$) — length of the sequence.
The second line contains $n$ distinct non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the elements of the sequence.
-----Output-----
You should output exactly one integer — the minimum possible number of elements to remove in order to make the remaining sequence good.
-----Examples-----
Input
5
0 1 5 2 6
Output
1
Input
7
6 9 8 7 3 5 2
Output
2
-----Note-----
Note that numbers which you remove don't impact the procedure of telling whether the resulting sequence is good.
It is possible that for some numbers $b_i$ and $b_j$, you will try to add the edge between them twice. Nevertheless, you will add this edge only once. | import sys
input = sys.stdin.buffer.readline
def largest_good(a):
if len(a) <= 3:
return len(a)
mx = max(a)
cutoff = 1
while 2 * cutoff <= mx:
cutoff *= 2
s0 = []
s1 = []
for i in a:
if i < cutoff:
s0.append(i)
else:
s1.append(i)
if s0 and s1:
return 1 + max(largest_good(s0), largest_good(s1))
else:
s1 = [(i - cutoff) for i in s1]
return largest_good(s1)
def prog():
n = int(input())
a = list(map(int, input().split()))
print(n - largest_good(a))
prog() | IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, N, A, X):
prefix = 0
ans = N
for i in range(30, -1, -1):
if X >> i & 1 != 0:
prefix ^= 1 << i
continue
ct = 0
p = prefix ^ 1 << i
for j in range(N):
if A[j] & p == p:
ct += 1
ans = min(ans, N - ct)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, N, A, X):
res = N
bitsetinX = 0
for i in range(31, -1, -1):
if X & 1 << i:
bitsetinX |= 1 << i
else:
mayberes = bitsetinX | 1 << i
noneedtochange = 0
for i in A:
if i & mayberes == mayberes:
noneedtochange += 1
res = min(res, N - noneedtochange)
return res
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N, X = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
ob = Solution()
ans = ob.count(N, A, X)
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, N, A, X):
m = A[0]
for i in range(1, N):
m &= A[i]
ans = N
for i in range(32):
if 1 << i | m > X:
c = 0
for x in A:
if x & 1 << i == 0:
c += 1
ans = min(ans, c)
s = set()
for i in range(31, -1, -1):
if 1 << i & X != 0:
for j, x in enumerate(A):
if 1 << i & x == 0:
s.add(j)
else:
t = set()
for j, x in enumerate(A):
if 1 << i & x == 0:
t.add(j)
ans = min(ans, len(s | t))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, n, l, x):
def fun(t):
c = 0
for i in l:
if t & i != t:
c += 1
return c
maxv = max(l)
ans = n
for i in range(maxv + 1):
a = 1 << i
t = x + a - x % a
if t > maxv:
break
ans = min(ans, fun(t))
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, N, A, X):
res, bitset = N, 0
for i in range(32, -1, -1):
if X & 1 << i > 0:
bitset |= 1 << i
else:
count = 0
temp = bitset | 1 << i
for num in A:
if num & temp != temp:
count += 1
res = min(res, count)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array A[ ] of N integers and an integer X. In one operation, you can change the i^{th} element of the array to any integer value where 1 ≤ i ≤ N. Calculate minimum number of such operations required such that the bitwise AND of all the elements of the array is strictly greater than X.
Example 1:
Input:
N = 4, X = 2
A[] = {3, 1, 2, 7}
Output:
2
Explanation:
After performing two operations:
Modified array: {3, 3, 11, 7}
Now, Bitwise AND of all the elements
is 3 & 3 & 11 & 7 = 3
Example 2:
Input:
N = 3, X = 1
A[] = {2, 2, 2}
Output:
0
Your Task:
You don't need to read input or print anything. Your task is to complete the function count( ) which takes N, A[ ] and X as input parameters and returns the minimum number of operations required.
Expected Time Complexity: O(N * Log(max(A[ ])))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[i] ≤ 10^{9}
1 ≤ X ≤ 10^{9} | class Solution:
def count(self, N, A, X):
ans = 10**9 + 9
cur_xor = 0
for i in range(31, -1, -1):
if X & 1 << i:
cur_xor = cur_xor | 1 << i
else:
temp = cur_xor | 1 << i
cnt = 0
for i in range(0, N):
if temp & A[i] == temp:
cnt = cnt + 1
ans = min(ans, N - cnt)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR |
Chef has a tree consisting of N nodes, rooted at node 1. Every node of this tree can be assigned a binary value (0 or 1). Initially, every node is assigned 0.
We define AND-value of a set of nodes as the [bitwise AND] of all the values of the nodes in it.
An edge in the tree is called good, if after removing this edge, the two trees obtained have the same AND-value.
You have to process Q queries. Each query is one of the following types :
1 u : If node u is a leaf, change its value to 1, else, change its value to the AND-value of its *descendants*.
2 : Find the total number of *good* edges in the tree.
Note: *Descendants* of some node X, are all the nodes in its [subtree],subset%20of%20the%20larger%20tree.) except X.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer N — the number of nodes in the tree.
- The next N-1 lines describe the edges. The i^{th} line contains two space-separated integers u_{i} and v_{i}, denoting an edge between u_{i} and v_{i}.
- The next line contains a single integer Q — the number of queries.
- Then, Q lines follow. Each of these lines describes a query in the format described above.
------ Output Format ------
For each query of the second type, print a single line containing one integer — the number of good edges in the tree.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$1 ≤ Q ≤ 3 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$ and $u_{i} \neq v_{i}$.
- The sum of $N$ and $Q$ over all test cases won't exceed $3 \cdot 10^{5}$.
- Atleast one query of type $2$ is present.
----- Sample Input 1 ------
1
5
1 2
1 3
2 4
2 5
5
1 1
1 3
2
1 5
2
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Query $1$: Node $1$ is not a leaf node. Thus, we change its value to the AND-value of its *descendants*. Descendants of node $1$ are $\{2, 3, 4, 5\}$. All these nodes have value $0$. Thus, the bitwise AND of all values is also $0$. Hence, node $1$ is assigned $0$.
Query $2$: Node $3$ is assigned $1$ since it is a leaf node.
Query $3$: Edges between nodes $(1, 2), (2, 4),$ and $(2, 5)$ are good edges.
- Edge $(1, 2)$: On removing the edge the set of nodes in the two trees are $\{1, 3\}$ and $\{2, 4, 5\}$. The AND-values of the respective set of nodes is $0$ $\&$ $1 = 0$ and $0$ $\&$ $0$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 4)$: On removing the edge the set of nodes in the two trees are $\{4\}$ and $\{1, 2, 3, 5\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 5)$: On removing the edge the set of nodes in the two trees are $\{5\}$ and $\{1, 2, 3, 4\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
Query $4$: Node $5$ is assigned $1$ since it is a leaf node.
Query $5$: Edges between nodes $(1, 2)$ and $(2, 4)$ are good edges. | def dfs(curr, adj, par, child):
for elem in adj[curr]:
if par[elem] != -1:
continue
par[elem] = curr
child[curr].append(elem)
dfs(elem, adj, par, child)
for i in range(int(input())):
n = int(input())
val = [0] * n
adj = []
child = []
for j in range(n):
adj.append([])
child.append([])
for j in range(n - 1):
s, d = map(int, input().split())
adj[s - 1].append(d - 1)
adj[d - 1].append(s - 1)
good = n - 1
par = [-2] + [-1] * (n - 1)
dfs(0, adj, par, child)
for q in range(int(input())):
ask = list(map(int, input().split()))
if ask[0] == 2:
print(good % n)
else:
node = ask[1] - 1
if val[node] == 1:
continue
if len(child[node]) == 0:
val[node] = 1
good -= 1
else:
change = True
for elem in child[node]:
if val[elem] == 0:
change = False
break
if change:
good -= 1
val[node] = 1 | FUNC_DEF FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER |
Chef has a tree consisting of N nodes, rooted at node 1. Every node of this tree can be assigned a binary value (0 or 1). Initially, every node is assigned 0.
We define AND-value of a set of nodes as the [bitwise AND] of all the values of the nodes in it.
An edge in the tree is called good, if after removing this edge, the two trees obtained have the same AND-value.
You have to process Q queries. Each query is one of the following types :
1 u : If node u is a leaf, change its value to 1, else, change its value to the AND-value of its *descendants*.
2 : Find the total number of *good* edges in the tree.
Note: *Descendants* of some node X, are all the nodes in its [subtree],subset%20of%20the%20larger%20tree.) except X.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer N — the number of nodes in the tree.
- The next N-1 lines describe the edges. The i^{th} line contains two space-separated integers u_{i} and v_{i}, denoting an edge between u_{i} and v_{i}.
- The next line contains a single integer Q — the number of queries.
- Then, Q lines follow. Each of these lines describes a query in the format described above.
------ Output Format ------
For each query of the second type, print a single line containing one integer — the number of good edges in the tree.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$1 ≤ Q ≤ 3 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$ and $u_{i} \neq v_{i}$.
- The sum of $N$ and $Q$ over all test cases won't exceed $3 \cdot 10^{5}$.
- Atleast one query of type $2$ is present.
----- Sample Input 1 ------
1
5
1 2
1 3
2 4
2 5
5
1 1
1 3
2
1 5
2
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Query $1$: Node $1$ is not a leaf node. Thus, we change its value to the AND-value of its *descendants*. Descendants of node $1$ are $\{2, 3, 4, 5\}$. All these nodes have value $0$. Thus, the bitwise AND of all values is also $0$. Hence, node $1$ is assigned $0$.
Query $2$: Node $3$ is assigned $1$ since it is a leaf node.
Query $3$: Edges between nodes $(1, 2), (2, 4),$ and $(2, 5)$ are good edges.
- Edge $(1, 2)$: On removing the edge the set of nodes in the two trees are $\{1, 3\}$ and $\{2, 4, 5\}$. The AND-values of the respective set of nodes is $0$ $\&$ $1 = 0$ and $0$ $\&$ $0$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 4)$: On removing the edge the set of nodes in the two trees are $\{4\}$ and $\{1, 2, 3, 5\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 5)$: On removing the edge the set of nodes in the two trees are $\{5\}$ and $\{1, 2, 3, 4\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
Query $4$: Node $5$ is assigned $1$ since it is a leaf node.
Query $5$: Edges between nodes $(1, 2)$ and $(2, 4)$ are good edges. | def dfs(curr, ar, ct, tree):
count = 0
for i in ar[curr]:
if tree[curr] == i:
continue
tree[i] = curr
count += dfs(i, ar, ct, tree)
ct[curr] = count
return 1
t = int(input())
for _ in range(t):
n = int(input())
l = [[] for _ in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
l[u].append(v)
l[v].append(u)
ct = [(0) for _ in range(n + 1)]
tree = [(0) for _ in range(n + 1)]
dfs(1, l, ct, tree)
q = int(input())
g_ct = n - 1
one = [False] * (n + 1)
for _ in range(q):
ar = list(map(int, input().split()))
if ar[0] == 1:
curr = ar[1]
if ct[curr] == 0 and not one[curr]:
ct[tree[curr]] -= 1
g_ct -= 1
one[curr] = True
else:
print(n - 1 if one[1] else g_ct) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Chef has a tree consisting of N nodes, rooted at node 1. Every node of this tree can be assigned a binary value (0 or 1). Initially, every node is assigned 0.
We define AND-value of a set of nodes as the [bitwise AND] of all the values of the nodes in it.
An edge in the tree is called good, if after removing this edge, the two trees obtained have the same AND-value.
You have to process Q queries. Each query is one of the following types :
1 u : If node u is a leaf, change its value to 1, else, change its value to the AND-value of its *descendants*.
2 : Find the total number of *good* edges in the tree.
Note: *Descendants* of some node X, are all the nodes in its [subtree],subset%20of%20the%20larger%20tree.) except X.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer N — the number of nodes in the tree.
- The next N-1 lines describe the edges. The i^{th} line contains two space-separated integers u_{i} and v_{i}, denoting an edge between u_{i} and v_{i}.
- The next line contains a single integer Q — the number of queries.
- Then, Q lines follow. Each of these lines describes a query in the format described above.
------ Output Format ------
For each query of the second type, print a single line containing one integer — the number of good edges in the tree.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$1 ≤ Q ≤ 3 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$ and $u_{i} \neq v_{i}$.
- The sum of $N$ and $Q$ over all test cases won't exceed $3 \cdot 10^{5}$.
- Atleast one query of type $2$ is present.
----- Sample Input 1 ------
1
5
1 2
1 3
2 4
2 5
5
1 1
1 3
2
1 5
2
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Query $1$: Node $1$ is not a leaf node. Thus, we change its value to the AND-value of its *descendants*. Descendants of node $1$ are $\{2, 3, 4, 5\}$. All these nodes have value $0$. Thus, the bitwise AND of all values is also $0$. Hence, node $1$ is assigned $0$.
Query $2$: Node $3$ is assigned $1$ since it is a leaf node.
Query $3$: Edges between nodes $(1, 2), (2, 4),$ and $(2, 5)$ are good edges.
- Edge $(1, 2)$: On removing the edge the set of nodes in the two trees are $\{1, 3\}$ and $\{2, 4, 5\}$. The AND-values of the respective set of nodes is $0$ $\&$ $1 = 0$ and $0$ $\&$ $0$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 4)$: On removing the edge the set of nodes in the two trees are $\{4\}$ and $\{1, 2, 3, 5\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 5)$: On removing the edge the set of nodes in the two trees are $\{5\}$ and $\{1, 2, 3, 4\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
Query $4$: Node $5$ is assigned $1$ since it is a leaf node.
Query $5$: Edges between nodes $(1, 2)$ and $(2, 4)$ are good edges. | def order(parent, children, thisdict, visited, element, par):
if visited[element] == True:
return
visited[element] = True
parent[element] = par
if par != -1:
children[par].append(element)
for i in thisdict[element]:
if visited[i] == False:
order(parent, children, thisdict, visited, i, element)
for i in range(int(input())):
N = int(input())
thisdict = {}
value = [(0) for _ in range(N)]
parent = [(-1) for i in range(N)]
children = [[] for i in range(N)]
visited = [(False) for i in range(N)]
for i in range(N - 1):
u, v = input().split()
u = int(u) - 1
v = int(v) - 1
if u in thisdict:
thisdict[u].append(v)
else:
thisdict[u] = [v]
if v in thisdict:
thisdict[v].append(u)
else:
thisdict[v] = [u]
order(parent, children, thisdict, visited, 0, -1)
Q = int(input())
ones = 0
for i in range(Q):
x = input().split()
type = int(x[0])
if type == 1:
index = int(x[1])
index = index - 1
if value[index] == 1:
continue
if children[index] == []:
value[index] = 1
ones += 1
else:
lol = 1
for j in children[index]:
if value[j] == 0:
lol = 0
break
lol = lol & value[j]
if lol == 1:
value[index] = 1
ones += 1
elif ones == N:
print(N - 1)
else:
print(N - 1 - ones) | FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR LIST ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR |
Chef has a tree consisting of N nodes, rooted at node 1. Every node of this tree can be assigned a binary value (0 or 1). Initially, every node is assigned 0.
We define AND-value of a set of nodes as the [bitwise AND] of all the values of the nodes in it.
An edge in the tree is called good, if after removing this edge, the two trees obtained have the same AND-value.
You have to process Q queries. Each query is one of the following types :
1 u : If node u is a leaf, change its value to 1, else, change its value to the AND-value of its *descendants*.
2 : Find the total number of *good* edges in the tree.
Note: *Descendants* of some node X, are all the nodes in its [subtree],subset%20of%20the%20larger%20tree.) except X.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer N — the number of nodes in the tree.
- The next N-1 lines describe the edges. The i^{th} line contains two space-separated integers u_{i} and v_{i}, denoting an edge between u_{i} and v_{i}.
- The next line contains a single integer Q — the number of queries.
- Then, Q lines follow. Each of these lines describes a query in the format described above.
------ Output Format ------
For each query of the second type, print a single line containing one integer — the number of good edges in the tree.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$1 ≤ Q ≤ 3 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$ and $u_{i} \neq v_{i}$.
- The sum of $N$ and $Q$ over all test cases won't exceed $3 \cdot 10^{5}$.
- Atleast one query of type $2$ is present.
----- Sample Input 1 ------
1
5
1 2
1 3
2 4
2 5
5
1 1
1 3
2
1 5
2
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Query $1$: Node $1$ is not a leaf node. Thus, we change its value to the AND-value of its *descendants*. Descendants of node $1$ are $\{2, 3, 4, 5\}$. All these nodes have value $0$. Thus, the bitwise AND of all values is also $0$. Hence, node $1$ is assigned $0$.
Query $2$: Node $3$ is assigned $1$ since it is a leaf node.
Query $3$: Edges between nodes $(1, 2), (2, 4),$ and $(2, 5)$ are good edges.
- Edge $(1, 2)$: On removing the edge the set of nodes in the two trees are $\{1, 3\}$ and $\{2, 4, 5\}$. The AND-values of the respective set of nodes is $0$ $\&$ $1 = 0$ and $0$ $\&$ $0$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 4)$: On removing the edge the set of nodes in the two trees are $\{4\}$ and $\{1, 2, 3, 5\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 5)$: On removing the edge the set of nodes in the two trees are $\{5\}$ and $\{1, 2, 3, 4\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
Query $4$: Node $5$ is assigned $1$ since it is a leaf node.
Query $5$: Edges between nodes $(1, 2)$ and $(2, 4)$ are good edges. | for _ in range(int(input())):
n = int(input())
graph = [dict() for _ in range(n + 1)]
cnt = [0] * (n + 1)
cnt[1] = 1
graph[0][1] = True
graph[1][0] = True
for i in range(n - 1):
u, v = map(int, input().split())
cnt[u] += 1
cnt[v] += 1
graph[u][v] = True
graph[v][u] = True
ans = n - 1
for _ in range(int(input())):
inp = [int(x) for x in input().split()]
if len(inp) > 1:
node = inp[1]
if cnt[node] > 0:
if cnt[node] == 1:
pnode = None
for i in graph[node].keys():
pnode = i
graph[pnode].pop(node)
cnt[node] -= 1
cnt[pnode] -= 1
if node == 1:
ans = n - 1
else:
ans -= 1
else:
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef has a tree consisting of N nodes, rooted at node 1. Every node of this tree can be assigned a binary value (0 or 1). Initially, every node is assigned 0.
We define AND-value of a set of nodes as the [bitwise AND] of all the values of the nodes in it.
An edge in the tree is called good, if after removing this edge, the two trees obtained have the same AND-value.
You have to process Q queries. Each query is one of the following types :
1 u : If node u is a leaf, change its value to 1, else, change its value to the AND-value of its *descendants*.
2 : Find the total number of *good* edges in the tree.
Note: *Descendants* of some node X, are all the nodes in its [subtree],subset%20of%20the%20larger%20tree.) except X.
------ Input Format ------
- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case contains a single integer N — the number of nodes in the tree.
- The next N-1 lines describe the edges. The i^{th} line contains two space-separated integers u_{i} and v_{i}, denoting an edge between u_{i} and v_{i}.
- The next line contains a single integer Q — the number of queries.
- Then, Q lines follow. Each of these lines describes a query in the format described above.
------ Output Format ------
For each query of the second type, print a single line containing one integer — the number of good edges in the tree.
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ N ≤ 3 \cdot 10^{5}$
$1 ≤ Q ≤ 3 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$ and $u_{i} \neq v_{i}$.
- The sum of $N$ and $Q$ over all test cases won't exceed $3 \cdot 10^{5}$.
- Atleast one query of type $2$ is present.
----- Sample Input 1 ------
1
5
1 2
1 3
2 4
2 5
5
1 1
1 3
2
1 5
2
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Query $1$: Node $1$ is not a leaf node. Thus, we change its value to the AND-value of its *descendants*. Descendants of node $1$ are $\{2, 3, 4, 5\}$. All these nodes have value $0$. Thus, the bitwise AND of all values is also $0$. Hence, node $1$ is assigned $0$.
Query $2$: Node $3$ is assigned $1$ since it is a leaf node.
Query $3$: Edges between nodes $(1, 2), (2, 4),$ and $(2, 5)$ are good edges.
- Edge $(1, 2)$: On removing the edge the set of nodes in the two trees are $\{1, 3\}$ and $\{2, 4, 5\}$. The AND-values of the respective set of nodes is $0$ $\&$ $1 = 0$ and $0$ $\&$ $0$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 4)$: On removing the edge the set of nodes in the two trees are $\{4\}$ and $\{1, 2, 3, 5\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
- Edge $(2, 5)$: On removing the edge the set of nodes in the two trees are $\{5\}$ and $\{1, 2, 3, 4\}$. The AND-values of the respective set of nodes is $0$ and $0$ $\&$ $0$ $\&$ $1$ $\&$ $0 = 0$, which is equal.
Query $4$: Node $5$ is assigned $1$ since it is a leaf node.
Query $5$: Edges between nodes $(1, 2)$ and $(2, 4)$ are good edges. | T = int(input())
for _ in range(T):
V = int(input())
val = {(i + 1): (0) for i in range(V)}
E = []
P = {(i + 1): (-1) for i in range(V)}
C = {(i + 1): [] for i in range(V)}
A = {(i + 1): [] for i in range(V)}
for i in range(V - 1):
u, v = (int(j) for j in input().split())
E.append((u, v))
A[u].append(v)
A[v].append(u)
vis = {(i + 1): (False) for i in range(V)}
qu = [1]
while len(qu) != 0:
v = qu[-1]
qu.pop()
vis[v] = True
for u in A[v]:
if not vis[u]:
qu.append(u)
C[v].append(u)
P[u] = v
Q = int(input())
gE = [True] * (V - 1)
gEn = V - 1
for i in range(Q):
inpQ = [int(j) for j in input().split()]
if inpQ[0] == 1:
u = inpQ[1]
if len(C[u]) == 0:
if val[u] == 1:
continue
if val[u] == 0:
val[u] = 1
gEn -= 1
else:
new_val = 1
for c in C[u]:
if val[c] == 0:
new_val = 0
break
if new_val == val[u]:
continue
else:
val[u] = 1
if u == 1:
gEn = V - 1
else:
gEn -= 1
if inpQ[0] == 2:
print(gEn) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
done = [0] * n
ans = 0
while nums != done:
for i, x in enumerate(nums):
if x % 2:
nums[i] -= 1
ans += 1
if nums != done:
for i, x in enumerate(nums):
nums[i] /= 2
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
count = 0
while True:
op = False
zeroes = 0
for i in range(len(nums)):
if nums[i] % 2 != 0:
count += 1
nums[i] -= 1
for i in range(len(nums)):
if nums[i] == 0:
zeroes += 1
else:
nums[i] = nums[i] // 2
op = True
if op:
count += 1
if zeroes == len(nums):
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
valid = {(0): 0}
for i in range(31):
valid[2**i] = i
ans = 0
max_multi_step = float("-inf")
for i in range(len(nums)):
if nums[i] > 0:
ans += 1
else:
continue
one_step, multi_step = 0, 0
while nums[i] not in valid:
if nums[i] & 1:
one_step += 1
nums[i] -= 1
else:
multi_step += 1
nums[i] //= 2
ans += one_step
max_multi_step = max(max_multi_step, multi_step + valid[nums[i]])
ans += max_multi_step
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def count_ops(x: int) -> int:
if x == 0:
return 0, 0
elif x == 1:
return 1, 0
elif x % 2 == 0:
add, mul = count_ops(x // 2)
return add, mul + 1
else:
add, mul = count_ops(x - 1)
return add + 1, mul
count = maxmul = 0
for i in nums:
add, mul = count_ops(i)
maxmul = max(maxmul, mul)
count += add
return count + maxmul | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER NUMBER IF VAR NUMBER RETURN NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
length = len(nums)
def check_all_zeros(arr):
for ele in arr:
if ele != 0:
return False
return True
def divide_by_2(arr):
return list([(x / 2) for x in arr])
ops = 0
while True:
if check_all_zeros(nums):
return ops
all_even = True
for i in range(length):
num = nums[i]
if num % 2 == 0:
if i == length - 1 and all_even:
nums = divide_by_2(nums)
ops += 1
else:
all_even = False
nums[i] -= 1
ops += 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
n = len(nums)
s = sum(nums)
while s != 0:
nums_2 = [(num % 2) for num in nums]
diff = sum(nums_2)
if diff == 0:
nums = [(num // 2) for num in nums]
ans += 1
s //= 2
else:
ans += diff
s -= diff
nums = [(nums[i] - nums_2[i]) for i in range(n)]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
count = 0
while True:
next_nums = [0] * len(nums)
all_zeros = True
for i, x in enumerate(nums):
if x == 0:
continue
if x % 2 != 0:
count += 1
next_num = x // 2
next_nums[i] = next_num
all_zeros &= next_num == 0
if all_zeros:
break
count += 1
nums = next_nums
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
adds = []
muls = []
for idx, num in enumerate(nums):
adds.append(0)
muls.append(0)
while num > 0:
if num % 2 == 1:
adds[idx] += 1
num -= 1
else:
muls[idx] += 1
num /= 2
return sum(adds) + max(muls) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ret = 0
change = 0
while sum(nums):
change = 0
for i in range(len(nums)):
if nums[i] % 2:
ret += 1
nums[i] -= 1
change = 1
if change == 0:
ret += 1
for i in range(len(nums)):
nums[i] = nums[i] // 2
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
count = 0
onesCount = 0
aboveOneIdxs = []
for idx, n in enumerate(nums):
if n == 1:
onesCount += 1
elif n > 1:
aboveOneIdxs.append(idx)
while onesCount + len(aboveOneIdxs) != 0:
count += onesCount
onesCount = 0
newAboveOneIdxs = []
for idx in aboveOneIdxs:
if nums[idx] % 2 != 0:
nums[idx] -= 1
count += 1
nums[idx] //= 2
if nums[idx] == 1:
onesCount += 1
else:
newAboveOneIdxs.append(idx)
if len(aboveOneIdxs) > 0:
count += 1
aboveOneIdxs = newAboveOneIdxs
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations_1(self, nums: List[int]) -> int:
ops = 0
while not all(n == 0 for n in nums):
for i, n in enumerate(nums):
if n % 2 != 0:
nums[i] = n - 1
ops += 1
if all(n == 0 for n in nums):
break
nums = [(n // 2) for n in nums]
ops += 1
return ops
def minOperations_2(self, nums: List[int]) -> int:
op_1 = 0
op_2 = 0
for n in nums:
op_2_cur = 0
while n > 0:
if n % 2 != 0:
op_1 += 1
n -= 1
else:
op_2_cur += 1
n = n // 2
op_2 = max(op_2, op_2_cur)
return op_1 + op_2
def minOperations(self, nums: List[int]) -> int:
op1 = 0
op2 = 0
for i in range(31):
for n in nums:
if n & 1 << i != 0:
op1 += 1
op2 = i
return op1 + op2 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ones = 0
maxLength = 0
for x in nums:
x = bin(x)[2:]
maxLength = max(maxLength, len(x))
ones += x.count("1")
print((ones, maxLength - 1))
return ones + maxLength - 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
valid = {(0): 0}
for i in range(31):
valid[2**i] = i
ans = 0
for i in range(len(nums)):
if nums[i] > 0:
ans += 1
max_val = float("-inf")
need_step = [0] * len(nums)
max_multiply_step = 0
for i in range(len(nums)):
one_step, multiply_step = 0, 0
while nums[i] not in valid:
if nums[i] & 1:
one_step += 1
nums[i] -= 1
else:
multiply_step += 1
nums[i] //= 2
max_multiply_step = max(max_multiply_step, multiply_step + valid[nums[i]])
ans += one_step
ans += max_multiply_step
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
while any(num != 0 for num in nums):
odd = 0
for idx, num in enumerate(nums):
if num & 1:
nums[idx] -= 1
odd += 1
ans += 1
if not odd:
for idx, num in enumerate(nums):
nums[idx] = num // 2
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, A: List[int]) -> int:
r = 0
while A != [(0) for i in A]:
odd = 0
for i in range(len(A)):
if A[i] % 2 == 1:
A[i] = A[i] - 1
odd += 1
if odd == 0:
A = [(x // 2) for x in A]
r += 1
else:
r += odd
return r | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations0(self, nums: List[int]) -> int:
l = len(nums)
ans = 0
def turnEven():
nonlocal ans
for i in range(l):
if nums[i] % 2 == 1:
nums[i] -= 1
ans += 1
def countZero():
count = 0
for n in nums:
if n == 0:
count += 1
return count
while True:
turnEven()
if countZero() == l:
return ans
for i in range(l):
nums[i] //= 2
ans += 1
def minOperations(self, nums: List[int]) -> int:
res = 0
maxLen = 1
for n in nums:
bits = 0
while n > 0:
res += n & 1
bits += 1
n >>= 1
maxLen = max(maxLen, bits)
return res + maxLen - 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR WHILE NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
zeroIndexes = set()
result = 0
isEven = False
while len(zeroIndexes) != len(nums):
if isEven:
for i in range(len(nums)):
nums[i] >>= 1
result += 1
isEven = False
else:
for i in range(len(nums)):
if i not in zeroIndexes:
if nums[i] & 1:
nums[i] -= 1
result += 1
if nums[i] == 0:
zeroIndexes.add(i)
isEven = True
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
result = 0
while True:
for i, x in enumerate(nums):
if x & 1:
result += 1
nums[i] -= 1
if all(x == 0 for x in nums):
return result
for i, x in enumerate(nums):
nums[i] //= 2
result += 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
maxTwos = 0
ones = 0
for num in nums:
count, left = self.getCount(num)
maxTwos = max(maxTwos, count)
ones += left
return ones + maxTwos
def getCount(self, num):
count = 0
odd = num % 2
if num % 2 == 1:
num -= 1
while num > 1:
odd += num % 2
num //= 2
count += 1
return count, num + odd | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
n = len(nums)
while True:
ok = False
flag = True
for i in range(n):
if nums[i] & 1:
ans += 1
nums[i] -= 1
if nums[i] != 0 and not nums[i] & 1:
nums[i] //= 2
if flag:
ans += 1
flag = False
if nums[i] != 0:
ok = True
if not ok:
break
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def all_zeros(nums):
for num in nums:
if num != 0:
return False
return True
def to_all_evens(nums):
cnt = 0
for i in range(len(nums)):
if nums[i] % 2 != 0:
cnt += 1
nums[i] -= 1
return cnt
op_cnt = 0
while not all_zeros(nums):
op_cnt += to_all_evens(nums)
if not all_zeros(nums):
nums = [(x // 2) for x in nums]
op_cnt += 1
return op_cnt | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
op = 0
while len(nums) > 0:
for i in range(len(nums)):
if nums[i] % 2 == 1:
nums[i] -= 1
op += 1
nums = list(filter(lambda x: x != 0, nums))
if len(nums) > 0:
nums[:] = [int(x / 2) for x in nums]
op += 1
return op | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
mx = 1
ops = 0
for nm in nums:
bt = 0
while nm > 0:
ops += nm & 1
bt += 1
nm = nm >> 1
mx = max(mx, bt)
return ops + mx - 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def makeHalf(self, a):
for i in range(len(a)):
a[i] //= 2
def makeEven(self, a):
c = 0
for i in range(len(a)):
if a[i] % 2 == 0:
continue
else:
a[i] -= 1
c += 1
return c
def minOperations(self, nums: List[int]) -> int:
op = 0
while sum(nums) != 0:
op += self.makeEven(nums)
if sum(nums) == 0:
return op
self.makeHalf(nums)
op += 1
return op | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, a: List[int]) -> int:
ans = 0
mx = 0
for x in a:
cnt = 0
while x:
if x % 2:
ans += 1
cnt += 1
x //= 2
mx = max(mx, cnt)
return max(ans + mx - 1, 0) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
res = 0
sum1 = 1
while sum1 != 0:
sum1 = 0
for ii, i in enumerate(nums):
if i % 2 != 0:
nums[ii] -= 1
res += 1
nums[ii] = nums[ii] // 2
sum1 += nums[ii]
res += 1
return res - 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ops = 0
while not all(n == 0 for n in nums):
for i, n in enumerate(nums):
if n % 2 != 0:
nums[i] = n - 1
ops += 1
if all(n == 0 for n in nums):
break
nums = [(n // 2) for n in nums]
ops += 1
return ops | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
res = 0
max_zero = 0
for i in range(len(nums)):
zero, one = Solution.parse(nums[i])
res += one
max_zero = max(max_zero, zero)
res += max_zero
return res
def parse(v):
zero, one = 0, 0
while v > 0:
if v % 2 == 1:
one += 1
v -= 1
else:
zero += 1
v /= 2
return zero, one | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
even_num = []
odd_num = []
zero_num = []
def category(n, zero_num, even_num, odd_num):
if n == 0:
zero_num.append(n)
elif n % 2 == 0:
even_num.append(n)
else:
odd_num.append(n)
for n in nums:
category(n, zero_num, even_num, odd_num)
ans = 0
while len(even_num) or len(odd_num):
new_odd_num = []
if len(odd_num):
ans += len(odd_num)
for n in odd_num:
n = n - 1
category(n, zero_num, even_num, new_odd_num)
odd_num = new_odd_num
new_even_num = []
if len(even_num):
ans += 1
for n in even_num:
n = n // 2
category(n, zero_num, new_even_num, odd_num)
even_num = new_even_num
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
nums.sort()
result = 0
multiple = 0
last = 0
def find(mx):
nonlocal last
if mx <= 1:
return mx
if mx % 2 == 1:
return find(mx - 1) + 1
last += 1
return find(mx // 2) + 1
result = 0
for i, n in enumerate(nums):
result += find(n) - last
multiple = max(multiple, last)
last = 0
return result + multiple | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, A):
steps, n = 0, len(A)
while True:
zeros = 0
for i in range(n):
if A[i] % 2 > 0:
A[i] -= 1
steps += 1
if A[i] == 0:
zeros += 1
if zeros == n:
break
for i in range(n):
A[i] >>= 1
steps += 1
return steps | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
inc, mult = 0, 0
for i in nums:
if i:
a, b = self.getRes(i)
inc += a
mult = max(mult, b)
return inc + mult
def getRes(self, n):
inc, mult = 0, 0
while n:
if n % 2 == 1:
n -= 1
inc += 1
else:
n //= 2
mult += 1
return inc, mult | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, t: List[int]) -> int:
n = len(t)
ans = 0
while 1:
z = 0
i = 0
while i < n:
if t[i] % 2:
break
elif t[i] == 0:
z += 1
i += 1
if z == n:
return ans
if i == n:
for j in range(n):
t[j] = t[j] // 2
ans += 1
for j in range(i, n):
if t[j] & 1:
t[j] -= 1
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | masks = [1431655765, 858993459, 252645135, 16711935, 65535]
class Solution:
def minOperations(self, nums: List[int]) -> int:
totOddRem = 0
maxX = nums[0]
for x in nums:
if x == 1:
totOddRem += 1
else:
xCopy = x
for p, m in enumerate(masks):
xCopy = (xCopy & m) + ((xCopy & ~m) >> (1 << p))
totOddRem += xCopy
if x > maxX:
maxX = x
leftMost = 0
step = 16
for m in reversed(masks):
m = ~m
if m & maxX != 0:
leftMost += step
maxX &= m
step >>= 1
return totOddRem + leftMost | ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
ans = 0
shift = 0
for k in nums:
strk = bin(k)[2:]
ans += strk.count("1")
shift = max(shift, len(strk) - 1)
return ans + shift | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution1:
def minOperations(self, nums: List[int]) -> int:
twos = 0
ones = 0
for n in nums:
mul = 0
while n:
if n % 2:
n -= 1
ones += 1
else:
n //= 2
mul += 1
twos = max(twos, mul)
return ones + twos
class Solution:
def minOperations(self, nums: List[int]) -> int:
maxval = max(nums)
idx = nums.index(maxval)
ans = 0
while nums[idx] != 0:
for i, n in enumerate(nums):
ans += n % 2
nums[i] = n - n % 2
if nums[idx] != 0:
for i, n in enumerate(nums):
nums[i] = nums[i] // 2
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
res = 0
nz = 0
for num in nums:
if num > 0:
nz += 1
while nz > 0:
div2 = False
for i in range(len(nums)):
if nums[i] % 2 == 1:
nums[i] -= 1
res += 1
if nums[i] == 0:
nz -= 1
if nums[i] > 0:
div2 = True
nums[i] //= 2
if nums[i] == 0:
nz -= 1
if div2:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def get_op(n):
op_0 = 0
op_1 = 0
while n != 0:
if n % 2 == 0:
op_1 += 1
n = n // 2
else:
op_0 += 1
n -= 1
return op_0, op_1
ans = 0
op_1 = 0
for n in nums:
tmp_0, tmp_1 = get_op(n)
ans += tmp_0
op_1 = max(op_1, tmp_1)
return ans + op_1 | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
arr = [0] * len(nums)
count = 0
while nums != arr:
check = True
for x in nums:
if x % 2 != 0:
check = False
if check == True:
count += 1
for x in range(len(nums)):
nums[x] = nums[x] // 2
for x in range(len(nums)):
if nums[x] % 2 != 0:
nums[x] = nums[x] - 1
count += 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums):
add_operations = {}
result = 0
max_m = 0
for i in nums:
current = i
if current in add_operations:
result += add_operations[current]
else:
add_amount = 0
multiplying_amount = 0
while current:
if current % 2:
add_amount += 1
current -= 1
else:
multiplying_amount += 1
current = current / 2
max_m = max(max_m, multiplying_amount)
result += add_amount
add_operations[i] = add_amount
return result + max_m | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
odds: List[int] = [n for n in nums if n % 2 == 1]
evens: Set[int] = [n for n in nums if n != 0 and n % 2 == 0]
new: List[int] = []
n: int = 0
n_ops: int = 0
while len(odds) > 0 or len(evens) > 0:
if len(odds) > 0:
n = odds.pop() - 1
if n > 0:
evens.append(n)
else:
new = [(n // 2) for n in evens]
odds = [n for n in new if n % 2 == 1]
evens = [n for n in new if n % 2 == 0]
n_ops += 1
return n_ops | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR LIST VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
count = 0
double = 0
for i in range(len(nums)):
cd = 0
while nums[i] > 0:
if nums[i] % 2 == 1:
count += 1
cd += 1
nums[i] //= 2
double = max(double, cd)
return count + double - 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
num_operations = 0
odd = True
even = False
while odd or even:
if odd:
for i in range(len(nums)):
if nums[i] != 0:
if nums[i] % 2:
nums[i] -= 1
num_operations += 1
if nums[i]:
even = True
odd = False
if even:
num_operations += 1
for i in range(len(nums)):
nums[i] /= 2
if nums[i] % 2:
even = False
odd = True
return num_operations | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
counter = 0
while 1:
temp_counter = 0
bigger_than_zero = 0
for i in range(len(nums)):
if nums[i] % 2 == 1:
temp_counter += 1
nums[i] -= 1
if nums[i] > 0:
bigger_than_zero += 1
temp = bin(nums[i])
counter += temp_counter
if bigger_than_zero == 0:
return counter
run_again = True
while run_again:
counter += 1
for i in range(len(nums)):
nums[i] //= 2
if nums[i] % 2 == 1:
run_again = False
return counter | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def helper(num):
if num in self.dic:
return self.dic[num]
n = num
count, even = 0, 0
while n != 0:
if n % 2 == 1:
n -= 1
else:
n //= 2
even += 1
count += 1
self.dic[num] = count, even
return count, even
ans, maxb, self.dic = 0, 0, {}
for num in nums:
a, b = helper(num)
ans += a - b
maxb = max(maxb, b)
return ans + maxb | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
mul = [(0) for i in range(len(nums))]
inc = [(0) for i in range(len(nums))]
for i in range(len(nums)):
while nums[i] != 0:
if nums[i] % 2 == 1:
inc[i] += 1
nums[i] = nums[i] - 1
else:
mul[i] += 1
nums[i] //= 2
return max(mul) + sum(inc) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
count = 0
while True:
zeros = 0
for i in range(len(nums)):
if nums[i] % 2 == 0:
continue
else:
nums[i] -= 1
count += 1
for j in range(len(nums)):
if nums[j] == 0:
zeros += 1
nums[j] = nums[j] // 2
if zeros == len(nums):
return count
count += 1 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def proc(nums):
cnt = 0
for i in range(len(nums)):
if nums[i] % 2 > 0:
nums[i] -= 1
cnt += 1
if cnt == 0:
for i in range(len(nums)):
nums[i] //= 2
return max(cnt, 1)
ops = 0
while sum(nums) > 0:
ops += proc(nums)
return ops | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
if max(nums) == 0:
return 0
return sum(map(lambda x: bin(x).count("1"), nums)) + int(log2(max(nums))) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def calculate(num):
temp1 = 0
temp2 = 0
while num:
if num % 2:
temp1 += 1
num -= 1
else:
temp2 += 1
num >>= 1
return temp1, temp2
ones, twos = 0, 0
for val in nums:
if val:
a, b = calculate(val)
ones += a
twos = max(twos, b)
print(ones)
return ones + twos | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
totOddRem = 0
max2Power = 0
for x in nums:
pow2 = 0
while True:
totOddRem += x & 1
if x > 1:
pow2 += 1
x >>= 1
else:
break
if pow2 > max2Power:
max2Power = pow2
return totOddRem + max2Power | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
if not nums or sum(nums) == 0:
return 0
cnt = 0
while sum(nums) != 0:
if all(num % 2 == 0 for num in nums):
cnt += 1
for i in range(len(nums)):
nums[i] //= 2
else:
for i in range(len(nums)):
if nums[i] % 2:
cnt += 1
nums[i] -= 1
return cnt | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
Your task is to form an integer array nums from an initial array of zeros arr that is the same size as nums.
Return the minimum number of function calls to make nums from arr.
The answer is guaranteed to fit in a 32-bit signed integer.
Example 1:
Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements) [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.
Example 2:
Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.
Example 3:
Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).
Example 4:
Input: nums = [3,2,2,4]
Output: 7
Example 5:
Input: nums = [2,4,8,16]
Output: 8
Constraints:
1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9 | class Solution:
def minOperations(self, nums: List[int]) -> int:
def calc(num):
tmp = 1
ans = [0, 0]
count = 0
while tmp <= num:
if tmp & num != 0:
ans[0] += 1
count += 1
tmp <<= 1
ans[1] = count - 1
return ans
result = 0
tt = 0
for num in nums:
r = calc(num)
result += r[0]
tt = max(tt, r[1])
result += tt
return result | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.