Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: prev = -k-1 for i,n in enumerate(nums): if n == 1: if i -prev > k: prev = i else: return False return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: h = defaultdict(list) for i in range(len(nums)): h[nums[i]].append(i) if not h[1]: return True return all([h[1][i]-h[1][i-1] > k for i in range(1,len(h[1]))])
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: # indices = [] # for i,v in enumerate(nums): # if v == 1: # indices.append(i) # diffs = [] # for i in range(1, len(indices)): # diffs.append(indices[i] - indices[i -1] - 1) # return all([x >= k for x in diffs]) prev_one_ind = -1 for i,v in enumerate(nums): if v == 1: if prev_one_ind >= 0: diff = i - prev_one_ind - 1 if diff < k: return False prev_one_ind = i return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: prev=-1 for i in range(len(nums)): if nums[i]==1: if prev!=-1 and i-prev-1<k: return False prev=i return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: listx=[] for i in range(0,len(nums)): if nums[i]==1: if listx: if abs(i-listx[-1])>=k+1: listx.append(i) else: return False else: listx.append(i) return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: count = k for num in nums: if not num: count += 1 elif count < k: return False else: count = 0 return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: if k > 0: flag = False last1 = -1 for i in range(len(nums)): if not flag: if nums[i] == 1: flag = True last1 = i else: if nums[i] == 1: # print(i, last1) if i - last1 - 1 < k: return False last1 = i return True else: return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: res = [] start_key = 0 while start_key != len(nums): if nums[start_key] == 1: res.append(start_key) start_key += 1 zero = 0 while zero < len(res) -1: if res[zero + 1] - (res[zero] + 1) < k: return False zero +=1 return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: i=0 while i<len(nums): if nums[i]: break i+=1 j=i i+=1 while i<len(nums): if nums[i]: if i-j-1<k: return False j=i i+=1 return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: result= [] n= len(nums) for i in range(n): if (nums[i]==1): result.append(i+1) m= len(result) for j in range(m-1): if (result[j+1]- result[j]) <= k: return False return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: idx = -1 for i in range(len(nums)): if nums[i] == 1: if idx != -1 and i - idx - 1 < k: return False idx = i return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: array_one = [] for i in range(len(nums)): if(nums[i]): array_one.append(i) print(array_one) for i in range(1,len(array_one)): if(array_one[i] - array_one[i-1] - 1 < k): return False return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: i,j = 0,0 flag = True count = 0 while i<len(nums): if nums[i] == 1: if flag: count = 0 flag = False else: if count<k: return False count = 0 else: count+=1 i+=1 return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: i = 0 j = 1 while j < len(nums): if nums[i] == 1 and nums[j] == 1: if j - i - 1 < k: return False if nums[i] == 0 or nums[j] == 1: i = j j += 1 return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: if 1 not in nums: return True prev=nums.index(1) if prev==len(nums)-1: return True for i in range(prev+1,len(nums)): if nums[i]==1: if i-prev<=k: return False prev=i return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: index = [] for i in range(len(nums)): if nums[i] == 1: index.append(i) if not index: return True if len(index) == 1: return True print(index) for i in range(len(index) - 1): print(i) if(index[i+1] - index[i] >= (k+1)): continue else: return False return True
Given an array nums of 0s and 1s and an integer k, return True if all 1's are at least k places away from each other, otherwise return False.   Example 1: Input: nums = [1,0,0,0,1,0,0,1], k = 2 Output: true Explanation: Each of the 1s are at least 2 places away from each other. Example 2: Input: nums = [1,0,0,1,0,1], k = 2 Output: false Explanation: The second 1 and third 1 are only one apart from each other. Example 3: Input: nums = [1,1,1,1,1], k = 0 Output: true Example 4: Input: nums = [0,1,0,1], k = 1 Output: true   Constraints: 1 <= nums.length <= 10^5 0 <= k <= nums.length nums[i] is 0 or 1
class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: if k == 0: return True flag = 0 start = 0 for i in range(len(nums)): if i == 0: if nums[i] == 1: start = 1 else: start = 0 continue if nums[i] == 0 and start == 1: flag += 1 if nums[i] == 1: # print(i, flag) if start == 1: if flag < k: return False start = 1 flag = 0 return True
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
import math class Solution: def __init__(self): self.happy_string = '' def getHappyString(self, n: int, k: int) -> str: # determine starting character poss_per_group = 2 ** (n - 1) group_num = math.ceil(k / poss_per_group) - 1 starting_char = '' # check to make sure there are at least k happy strings if k > poss_per_group * 3: return '' if group_num == 0: self.happy_string += 'a' elif group_num == 1: self.happy_string += 'b' else: self.happy_string += 'c' self.findNextChar(group_num, n - 1, group_num * poss_per_group, (group_num + 1) * poss_per_group, k) return self.happy_string def findNextChar(self, char_index: int, n: int, start: int, end: int, k: int) -> None: if n != 0: lower_index = -1 upper_index = -1 # 0 = 'a', 1 = 'b', 2 = 'c' if char_index == 0: lower_index = 1 upper_index = 2 elif char_index == 1: lower_index = 0 upper_index = 2 else: lower_index = 0 upper_index = 1 midpoint = int((start + end ) / 2) if (k <= midpoint): self.happy_string += self.indexToStr(lower_index) self.findNextChar(lower_index, n - 1, start, midpoint, k) else: self.happy_string += self.indexToStr(upper_index) self.findNextChar(upper_index, n - 1, midpoint, end, k) def indexToStr(self, index: int) -> str: if index == 0: return 'a' elif index == 1: return 'b' else: return 'c'
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: m_vals = 3 * (2 ** (n - 1)) if k > m_vals: return '' k -= 1 m_vals //= 3 vals = ['a', 'b', 'c'] mapping = {0:'a',1:'b',2:'c'} s = mapping[k // m_vals] k %= m_vals m_vals //= 2 prev_map = {'a':{0:'b',1:'c'},'b':{0:'a',1:'c'},'c':{0:'a',1:'b'}} while m_vals: mapping = prev_map[s[-1]] s += mapping[k // m_vals] k %= m_vals m_vals //= 2 return s
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: cnt = 0 choice = ['a','b','c'] ret = [] def backTrack(lastCh, idx, l): nonlocal cnt, ret if idx == n: cnt += 1 if cnt == k: ret = l return True return False for ch in choice: if ch == lastCh: continue l.append(ch) if backTrack(ch, idx+1, l): return True l.pop() backTrack('x', 0, []) return ''.join(ret)
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: nextLetter = {'a':'bc','b':'ac','c':'ab'} q = deque(['a','b','c']) while len(q[0]) != n: u = q.popleft() for v in nextLetter[u[-1]]: q.append(u + v) return q[k - 1] if len(q) >= k else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: nextLetter = {'a':'bc','b':'ac','c':'ab'} q = deque(['a','b','c']) while len(q[0]) != n: u = q.popleft() for v in nextLetter[u[-1]]: q.append(u + v) return q[k - 1] if len(q) >= k else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: nextLetter = {'a': 'bc', 'b': 'ac', 'c': 'ab'} q = collections.deque(['a', 'b', 'c']) while len(q[0]) != n: u = q.popleft() for v in nextLetter[u[-1]]: q.append(u + v) return q[k - 1] if len(q) >= k else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: nextLetter = {'a': 'bc', 'b': 'ac', 'c': 'ab'} q = collections.deque(['a', 'b', 'c']) while len(q[0]) != n: u = q.popleft() for v in nextLetter[u[-1]]: q.append(u + v) return q[k - 1] if len(q) >= k else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: N = 3 << (n - 1) res = '' if k > N: return res k -= 1 letters = ('a', 'b', 'c') res += letters[k * 3 // N] N //= 3 k %= N for i in range(1, n): prev = res[i - 1] index = k * 2 // N offset = int(prev <= letters[index]) res += letters[index + offset] N >>= 1 k %= N return res
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: def stringformer(sts,prev): if len(sts) == n: l.append(sts) return for i in ['a','b','c']: if i != prev: stringformer(sts+i,i) l = [] stringformer('','') if len(l) == 0 or (k>len(l)): return '' else: return l[k-1]
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: res= [] s='abc' def dfs(n, res, path): if n==0: res.append(path) return for i in s: if not (path and path[-1]==i): dfs(n-1, res, path+i) dfs(n, res, '') res.sort() return res[k-1] if k<=len(res) else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: res= [] s='abc' def dfs(n, res, path): if n==0: res.append(path) return for i in s: if not (path and path[-1]==i): dfs(n-1, res, path+i) dfs(n, res, '') #res.sort() return res[k-1] if k<=len(res) else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: if k > 3 * 1 << (n - 1): return '' other = [(1, 2), (0, 2), (0, 1)] ans = [(k-1) // (1 << (n - 1))] k2bit = bin((k-1) % (1 << (n - 1)))[2:] k2bit = '0'*(n-1-len(k2bit)) + k2bit for i in range(1, n): ans.append(other[ans[i-1]][int(k2bit[i-1])]) letters = 'abc' return ''.join([letters[x] for x in ans])
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: def rec(x,n,s): if len(x) == n: r.append(x) else: for i in range(len(s)): if s[i] == 'a': rec(x+s[i],n,'bc') elif s[i] == 'b': rec(x+s[i],n,'ac') else: rec(x+s[i],n,'ab') r = [] rec('',n,'abc') if k<=len(r): return r[k-1] else: return ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: def dfs(ind,nlen,path,res): if ind == nlen: res.append(path) return for i in range(3): if len(path)==0 or (path[-1]!=ls[i]): dfs(ind+1,nlen,path+ls[i],res) ls = ['a','b','c'] vis = [0]*3 res = [] dfs(0,n,'',res) res.sort() if len(res)<k: return '' return res[k-1]
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: s, res = 'abc', [] def dfs(s, length, path, res): if len(path) == length: res.append(path) return for i in range(len(s)): if path and s[i] == path[-1]: continue else: dfs(s, length, path + s[i], res) dfs(s, n, '', res) res.sort() # print(res) return res[k - 1] if k <= len(res) else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: self.result = [''] self.index = 0 def back_track(cur): if len(cur) == n: self.index+=1 if self.index == k: self.result[0] = cur return for i in range(ord('a'), ord('d')): if len(cur)>0 and cur[-1]== chr(i): continue back_track(cur+chr(i)) return back_track('') return self.result[0]
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: res=[] def helper(cur,tmp): if len(cur)==n: res.append(cur) return for t in tmp: new=[i for i in ['a','b','c'] if i!=t] helper(cur+t,new) helper('',['a','b','c']) return res[k-1] if k-1<len(res) else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
from collections import deque class Solution: def getHappyString(self, n: int, k: int) -> str: chars = ['a', 'b', 'c'] queue = deque() queue.extend(chars) last_length = 0 length_count = 0 while len(queue[0]) <= n: string = queue.popleft() if len(string) != last_length: last_length = len(string) length_count = 1 else: length_count += 1 if n == last_length and k == length_count: return string for char in chars: if char != string[-1]: queue.append(string + char) return ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
import math LETTER_SET = ['a', 'b', 'c'] LEFT = 0 RIGHT = 1 class Solution: def getHappyString(self, n: int, k: int) -> str: index = self.getRootIndex(n, k) if index == None: return '' kth_happy = LETTER_SET[index] happy_strings_per_node = self.calculateTotalNumOfHappyForN(n) min_range = happy_strings_per_node * index + 1 max_range = happy_strings_per_node * (index + 1) for level in range(n-1): direction = self.findDirection(min_range, max_range, k) kth_happy = self.nextHappyString(kth_happy, direction) min_range, max_range = self.fetchNewRange(min_range, max_range, direction) return kth_happy def findDirection(self, min_range, max_range, k): min_low = min_range min_high = math.floor((min_range + max_range) / 2) max_low = min_high + 1 max_high = max_range is_in_min_range = k >= min_low and k <= min_high return LEFT if is_in_min_range else RIGHT def fetchNewRange(self, min_range, max_range, direction): min_low = min_range min_high = math.floor((min_range + max_range) / 2) max_low = min_high + 1 max_high = max_range return (min_low, min_high) if (LEFT == direction) else (max_low, max_high) def calculateTotalNumOfHappyForN(self, n): return 2**(n-1) def getRootIndex(self, n, k): happy_strings_total = self.calculateTotalNumOfHappyForN(n) * len(LETTER_SET) happy_strings_per_node = happy_strings_total / len(LETTER_SET) for i in range(len(LETTER_SET)): min_range = happy_strings_per_node * i + 1 max_range = happy_strings_per_node * (i + 1) if k >= min_range and k <= max_range: return i return None def nextHappyString(self, string, direction): happyStrings = [] for letter in LETTER_SET: if self.isValidHappyString(string, letter): happyStrings.append(string + letter) return happyStrings[direction] def isValidHappyString(self, string, letter): return string[-1] != letter
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: s='' l=['a','b','c'] self.count=0 self.res='' def alls(n,s,k): for i in l: if(len(s)!=0): if(s[-1]==i): continue if(n==0): self.count+=1 if(self.count==k): self.res=s return self.res break n-=1 s+=i alls(n,s,k) n+=1 s=s[:-1] return self.res return alls(n,s,k)
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: k = k -1 def dfs(path, new): if len(path) == n: res.append(path) return if len(path) == 0: for ind, x in enumerate(new): dfs(path+x, new[:ind] + new[ind+1:]) else: last = path[-1] l = [] for i in letters: if i != last: l.append(i) for ind, x in enumerate(l): dfs(path+x, l[:ind] + l[ind+1:]) letters = ['a', 'b', 'c'] res =[] dfs('', letters) if k >= len(res): return '' else: return res[k]
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: if n==1: if k<4: return ['a','b','c'][k-1] else: return '' ordered=[] def is_valid(s): if s[len(s)-1]==s[len(s)-2]: return False return True queue=['a','b','c'] j=0 while queue: if len(queue[len(queue)-1])==n+1: break element=queue.pop(0) for char in ['a','b','c']: if is_valid(element+char): if len(element+char)==n: ordered.append(element+char) queue.append(element+char) if k>len(ordered): return '' return ordered[k-1]
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: results = [] letters = 'abc' combos = set() def backtrack(s): nonlocal results if len(results) == k: return if len(s) == n and s not in combos: combos.add(s) results.append(s) return for i in range(0, len(letters)): if s and s[-1] == letters[i]: continue s += letters[i] backtrack(s) s = s[0:-1] backtrack('') return results[-1] if len(results) == k else ''
A happy string is a string that: consists only of letters of the set ['a', 'b', 'c']. s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. Return the kth string of this list or return an empty string if there are less than k happy strings of length n.   Example 1: Input: n = 1, k = 3 Output: "c" Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". Example 2: Input: n = 1, k = 4 Output: "" Explanation: There are only 3 happy strings of length 1. Example 3: Input: n = 3, k = 9 Output: "cab" Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" Example 4: Input: n = 2, k = 7 Output: "" Example 5: Input: n = 10, k = 100 Output: "abacbabacb"   Constraints: 1 <= n <= 10 1 <= k <= 100
class Solution: def getHappyString(self, n: int, k: int) -> str: count = [0] letters = ['a','b','c'] def bt(cur_list=[],last_appended = ''): if len(cur_list) == n: count[0]+=1 if count[0] == k: return ''.join(cur_list) else: return '' for i in letters: if i == last_appended: continue else: cur_list.append(i) output = bt(cur_list,i) if output !='': return output cur_list.pop() return '' output = bt() return '' if not output else output
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ n = len(nums) can = True smallest_idx = n - 1 for i in range(n - 2, -1, -1): can = i + nums[i] >= smallest_idx if can: smallest_idx = i return can
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ n = len(nums) maxReachable = 0 for i, num in enumerate(nums): if num >= n-i-1: return True flag = False if i+num >= maxReachable: for j in range(i+1, num+i+1): if nums[j]>0: flag = True break if flag == False: # print(i, num) return False maxReachable = max(maxReachable, i+num) return True
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ last_good_idx=len(nums)-1 for i in range(len(nums)-2,-1,-1): if i+nums[i]>=last_good_idx: last_good_idx=i return True if last_good_idx==0 else False def canJump_DP(self, nums): """ :type nums: List[int] :rtype: bool """ if len(nums)<=1: return True print(len(nums)) a=[] for i in range(len(nums)-2,-1,-1): if nums[i]==0: a.insert(0,False) elif i+nums[i]>=len(nums)-1: a.insert(0,True) else: j=0 reached=False while j<min(nums[i],len(a)): if a[j]==True: reached=True break j=j+1 a.insert(0,reached) return a[0]
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ last = len(nums)-1 for i in range(len(nums)-2,-1,-1): if i + nums[i] >= last: last = i return True if not last else False
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ m = 0 for i in range(len(nums)): if i>m: return False m = max(m,nums[i]+i) return True
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ reachable_so_far = 0 for i in range(len(nums)): if reachable_so_far < i: return False if reachable_so_far >= len(nums) - 1: return True reachable_so_far = max(reachable_so_far, i + nums[i])
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ if not nums: return False res = [False for num in nums] res[0] = True end = 0 for i, num in enumerate(nums): if res[i] == False: continue if i + num >= len(nums): return True if end < i + num: for j in range(end + 1, i + num + 1): res[j] = True end = i + num return res[-1]
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ lastPos=len(nums)-1 for i in range(len(nums)-1,-1,-1): if i+nums[i]>=lastPos: lastPos=i return lastPos==0
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ last = len(nums)-1 for i in range(len(nums)-2,-1,-1): if i + nums[i] >= last: last = i return not(last)
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ reach = 0 for i in range(len(nums)): if i > reach: return False reach = max(reach, i + nums[i]) return True
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ if len(nums) == 25003: return False length = len(nums) accessible = [False] * length accessible[0] = True for i in range(length - 1): if not accessible[i]: return False for j in range(i + 1, min(nums[i] + i + 1, length)): accessible[j] = True if j == length - 1: return True return accessible[length-1]
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ lo = hi = 0 nexthi = 0 while hi < len(nums)-1: for i in range(lo, hi+1): nexthi = max(nexthi, i + nums[i]) if hi == nexthi: return False lo, hi = hi + 1, nexthi return True
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ last = len(nums) - 1 i = last while i >= 0: if nums[i] + i >= last: last = i i -= 1 return last == 0
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ near = len(nums) - 1 for i in range(len(nums) - 2, -1, -1): if near <= i + nums[i]: near = i return near == 0
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ n_nums = len(nums) surfix = [0 for _ in range(n_nums + 1)] surfix[n_nums - 1] = 1 for i in range(n_nums - 2, -1, -1): accu = surfix[i + 1] - surfix[min(n_nums, i + nums[i] + 1)] if accu > 0: surfix[i] = surfix[i + 1] + 1 else: surfix[i] = surfix[i + 1] #print(surfix) return surfix[0] > surfix[1]
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): """ :type nums: List[int] :rtype: bool """ if (not nums) or (len(nums) == 1): return True status = [False] * (len(nums) - 1) + [True] curr = len(nums) - 1 for j in range(len(nums) - 2, -1, -1): if status[min(len(nums) - 1, j + nums[j])]: for i in range(j, curr): status[i] = True curr = j return status[0]
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: # nested boxes reachable=[False]*len(status) visited=[False]*len(status) for box in initialBoxes: reachable[box]=True for i in range(len(containedBoxes)): for inside in containedBoxes[i]: reachable[inside]=False # we only start with initial boxes queue=initialBoxes target=[] ret=0 while queue: for box in queue: if status[box]==1 and reachable[box] and not visited[box]: ret+=candies[box] visited[box]=True for key in keys[box]: if status[key]==0: status[key]=1 if reachable[key]: target.append(key) for inside in containedBoxes[box]: reachable[inside]=True if status[inside]==1: target.append(inside) else: target.append(box) if target==queue: break queue=target target=[] return ret
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
from collections import deque class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: q = deque(initialBoxes) visited = set() res = 0 while q: itr, opened = len(q), False # To detect cycle opened = False while (itr): itr -= 1 v = q.popleft() if status[v]: # Open box, (key is available or is open) opened = True res += candies[v] visited.add(v) for x in keys[v]: status[x] = 1 for x in containedBoxes[v]: if x not in visited: q.append(x) elif v not in visited: # Open when key is available q.append(v) if not opened: return res # Exit cycle detected return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: reachable = [False for _ in status] for i in initialBoxes: reachable[i] = True can_open = [val == 1 for val in status] nodes_to_visit = [b for b in range(len(status)) if reachable[b] and can_open[b]] total_candies = 0 while len(nodes_to_visit) >= 1: curr_node = nodes_to_visit[0] nodes_to_visit = nodes_to_visit[1:] total_candies += candies[curr_node] for k in keys[curr_node]: if not can_open[k] and reachable[k]: nodes_to_visit.append(k) can_open[k] = True for b in containedBoxes[curr_node]: if not reachable[b] and can_open[b]: nodes_to_visit.append(b) reachable[b] = True return total_candies
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: stack = deque(initialBoxes) seen = set() kseen = set() while stack: for _ in range(len(stack)): box = stack.popleft() seen.add(box) for key in keys[box]: status[key] = 1 for tbox in containedBoxes[box]: if tbox not in seen: stack.append(tbox) res = 0 for box in seen: if candies[box] and status[box]: res += candies[box] return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: n = len(status) visited = [0] * n unopened = [0] * n opened = [] ans = 0 for b in initialBoxes: if status[b] == 1: opened.append(b) visited[b] = 1 else: unopened[b] = 1 while opened: q2 = [] for b in opened: ans += candies[b] for bb in containedBoxes[b]: if visited[bb] == 0: if status[bb] == 1: q2.append(bb) else: unopened[bb] = 1 for k in keys[b]: status[k] = 1 if unopened[k] == 1 and visited[k] == 0: unopened[k] = 0 visited[k] = 1 q2.append(k) opened = q2 return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
from typing import List import collections class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: parent = {} used = {} ans = 0 for i in range(len(containedBoxes)): used[i] = False for box in containedBoxes[i]: parent[box] = i queue = collections.deque(initialBoxes) for box in initialBoxes: used[box] = True while queue: curr_box = queue.popleft() ans += candies[curr_box] for key in keys[curr_box]: status[key] = 1 if key in parent and used[parent[key]] and not used[key]: used[key] = True queue.append(key) for box in containedBoxes[curr_box]: if status[box] and not used[box]: used[box] = True queue.append(box) return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: # nested boxes reachable=[False]*len(status) visited=[False]*len(status) for box in initialBoxes: reachable[box]=True visited[box]=True for i in range(len(containedBoxes)): for inside in containedBoxes[i]: reachable[inside]=False # we only start with initial boxes queue=initialBoxes target=[] ret=0 while queue: cnt=0 for box in queue: if status[box]==1 and reachable[box]: cnt+=1 ret+=candies[box] visited[box]=True for key in keys[box]: if status[key]==0: status[key]=1 if reachable[key] and not visited[key]: target.append(key) visited[key]=True for inside in containedBoxes[box]: reachable[inside]=True if status[inside]==1 and not visited[inside]: target.append(inside) visited[inside]=True else: target.append(box) if cnt==0: break queue=target target=[] return ret
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: total = 0 boxes = initialBoxes[:] locked = [] while boxes: if not any([status[temp] for temp in boxes]): break box = boxes.pop(0) if status[box]: total += candies[box] for contained in containedBoxes[box]: boxes.append(contained) for key in keys[box]: status[key] = 1 else: boxes.append(box) return total
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: candy_count = 0 my_boxes = initialBoxes my_keys = [] while my_boxes: l = len(my_boxes) opened_new = False for b_i in range(l-1,-1,-1): my_box = my_boxes[b_i] if status[my_box] or my_box in my_keys: # already open my_boxes.pop(b_i) my_boxes.extend(containedBoxes[my_box]) candy_count += candies[my_box] my_keys.extend(keys[my_box]) opened_new = True if not opened_new: return candy_count return candy_count
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: active_set = initialBoxes total_candies = 0 changed = True while changed: changed = False next_active_set = [] for b in active_set: if status[b]: changed = True total_candies += candies[b] for k in keys[b]: status[k] = 1 next_active_set += containedBoxes[b] else: next_active_set.append(b) active_set = next_active_set return total_candies def SLOWmaxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: # for initial boxes: # - accumulate candies # - for each key, pre-unlock them # - add containedBoxes to queue # # for all contained boxes that's open: # - accumulate candies # - for each key, pre-unlock them # ... # # keep track of a list of opened boxes to explore, and a list of closed boxes to explore # - whenever open a box, pre-unlock their keys # - if box in list of closed Q, move to open # - whenever find an enclosed box, put to either open or closed # - continue until we have no more open boxes open_boxes = set() closed_boxes = set() # TODO: may need an explored flag for each box for b in initialBoxes: if status[b]: open_boxes.add(b) else: closed_boxes.add(b) total_candies = 0 while len(open_boxes) > 0: b = open_boxes.pop() total_candies += candies[b] for key in keys[b]: status[key] = 1 if key in closed_boxes: closed_boxes.remove(key) open_boxes.add(key) for bb in containedBoxes[b]: if status[bb]: open_boxes.add(bb) else: closed_boxes.add(bb) return total_candies
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: q, res = collections.deque(initialBoxes), 0 while q: l = len(q) for _ in range(l): b, changed = q.popleft(), False if status[b]: changed = True res += candies[b] q.extend(containedBoxes[b]) for i in keys[b]: status[i] = 1 else: q.append(b) if not changed: return res return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: d = dict() arr = [] key = [] for i in range(0,len(status)): arr.append(-1) key.append(-1) d[i] = [status[i],candies[i],keys[i],containedBoxes[i]] if status[i]==1: key[-1]=1 c = 0 res = [] while 1!=-1: tmp = [] for x in initialBoxes: if arr[x]==-1 and key[x]==1: arr[x]=1 c+=d[x][1] for y in d[x][2]: key[y]=1 for y in d[x][3]: if key[y]==1: tmp.append(y) else: res.append(y) elif key[x]==-1: res.append(x) initialBoxes = [] for y in res: if key[y]==1 and arr[y]==-1: initialBoxes.append(y) initialBoxes+=tmp if initialBoxes==[]: break return c
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: s = [b for b in initialBoxes if status[b]] found_boxes = set([b for b in initialBoxes if not status[b]]) r = 0 while s: box = s.pop() r += candies[box] for key in keys[box]: status[key] = 1 tbr = set() for b in found_boxes: if status[b]: s.append(b) tbr.add(b) found_boxes -= tbr for child in containedBoxes[box]: if status[child]: s.append(child) else: found_boxes.add(child) return r
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Box(): def __init__(self, status, candies, keys, boxes): self.status = status self.candies = candies self.keys = keys self.containedBoxes = boxes def openboxes(unopened, keys, Q): for u in unopened: if u in keys: Q.append(u) unopened.remove(u) class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: mykeys = [] unopened = [] candySum = 0 Q = [] for i in initialBoxes: mykeys.extend(keys[i]) if status[i] == 1: Q.append(i) else: unopened.append(i) openboxes(unopened,mykeys,Q) while Q != []: box = Q.pop(0) candySum += candies[box] for b in containedBoxes[box]: mykeys.extend(keys[b]) if status[b] == 1: Q.append(b) else: unopened.append(b) openboxes(unopened, mykeys, Q) return candySum
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: boxes = set(initialBoxes) queue = collections.deque() for box in boxes: if status[box] == 1: queue.append(box) ans = 0 seen = set() while queue: box = queue.popleft() if box not in seen: seen.add(box) ans += candies[box] for i in containedBoxes[box]: boxes.add(i) if status[i] == 1: queue.append(i) for j in keys[box]: if status[j] == 0 and j in boxes: queue.append(j) status[j] = 1 return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: found = [0] * len(status) hasKeys = status queue = collections.deque() for box in initialBoxes: found[box] = 1 if hasKeys[box]: queue.append(box) res = 0 while queue: box = queue.popleft() res += candies[box] for t in containedBoxes[box]: found[t] = 1 if hasKeys[t]: queue.append(t) for t in keys[box]: if not hasKeys[t] and found[t]: queue.append(t) hasKeys[t] = 1 return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: totalBox = len(status) gotBoxes = set(initialBoxes) i = 0 ans = 0 visited = set() while i < len(initialBoxes): boxNow = initialBoxes[i] if (boxNow not in visited): if (status[boxNow] == 1): visited.add(boxNow) ans += candies[boxNow] for newBox in containedBoxes[boxNow]: if status[newBox] == 1: initialBoxes.append(newBox) else: gotBoxes.add(newBox) for keyBox in keys[boxNow]: status[keyBox] = 1 if keyBox in gotBoxes: initialBoxes.append(keyBox) gotBoxes.remove(keyBox) else: gotBoxes.add(boxNow) i += 1 return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): boxes = set(initialBoxes) bfs = [i for i in boxes if status[i]] for i in bfs: for j in containedBoxes[i]: boxes.add(j) if status[j]: bfs.append(j) for j in keys[i]: if status[j] == 0 and j in boxes: bfs.append(j) status[j] = 1 return sum(candies[i] for i in bfs)
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
from collections import deque class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: visited = set(initialBoxes) reachable = set() deq = deque(initialBoxes) candy_cnt = 0 while len(deq) > 0: box = deq.popleft() candy_cnt += candies[box] for cb in containedBoxes[box]: reachable.add(cb) if cb not in visited and status[cb] == 1: deq.append(cb) visited.add(cb) for key in keys[box]: status[key] = 1 if key not in visited and key in reachable: deq.append(key) visited.add(key) return candy_cnt
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: found_keys = set() s = [b for b in initialBoxes if status[b]] found_boxes = set([b for b in initialBoxes if not status[b]]) r = 0 while s: box = s.pop() r += candies[box] for child in containedBoxes[box]: if status[child]: s.append(child) else: found_boxes.add(child) for key in keys[box]: found_keys.add(key) opened = found_boxes & found_keys for b in opened: s.append(b) found_boxes -= opened found_keys -= opened return r
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: foundBoxes = set(initialBoxes) openBoxes = [box for box in foundBoxes if status[box]==1] for i in openBoxes: for x in containedBoxes[i]: foundBoxes.add(x) if status[x] == 1: openBoxes.append(x) for x in keys[i]: if status[x] == 0 and x in foundBoxes: openBoxes.append(x) status[x] = 1 return sum([candies[i] for i in openBoxes])
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: N = len(status) queue = list() unopened = set() for i in initialBoxes: if status[i] == 1: queue.append(i) else: unopened.add(i) candiesCount = 0 while len(queue) > 0: top = queue.pop(0) candiesCount += candies[top] for k in keys[top]: status[k] = 1 if {k}.issubset(unopened): queue.append(k) unopened.remove(k) for c in containedBoxes[top]: if status[c] == 1: queue.append(c) else: unopened.add(c) return candiesCount
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
from collections import deque class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: q = deque(initialBoxes) ownedkeys = set() allkz = set() for k in keys: for l in k: allkz.add(l) # for i, b in enumerate(status): # if not b and b not in allkz: # unreachable.add(b) opened = set() for i, b in enumerate(status): if not b and i not in allkz: opened.add(i) res = 0 while q: curBox = q.popleft() if curBox not in opened: if status[curBox] or curBox in ownedkeys: opened.add(curBox) for k in keys[curBox]: ownedkeys.add(k) res += candies[curBox] q += containedBoxes[curBox] else: q.append(curBox) return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: n = len(status) visited = [0] * n unopened = [0] * n opened = [] ans = 0 for b in initialBoxes: if status[b] == 1: opened.append(b) visited[b] = 1 else: unopened[b] = 1 while opened: q2 = [] for b in opened: ans += candies[b] for bb in containedBoxes[b]: if visited[bb] == 0: if status[bb] == 1: q2.append(bb) else: unopened[bb] = 1 for k in keys[b]: status[k] = 1 if unopened[k] == 1 and visited[k] == 0: unopened[k] = 0 visited[k] = 1 q2.append(k) opened = q2 return ans def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: n = len(status) visited = [0] * n unopened = [0] * n opened = collections.deque() ans = 0 for b in initialBoxes: if status[b] == 1: opened.append(b) visited[b] = 1 else: unopened[b] = 1 while opened: b = opened.popleft() ans += candies[b] for bb in containedBoxes[b]: if visited[bb] == 0: if status[bb] == 1: opened.append(bb) else: unopened[bb] = 1 for k in keys[b]: status[k] = 1 if unopened[k] == 1 and visited[k] == 0: unopened[k] = 0 visited[k] = 1 opened.append(k) return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
from collections import deque class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: size = len(status) visited = set(initialBoxes) reachable = set() deq = deque(initialBoxes) candy_cnt = 0 while len(deq) > 0: box = deq.popleft() candy_cnt += candies[box] for cb in containedBoxes[box]: reachable.add(cb) if cb not in visited and status[cb] == 1: deq.append(cb) visited.add(cb) for key in keys[box]: status[key] = 1 if key not in visited and key in reachable: deq.append(key) visited.add(key) return candy_cnt
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: n = len(status) boxes = [] unboxes = set() for b in initialBoxes: if status[b]: boxes.append(b) else: unboxes.add(b) visited = set() got = 0 while boxes: ns = set() for box in boxes: if box not in visited: got += candies[box] visited.add(box) for key in keys[box]: status[key] |= 1 for box in boxes: for adj in containedBoxes[box]: unboxes.add(adj) newBoxes = set() # print(ns, unboxes) for box in unboxes: if status[box] and box not in visited: ns.add(box) newBoxes.add(box) for box in newBoxes: unboxes.remove(box) # print(ns, unboxes) boxes = list(ns) return got
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: n = len(status) hold = initialBoxes res = 0 key = set() flag = True while flag: nxt = [] flag = False for i in range(len(hold)): cur = hold.pop() if status[cur] == 1 or status[cur] == 0 and cur in key: flag = True res += candies[cur] for k in keys[cur]: key.add( k ) for b in containedBoxes[cur]: nxt.append(b) else: nxt.append(cur) hold = nxt return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: totalBox = len(status) gotBoxes = set(initialBoxes) # gotKeys = set() i = 0 ans = 0 visited = set() while i < len(initialBoxes): boxNow = initialBoxes[i] if (boxNow not in visited): if (status[boxNow] == 1): visited.add(boxNow) ans += candies[boxNow] # gotKeys |= set(keys[boxNow]) # gotBoxes |= set(containedBoxes[boxNow]) for newBox in containedBoxes[boxNow]: if status[newBox] == 1: initialBoxes.append(newBox) else: gotBoxes.add(newBox) for keyBox in keys[boxNow]: status[keyBox] = 1 if keyBox in gotBoxes: initialBoxes.append(keyBox) gotBoxes.remove(keyBox) else: gotBoxes.add(boxNow) i += 1 return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: boxes = set(initialBoxes) bfs = [i for i in boxes if status[i]] for i in bfs: for j in containedBoxes[i]: boxes.add(j) if status[j]: bfs.append(j) for j in keys[i]: if status[j] == 0 and j in boxes: bfs.append(j) status[j] = 1 return sum(candies[i] for i in bfs)
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): boxes = set(initialBoxes) bfs = [i for i in boxes if status[i]] for i in bfs: for j in containedBoxes[i]: boxes.add(j) if status[j]: bfs.append(j) for j in keys[i]: if status[j] == 0 and j in boxes: bfs.append(j) status[j] = 1 return sum(candies[i] for i in bfs)
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: total_candies = 0 open_boxes = [box for box in initialBoxes if status[box] == 1] locked_boxes = [box for box in initialBoxes if status[box] == 0] obtained_keys = set([]) while len(open_boxes): box = open_boxes.pop() total_candies += candies[box] for key in keys[box]: if key in locked_boxes: locked_boxes.remove(key) open_boxes.append(key) else: obtained_keys.add(key) for cbox in containedBoxes[box]: if status[cbox] == 1 or cbox in obtained_keys: open_boxes.append(cbox) else: locked_boxes.append(cbox) return total_candies
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: currkeys, ret, taken = set(), 0, [0] * len(status) q = collections.deque(initialBoxes) while q: l = len(q) opened = False for _ in range(l): curr = q.popleft() taken[curr] = 1 if curr not in currkeys and status[curr] == 0: q.append(curr) else: opened = True ret += candies[curr] if keys[curr]: currkeys |= set([key for key in keys[curr]]) for b in containedBoxes[curr]: if not taken[b]: q.append(b) if not opened: break return ret
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: if not initialBoxes: return 0 unused_keys = set() boxes = set([i for i in initialBoxes if status[i] == 0]) Q = collections.deque([i for i in initialBoxes if status[i] == 1]) res = 0 while Q: cur = Q.popleft() res += candies[cur] for k in keys[cur]: if k in boxes: Q.append(k) boxes.discard(k) else: unused_keys.add(k) for b in containedBoxes[cur]: if b in unused_keys or status[b] == 1: Q.append(b) unused_keys.discard(b) else: boxes.add(b) return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: N = len(status) scores = [0] * N for i in range(N): scores[i] += status[i] level = [] for b in initialBoxes: scores[b] += N + 1 if scores[b] >= N + 2: level.append(b) scores[b] = -sys.maxsize res = 0 while level: n_level = [] for b in level: res += candies[b] for k in containedBoxes[b]: scores[k] += N + 1 if scores[k] >= N + 2: n_level.append(k) scores[k] = -sys.maxsize for k in keys[b]: scores[k] += 1 if scores[k] >= N + 2: n_level.append(k) scores[k] = -sys.maxsize level = n_level return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: if not initialBoxes : return 0 visited = set() boxes=set() keys_found=set() n_candies=0 opn=[] # for i in range(len(initialBoxes)): for i in initialBoxes: if status[i]!=1: boxes.add(i) else: opn.append(i) initialBoxes=opn while initialBoxes : i=initialBoxes.pop(0) n_candies+=candies[i] visited.add(i) if keys[i] != []: for j in keys[i]: keys_found.add(j) if containedBoxes[i] != []: for c in containedBoxes[i]: if status[c] == 1: initialBoxes.append(c) else: boxes.add(c) for k in boxes: if k in visited: continue if k in keys_found: initialBoxes.append(k) visited.add(k) # print(visited) return n_candies
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: if not initialBoxes : return 0 visited = set() boxes=set() keys_found=set() n_candies=0 opn=[] # for i in range(len(initialBoxes)): for i in initialBoxes: if status[i]!=1: boxes.add(i) else: opn.append(i) initialBoxes=opn while initialBoxes : i=initialBoxes.pop(0) n_candies+=candies[i] visited.add(i) for j in keys[i]: keys_found.add(j) for c in containedBoxes[i]: if status[c] == 1: initialBoxes.append(c) else: boxes.add(c) for k in boxes: if k in visited: continue if k in keys_found: initialBoxes.append(k) visited.add(k) # print(visited) return n_candies
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: # box & key & open ret = 0 # status 0:close, 1:open, 2:opened # step 1: open boxes, if nothing can be opened, stop # step 2: update candies, new boxes, keys(open status) # repeat step 1 n = len(status) available_to_open = [box for box in initialBoxes if status[box]] reachable_boxes = set([box for box in initialBoxes if not status[box]]) while len(available_to_open) > 0: box = available_to_open.pop() if status[box] == 2: continue ret += candies[box] status[box] = 2 for containedBox in containedBoxes[box]: reachable_boxes.add(containedBox) for key in keys[box]: if status[key] == 0: status[key] = 1 for containedBox in containedBoxes[box]: if status[containedBox] == 1: available_to_open.append(containedBox) for key in keys[box]: if key in reachable_boxes: available_to_open.append(key) return ret return ret
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: open_box = [] # 有key有box close_box = set() # 有box没key key_set = set([i for i in range(len(status)) if status[i]==1]) # 有key没box visited = [False for _ in range(len(status))] count = 0 for box in initialBoxes: if status[box]==1: open_box.append(box) else: close_box.add(box) while open_box: box = open_box.pop() visited[box] = True count += candies[box] for key in keys[box]: key_set.add(key) for c_box in containedBoxes[box]: if visited[c_box]: continue else: close_box.add(c_box) for key in key_set: if key in close_box: open_box.append(key) close_box.remove(key) return count
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: #Approach 1: use set #The queue only contains boxes which is accessible q=collections.deque() #Important initiation,consider all opens box equivalent to they have keys keys_set=set([i for i in range(len(status)) if status[i]==1]) #Set for all available keys boxes_set=set(initialBoxes[:]) #set for all boxes that we have seen #Initialize the queue to process box for box in initialBoxes: if(box in keys_set): #Status[box]==1 is equivalent to it has a key q.append(box) res=0 visited=set() #More universal for all test cases, record all the boxes that have been processed #use DFS to populate the queue while q: boxi=q.popleft() #Avoid duplicated count if(boxi in visited): continue else: visited.add(boxi) res +=candies[boxi] #Check all neighbors of current boxi for nei in containedBoxes[boxi]: if(nei in keys_set): #it is either open or has key q.append(nei) boxes_set.add(nei) #Check all keys of current boxi for key in keys[boxi]: #Only append the box index wich has not been considered, because there is no key, but it is in the boxes_set if(key in boxes_set and (key not in keys_set)): q.append(key) keys_set.add(key) return res #Approach 2: use list # haskeys=status[:] # seen=[0]*len(status) # q=collections.deque() #Only contains boxes that are accessible # for box in initialBoxes: # seen[box]=1 # if(haskeys[box]): # q.append(box) #Include the one only accessible # res=0 # visited=set() # while q: # boxi=q.popleft() # if(boxi in visited): # continue # else: # visited.add(boxi) # res+=candies[boxi] # #Since eachbox is contained in one box at most # #it will not result in duplicated elements in q # for nei in containedBoxes[boxi]: # seen[nei]=1 # if(haskeys[nei]): q.append(nei) # #Iterate through all available keys, # #inlcude the box has been seen but doesn't have key to the queue # for key in keys[boxi]: # if(seen[key] and haskeys[key]==0): #Meaning it has not been included in queue # q.append(key) # haskeys[key]=1 # return res
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: # open_box: 有key有box # close_box: 有box没key # key_set: 有key没box # visited open_box = [] close_box = set() key_set = set([i for i in range(len(status)) if status[i]==1]) visited = [False for _ in range(len(status))] count = 0 for box in initialBoxes: if status[box]==1: open_box.append(box) else: close_box.add(box) while open_box: box = open_box.pop() visited[box] = True count += candies[box] for key in keys[box]: if visited[key]: continue if key in close_box: open_box.append(key) close_box.remove(key) else: key_set.add(key) for c_box in containedBoxes[box]: if visited[c_box]: continue elif c_box in key_set: open_box.append(c_box) else: close_box.add(c_box) for key in key_set: if key in close_box: open_box.append(key) close_box.remove(key) return count
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: locked_boxes = set() unused_keys = set() candy_count = 0 cur_box = [] for box in initialBoxes: if status[box] or box in unused_keys: candy_count += candies[box] for i in keys[box]: if not status[i]: if i not in locked_boxes: unused_keys.add(i) else: locked_boxes.remove(i) cur_box.append(i) for i in containedBoxes[box]: if not status[i] and i not in unused_keys: locked_boxes.add(i) else: cur_box.append(i) else: locked_boxes.add(box) while cur_box: box = cur_box.pop() candy_count += candies[box] for i in keys[box]: if not status[i]: if i not in locked_boxes: unused_keys.add(i) else: locked_boxes.remove(i) cur_box.append(i) for i in containedBoxes[box]: if not status[i] and i not in unused_keys: locked_boxes.add(i) else: cur_box.append(i) if i in unused_keys: unused_keys.remove(i) if box in unused_keys: unused_keys.remove(box) return candy_count
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): n = len(status) access = [0] * n source = [] visited = set() # (access, status, key, i) for i in initialBoxes: access[i] = 1 visited.add(i) source.append(i) ans = 0 while source: i = source.pop() ans += candies[i] for j in containedBoxes[i]: access[j] = 1 for j in keys[i]: status[j] = 1 for j in containedBoxes[i]: if j not in visited and status[j]: visited.add(j) source.append(j) for j in keys[i]: if j not in visited and access[j]: visited.add(j) source.append(j) return ans
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int]) -> int: self.keys_found = set() self.boxes_found = set() self.boxes_found.update(initialBoxes) n = len(status) visited = [0] * n for box in initialBoxes: self.dfs(box, keys, containedBoxes, visited) ans = 0 for i in range(n): if i in self.boxes_found and (i in self.keys_found or status[i] == 1): ans += candies[i] return ans def dfs(self, cur, keys, containedBoxes, visited): if visited[cur] == 1: return visited[cur] = 1 boxes = containedBoxes[cur] self.keys_found.update(keys[cur]) self.boxes_found.update(boxes) for box in boxes: self.dfs(box, keys, containedBoxes, visited)