description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) count = 0 i, j = 0, len(people) - 1 while 0 <= i < j < len(people): while 0 <= i < j < len(people) and people[i] + people[j] > limit: j -= 1 count += 1 i += 1 j -= 1 count += 1 if i == j: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER VAR VAR FUNC_CALL VAR VAR WHILE NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: q = collections.deque(sorted(people)) ans = 0 while len(q) > 1: i, x = q.pop(), q[0] if i + x <= limit: q.popleft() ans += 1 return ans + (1 if q else 0)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: s = [] c = 0 people.sort(reverse=True) for i in range(len(people)): if s and people[i] < s[-1]: s[-1] = s[-1] - people[i] s.pop() elif s and people[i] == s[-1]: s.pop() elif s and people[i] > s[-1]: s.append(limit - people[i]) c += 1 elif people[i] == limit: c += 1 else: c += 1 s.append(limit - people[i]) return c
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() low = 0 up = len(people) - 1 boats = 0 while low <= up: if up - 1 >= low and people[up] + people[up - 1] <= limit: up -= 2 boats += 1 elif up != low and people[up] + people[low] <= limit: up -= 1 low += 1 boats += 1 else: up -= 1 boats += 1 return boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() n = len(people) i = 0 j = n - 1 boats = 0 while i <= j: if people[i] + people[j] <= limit: i += 1 j -= 1 boats += 1 return boats
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: groups = self.group_people_by_weight(people, limit) return self.count_num_boats(groups, limit) def group_people_by_weight(self, people, limit): groups = [0] * (limit + 1) for person_weight in people: groups[person_weight] += 1 return groups def count_num_boats(self, groups, limit): num_boats = 0 start = 0 end = len(groups) - 1 while start <= end: while start <= end and groups[start] <= 0: start += 1 while start <= end and groups[end] <= 0: end -= 1 if start > end: break if start + end <= limit: groups[start] -= 1 groups[end] -= 1 num_boats += 1 return num_boats
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people, limit): people.sort() i, j = 0, len(people) - 1 ans = 0 while i <= j: ans += 1 if people[i] + people[j] <= limit: i += 1 j -= 1 return ans def numRescueBoats2(self, people: List[int], limit: int) -> int: self.ans = 9999 visited = [(0) for _ in range(len(people) + 1)] def traverse(ind: int, boatCnt: int, boats): if visited[ind]: return if ind == len(people): if boatCnt + 1 < self.ans: print(boats) self.ans = boatCnt + 1 else: visited[ind] = 1 for i in range(ind + 1, len(people) + 1): b = boats[:] if boatCnt >= len(b): b.append([]) if sum(b[boatCnt]) + people[ind] > limit: boatCnt += 1 if boatCnt >= len(b): b.append([]) b[boatCnt].append(people[ind]) traverse(ind + 1, boatCnt, b) visited[ind] = 0 traverse(0, 0, []) return self.ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF VAR VAR IF VAR VAR RETURN IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER LIST RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], cap: int) -> int: people.sort() N = len(people) i, j = 0, N - 1 ans = 0 k = cap s = 2 while i <= j: while s and j >= 0: if k - people[j] < 0: break k -= people[j] j -= 1 s -= 1 while s and i < N: if k - people[i] < 0: break k -= people[i] i += 1 s -= 1 k = cap s = 2 ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: less_than_half = dict() more_than_half = dict() half = 0 total_boats = 0 for person in people: diff = limit - person if diff == 0: total_boats += 1 continue if person < limit / 2: if diff in more_than_half: total_boats += 1 if more_than_half[diff] == 1: del more_than_half[diff] else: more_than_half[diff] -= 1 elif person in less_than_half: less_than_half[person] += 1 else: less_than_half[person] = 1 elif person > limit / 2: if diff in less_than_half: total_boats += 1 if less_than_half[diff] == 1: del less_than_half[diff] else: less_than_half[diff] -= 1 elif person in more_than_half: more_than_half[person] += 1 else: more_than_half[person] = 1 elif half == 1: total_boats += 1 half = 0 else: half = 1 less_keys = sorted(less_than_half.keys()) more_keys = sorted(list(more_than_half.keys()), reverse=True) while len(less_keys) and len(more_keys): if less_keys[0] + more_keys[0] <= limit: if less_than_half[less_keys[0]] < more_than_half[more_keys[0]]: total_boats += less_than_half[less_keys[0]] more_than_half[more_keys[0]] -= less_than_half[less_keys[0]] less_keys.pop(0) elif less_than_half[less_keys[0]] > more_than_half[more_keys[0]]: total_boats += more_than_half[more_keys[0]] less_than_half[less_keys[0]] -= more_than_half[more_keys[0]] more_keys.pop(0) else: total_boats += less_than_half[less_keys[0]] less_keys.pop(0) more_keys.pop(0) else: total_boats += more_than_half[more_keys[0]] more_keys.pop(0) less_total = 0 for k in less_keys: less_total += less_than_half[k] more_total = 0 for k in more_keys: more_total += more_than_half[k] total_boats += (less_total + half + 1) // 2 + more_total return total_boats
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: ret = 0 people.sort(reverse=True) start = len(people) - 1 for i in range(len(people)): if start >= i: ret += 1 temp_lim = limit - people[i] for j in range(start, i, -1): if people[j] <= temp_lim: start = j - 1 break elif people[j] > temp_lim: start = j break print(temp_lim) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() if people[0] >= limit: return 0 res = [0] i, j = 0, len(people) - 1 num_people = 0 while i <= j: if res[-1] + people[j] <= limit and num_people < 2: res[-1] += people[j] j -= 1 num_people += 1 elif res[-1] + people[i] <= limit and num_people < 2: res[-1] += people[i] i += 1 num_people += 1 else: res.append(0) num_people = 0 return len(res)
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: n = len(people) people.sort() print(people) i = 0 j = n - 1 re = [] c = 0 tmp = [] while i <= j: if people[i] + people[j] > limit: c += 1 tmp.append(people[j]) re.append(tmp) tmp = [] j = j - 1 else: tmp.append(people[i]) tmp.append(people[j]) re.append(tmp) tmp = [] i = i + 1 j = j - 1 print(re) return len(re)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: total = 0 people.sort() print(people) i = 0 j = len(people) - 1 while i <= j: print(j) if people[i] + people[j] > limit or i == j: j -= 1 total += 1 else: i += 1 j -= 1 total += 1 return total
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by a boat.)   Example 1: Input: people = [1,2], limit = 3 Output: 1 Explanation: 1 boat (1, 2) Example 2: Input: people = [3,2,2,1], limit = 3 Output: 3 Explanation: 3 boats (1, 2), (2) and (3) Example 3: Input: people = [3,5,3,4], limit = 5 Output: 4 Explanation: 4 boats (3), (3), (4), (5) Note: 1 <= people.length <= 50000 1 <= people[i] <= limit <= 30000
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) l, r = 0, len(people) - 1 while l <= r: if people[l] + people[r] <= limit: r -= 1 l += 1 return l
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = list(map(int, input().split())) a = list(map(int, input().split())) a = [[a[i], i] for i in range(n)] a.sort() b = [0] * n c = [] e = 0 for i in range(n): if i == 0: b[a[i][1]] = 1 c.append([1, a[i][0]]) e += 1 elif a[i][0] - c[0][1] > d: c[0][1] = a[i][0] b[a[i][1]] = c[0][0] j = 0 while j * 2 + 1 < e and ( c[j][1] > c[j * 2 + 1][1] or j * 2 + 2 < e and c[j][1] > c[j * 2 + 2][1] ): if 2 * j + 2 >= e or c[2 * j + 1][1] < c[2 * j + 2][1]: c[j], c[2 * j + 1] = list(c[2 * j + 1]), list(c[j]) j = 2 * j + 1 else: c[j], c[2 * j + 2] = list(c[2 * j + 2]), list(c[j]) j = 2 * j + 2 else: e += 1 c.append([e, a[i][0]]) b[a[i][1]] = e print(e) print(*b)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = map(int, input().split()) a = list(map(int, input().split())) l1 = [[a[i], i] for i in range(n)] l1.sort() ans = [0] * n j, k = 0, 0 for i in range(n): if l1[i][0] - l1[j][0] <= d: k += 1 ans[l1[i][1]] = k else: ans[l1[i][1]] = ans[l1[j][1]] j += 1 print(k) print(*ans, sep=" ")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = map(int, input().split()) a = list(map(int, input().split())) id = {a[i]: i for i in range(n)} a.sort() def Solve(x): for i in range(x, n): if a[i] - a[i - x] <= d: return False return True l = 1 r = n while l < r: mid = int((l + r) / 2) if Solve(mid): r = mid else: l = mid + 1 ans = [(0) for i in range(n)] for i in range(n): ans[id[a[i]]] = i % l + 1 print(l) print(" ".join(map(str, ans)))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = map(int, input().split()) a = list(map(int, input().split())) o = [0] * n x = [(a[i], i) for i in range(n)] x.sort() u = 0 i = j = 0 while i < n: if x[i][0] - d <= x[j][0]: u += 1 o[x[i][1]] = u else: o[x[i][1]] = o[x[j][1]] j += 1 i += 1 print(u) print(" ".join(list(map(str, o))))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = map(int, input().split()) a = list(map(int, input().split())) a = sorted([(a[i], i) for i in range(n)]) right = 0 left = 0 day = 0 ret = [0] * n while right < n: if a[right][0] - a[left][0] <= d: day += 1 ret[a[right][1]] = day else: ret[a[right][1]] = ret[a[left][1]] left += 1 right += 1 print(day) print(*ret)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = map(int, input().split()) b = sorted([[int(s), i] for i, s in enumerate(input().split())]) ans = [0] * n day = 0 i = j = 0 while i < n: if b[i][0] - d <= b[j][0]: day += 1 ans[b[i][1]] = day else: ans[b[i][1]] = ans[b[j][1]] j += 1 i += 1 print(day) print(*ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = list(map(int, input().split())) arr = list(map(int, input().split())) arrx = [] for i in range(n): arrx.append((arr[i], i)) arrx.sort() arr1 = [0] * n arr1[arrx[0][1]] = 1 count = 1 index = 0 for i in range(1, n): if arrx[i][0] >= arrx[index][0] + d + 1: arr1[arrx[i][1]] = arr1[arrx[index][1]] index += 1 else: count += 1 arr1[arrx[i][1]] = count print(count) print(*arr1)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
from sys import stdin inp = stdin.readline n, m, d = list(map(int, inp().split())) a = [(int(minute), index) for index, minute in enumerate(inp().split())] ans = [0] * n a.sort() ans[a[0][1]] = 1 max_cur_day = 1 j = 0 count = 1 for i in range(1, n): if a[i][0] - a[j][0] > d: ans[a[i][1]] = ans[a[j][1]] j += 1 else: count += 1 ans[a[i][1]] = count print(count) print(*ans)
ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
def check(p): for i in range(n - p): if a[i + p] - a[i] - 1 < d: return False return True def binSearch(a, b): left, right = a - 1, b + 1 while right - left > 1: mid = (left + right) // 2 if check(mid): right = mid else: left = mid return right n, m, d = map(int, input().split()) a = list(map(int, input().split())) a_ind = {i: a[i] for i in range(n)} a.sort() b = binSearch(1, n) ans = {el: (0) for el in a} for i, el in enumerate(a, 1): ans[el] = (i - 1) % b + 1 print(b) print(" ".join(map(str, [ans[a_ind[i]] for i in range(n)])))
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, d = list(map(int, input().split())) breaks = list(map(int, input().split())) breaks_S = sorted(breaks) index_s = [i[0] for i in sorted(enumerate(breaks), key=lambda x: x[1])] d += 1 answer = [-1] * n x = 1 for i in range(n): while True: if i >= x and breaks_S[i] - breaks_S[i - x] < d: x += 1 elif n - i > x and breaks_S[i + x] - breaks_S[i] < d: x += 1 else: break for i in range(n): answer[index_s[i]] = i % x + 1 print(x) print(*answer)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
R = lambda: map(int, input().split()) n, m, d = R() a = sorted((x, i) for i, x in enumerate(R())) res = [-1] * len(a) res[0] = 0 cnt = 0 l = 0 for r in range(1, n): if a[r][0] - d <= a[l][0]: cnt += 1 res[r] = cnt else: res[r] = res[l] l += 1 print(max(res) + 1) for t in sorted(zip(a, res), key=lambda x: x[0][1]): print(t[1] + 1, end=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER STRING
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
a, b, c = map(int, input().split()) l = list(map(int, input().split())) l1 = [(l[i], i) for i in range(a)] otv = [0] * a l1 = sorted(l1, key=lambda x: x[0]) i = j = 0 k = 0 for i in range(a): if l1[i][0] - c <= l1[j][0]: k += 1 otv[l1[i][1]] = k else: otv[l1[i][1]] = otv[l1[j][1]] j += 1 print(k) print(" ".join(map(str, otv)))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n, m, d = lst() b = lst() a = sorted(b) ans = {} day = 1 p = 0 for i in range(n): v = a[i] if v - a[p] > d: ans[v] = ans[a[p]] p += 1 else: ans[v] = day day += 1 print(day - 1) for v in b: print(ans[v], end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, k = list(map(int, input().split())) l = list(map(int, input().split())) for i in range(n): l[i] = [l[i], i] l.sort() uk1 = 1 uk2 = n def pos(n): cnt = [-10000000001] * len(l) nonlocal k for i in range(len(l)): if l[i][0] - cnt[i % n] <= k: return False else: cnt[i % n] = l[i][0] return True while uk2 - uk1 > 1: if pos((uk2 + uk1) // 2): uk2 = (uk1 + uk2) // 2 else: uk1 = (uk1 + uk2) // 2 ans = [0] * n if not pos(uk1): for i in range(n): ans[l[i][1]] = i % uk2 + 1 print(uk2) for i in range(n): print(ans[i], end=" ") else: for i in range(n): ans[l[i][1]] = i % uk1 + 1 print(uk1) for i in range(n): print(ans[i], end=" ")
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER WHILE BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Recently Monocarp got a job. His working day lasts exactly $m$ minutes. During work, Monocarp wants to drink coffee at certain moments: there are $n$ minutes $a_1, a_2, \dots, a_n$, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute). However, Monocarp's boss doesn't like when Monocarp takes his coffee breaks too often. So for the given coffee break that is going to be on minute $a_i$, Monocarp must choose the day in which he will drink coffee during the said minute, so that every day at least $d$ minutes pass between any two coffee breaks. Monocarp also wants to take these $n$ coffee breaks in a minimum possible number of working days (he doesn't count days when he is not at work, and he doesn't take coffee breaks on such days). Take into account that more than $d$ minutes pass between the end of any working day and the start of the following working day. For each of the $n$ given minutes determine the day, during which Monocarp should take a coffee break in this minute. You have to minimize the number of days spent. -----Input----- The first line contains three integers $n$, $m$, $d$ $(1 \le n \le 2\cdot10^{5}, n \le m \le 10^{9}, 1 \le d \le m)$ — the number of coffee breaks Monocarp wants to have, the length of each working day, and the minimum number of minutes between any two consecutive coffee breaks. The second line contains $n$ distinct integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le m)$, where $a_i$ is some minute when Monocarp wants to have a coffee break. -----Output----- In the first line, write the minimum number of days required to make a coffee break in each of the $n$ given minutes. In the second line, print $n$ space separated integers. The $i$-th of integers should be the index of the day during which Monocarp should have a coffee break at minute $a_i$. Days are numbered from $1$. If there are multiple optimal solutions, you may print any of them. -----Examples----- Input 4 5 3 3 5 1 2 Output 3 3 1 1 2 Input 10 10 1 10 5 7 4 6 3 2 1 9 8 Output 2 2 1 1 2 2 1 2 1 1 2 -----Note----- In the first example, Monocarp can take two coffee breaks during the first day (during minutes $1$ and $5$, $3$ minutes will pass between these breaks). One break during the second day (at minute $2$), and one break during the third day (at minute $3$). In the second example, Monocarp can determine the day of the break as follows: if the minute when he wants to take a break is odd, then this break is on the first day, if it is even, then this break is on the second day.
n, m, D = map(int, input().split()) lst = [*map(int, input().split())] d = {x: i for i, x in enumerate(lst)} lst.sort() res, j, result = [0] * n, 0, 0 for i, x in enumerate(lst): if x - lst[j] > D: res[d[x]] = res[d[lst[j]]] j += 1 else: result += 1 res[d[x]] = result print(result) print(*res)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: results = [] end = len(arr) while end > 1: max_idx = arr.index(max(arr[:end])) if max_idx == end - 1: end -= 1 else: if max_idx > 0: results.append(max_idx + 1) arr[: max_idx + 1] = reversed(arr[: max_idx + 1]) results.append(end) arr[:end] = reversed(arr[:end]) end -= 1 print(arr) return results
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: def flip(sublist, k): i = 0 while i < k / 2: sublist[i], sublist[k - i - 1] = sublist[k - i - 1], sublist[i] i += 1 ans = [] value_to_sort = len(arr) while value_to_sort > 0: index = arr.index(value_to_sort) if index != value_to_sort - 1: if index != 0: ans.append(index + 1) flip(arr, index + 1) ans.append(value_to_sort) flip(arr, value_to_sort) value_to_sort -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, A: List[int]) -> List[int]: def flip(r): l = 0 while l < r: A[l], A[r] = A[r], A[l] l, r = l + 1, r - 1 ans = [] for x in range(len(A), 0, -1): i = A.index(x) + 1 ans.extend([i, x]) flip(i - 1) flip(x - 1) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, A: List[int]) -> List[int]: N = len(A) res = [] for x in range(N, 0, -1): i = A.index(x) res.extend([i + 1, x]) A = A[:i:-1] + A[:i] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: res = [] for x in range(len(arr), 1, -1): i = arr.index(x) res.extend([i + 1, x]) arr = arr[:i:-1] + arr[:i] return res out = [] for i in range(len(arr), 0, -1): if arr[i - 1] != i: j = arr.index(i) out.extend([j + 1, i]) arr[: j + 1] = reversed(arr[: j + 1]) arr[:i] = reversed(arr[:i]) return out
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: n = len(arr) ans = [] def flip(idx): arr[: idx + 1] = arr[: idx + 1][::-1] for i in range(len(arr)): maxInd = arr[:n].index(max(arr[:n])) if maxInd != n: ans.append(maxInd + 1) ans.append(n) flip(maxInd) flip(n - 1) n -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: last = len(arr) ans = [] while last > 1: max_pos = 0 max_v = -float("inf") for i in range(last): if arr[i] > max_v: max_v = arr[i] max_pos = i ans.append(max_pos + 1) ans.append(last) arr[: max_pos + 1] = arr[: max_pos + 1][::-1] arr[:last] = arr[:last][::-1] last -= 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Given an array of integers arr, sort the array by performing a series of pancake flips. In one pancake flip we do the following steps: Choose an integer k where 1 <= k <= arr.length. Reverse the sub-array arr[1...k]. For example, if arr = [3,2,1,4] and we performed a pancake flip choosing k = 3, we reverse the sub-array [3,2,1], so arr = [1,2,3,4] after the pancake flip at k = 3. Return the k-values corresponding to a sequence of pancake flips that sort arr. Any valid answer that sorts the array within 10 * arr.length flips will be judged as correct.   Example 1: Input: arr = [3,2,4,1] Output: [4,2,4,3] Explanation: We perform 4 pancake flips, with k values 4, 2, 4, and 3. Starting state: arr = [3, 2, 4, 1] After 1st flip (k = 4): arr = [1, 4, 2, 3] After 2nd flip (k = 2): arr = [4, 1, 2, 3] After 3rd flip (k = 4): arr = [3, 2, 1, 4] After 4th flip (k = 3): arr = [1, 2, 3, 4], which is sorted. Notice that we return an array of the chosen k values of the pancake flips. Example 2: Input: arr = [1,2,3] Output: [] Explanation: The input is already sorted, so there is no need to flip anything. Note that other answers, such as [3, 3], would also be accepted.   Constraints: 1 <= arr.length <= 100 1 <= arr[i] <= arr.length All integers in arr are unique (i.e. arr is a permutation of the integers from 1 to arr.length).
class Solution: def pancakeSort(self, arr: List[int]) -> List[int]: n = len(arr) ans = [] for i in range(n, 0, -1): for j in range(i - 1): if arr[j] == i: ans.append(j + 1) ans.append(i) print((j, i)) arr = list(reversed(arr[: j + 1])) + arr[j + 1 :] print(arr) arr = list(reversed(arr[:i])) + arr[i:] print(arr) break return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
def maximum_profit_for_difficulty(diff_job, difficulty, profit): return max([profit[job] for job in diff_job[difficulty]]) class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: min_difficulty = min(difficulty) max_difficulty = max(difficulty) least_difficult_job = difficulty.index(min_difficulty) diff_job = defaultdict(list) for i, diff in enumerate(difficulty): diff_job[diff].append(i) max_profit = [maximum_profit_for_difficulty(diff_job, min_difficulty, profit)] for i in range(1, max_difficulty - min_difficulty + 1): current_difficulty = i + min_difficulty if current_difficulty in diff_job: current_job_profit = maximum_profit_for_difficulty( diff_job, current_difficulty, profit ) max_profit.append(max(max_profit[i - 1], current_job_profit)) else: max_profit.append(max_profit[i - 1]) result = 0 for ability in worker: if ability >= max_difficulty: result += max_profit[-1] elif ability < min_difficulty: result += 0 else: result += max_profit[ability - min_difficulty] return result
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, difficulty, profit, worker): data = sorted([(d, p) for d, p in zip(difficulty, profit)]) worker.sort() i, n, money = 0, len(data), 0 res = 0 for w in worker: while i < n and w >= data[i][0]: money = max(money, data[i][1]) i += 1 res += money return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: ls = sorted(range(len(difficulty)), key=lambda x: difficulty[x]) workers = sorted(worker) idx = 0 resp = 0 best = 0 for worker in workers: while idx < len(ls) and worker >= difficulty[ls[idx]]: best = max(best, profit[ls[idx]]) idx += 1 resp += best return resp
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: for i in range(len(profit)): difficulty[i] = [difficulty[i], profit[i]] difficulty.sort() for i in range(1, len(profit)): difficulty[i][1] = max(difficulty[i - 1][1], difficulty[i][1]) def search(d): if d < difficulty[0][0]: return -1 l = 0 r = len(profit) - 1 while l < r: m = (l + r + 1) // 2 if difficulty[m][0] <= d: l = m else: r = m - 1 return l res = 0 for w in worker: i = search(w) if i >= 0: res += difficulty[i][1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FUNC_DEF IF VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: worker.sort() arr = [] for i in range(len(profit)): arr.append([difficulty[i], profit[i]]) arr.sort(key=lambda x: x[0]) for i in range(len(arr)): difficulty[i] = arr[i][0] profit[i] = arr[i][1] prev = [profit[0]] for i in range(1, len(profit)): prev.append(max(profit[i], prev[-1])) i = 0 p = 0 j = 0 while j < len(worker): if i == len(difficulty): p += prev[i - 1] j += 1 continue if difficulty[i] <= worker[j]: i += 1 else: x = i - 1 while x >= 0 and difficulty[x] > worker[j]: x -= 1 if x >= 0: p += prev[x] j += 1 return p
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: job = {} for cost, gain in zip(difficulty, profit): if cost in job: job[cost] = max(job[cost], gain) else: job[cost] = gain job = [(k, v) for k, v in list(job.items())] + [(0, 0)] job.sort() useless = set() global_max = -float("inf") for i, (c, g) in enumerate(job): if g < global_max: useless.add(i) else: global_max = g job = [job[i] for i in range(len(job)) if i not in useless] print(job) worker.sort() i, j = 0, 0 ans = 0 while j < len(worker): while job[i][0] < worker[j] and i < len(job) - 1: i += 1 ans += job[i][1] if job[i][0] <= worker[j] else job[i - 1][1] j += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = sorted(zip(difficulty, profit)) worker.sort() m = len(worker) n = len(jobs) j = 0 max_profit = 0 result = 0 for i in range(m): ability = worker[i] while j < n and jobs[j][0] <= ability: max_profit = max(max_profit, jobs[j][1]) j += 1 result += max_profit return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: d = sorted([(difficulty[i], profit[i]) for i in range(len(difficulty))]) c = [0] * 100005 t = 0 for i in range(1, 100005): c[i] = c[i - 1] while t < len(d) and i == d[t][0]: c[i] = max(c[i], d[t][1]) t += 1 return sum([c[i] for i in worker])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = [] total = 0 for i in range(len(profit)): jobs.append([difficulty[i], profit[i]]) print(jobs) worker.sort() jobs.sort() i = 0 best = 0 for work in worker: while i < len(jobs) and work >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 total += best return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: m = max(difficulty) dp = [0] * m for d, p in zip(difficulty, profit): dp[d - 1] = max(dp[d - 1], p) for i in range(1, len(dp)): dp[i] = max(dp[i], dp[i - 1]) return sum([dp[min(w - 1, m - 1)] for w in worker])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: make = [0] * 10**5 num = [(difficulty[i], profit[i]) for i in range(len(profit))] num.sort(key=lambda x: x[0]) x = 0 while x < len(num): if x + 1 < len(num): if num[x][1] > num[x + 1][1]: num.pop(x + 1) else: x += 1 else: break for i in range(len(num)): make[num[i][0]] = num[i][1] pre = -1 for i in range(len(make)): if make[i] != 0: pre = make[i] elif pre != -1: make[i] = pre ans = 0 for w in worker: ans += make[w] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = sorted(zip(difficulty, profit), key=lambda t: t[0]) ans = i = best = 0 for w in sorted(worker): while i < len(jobs) and w >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: rel = zip(difficulty, profit) rel = sorted(rel) output = 0 worker.sort() mx = 0 j = 0 for i in worker: while j < len(rel) and i >= rel[j][0]: mx = max(mx, rel[j][1]) j += 1 print(i, mx) output += mx return output
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, d: List[int], p: List[int], work: List[int]) -> int: N, maxd = len(p), max(d) cmb = [(d[i], p[i]) for i in range(N)] cmb.sort() dd = [0] * (maxd + 1) val = j = 0 for i in range(maxd + 1): while j < N and i >= cmb[j][0]: val = max(val, cmb[j][1]) j += 1 dd[i] = val ans = 0 for w in work: if w > maxd: ans += dd[maxd] else: ans += dd[w] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution(object): def maxProfitAssignment(self, difficulty, profit, worker): arr = [] for d, p in sorted(zip(difficulty, profit)): if arr and arr[-1][1] >= p: continue arr.append([d, p]) N = len(arr) res, i, best = 0, 0, 0 for w in sorted(worker): while i < N and w >= arr[i][0]: best = arr[i][1] i += 1 res += best return res
CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: mapProfit = dict() for i in range(len(difficulty)): if difficulty[i] in mapProfit: mapProfit[difficulty[i]] = max(mapProfit[difficulty[i]], profit[i]) else: mapProfit[difficulty[i]] = profit[i] l = list() maxDiff = max(difficulty) for i in range(maxDiff + 1): if i in mapProfit: l.append(mapProfit[i]) else: l.append(0) maxFromBegin = 0 for i in range(len(l)): if l[i] < maxFromBegin: l[i] = maxFromBegin else: maxFromBegin = l[i] res = 0 for work in worker: if work > maxDiff: res += l[-1] else: res += l[work] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: bfb = [0] * max(max(worker), max(difficulty)) for diff, prof in sorted(zip(difficulty, profit)): bfb[diff - 1] = max(bfb[diff - 1], prof) currMax = 0 for i in range(len(bfb)): bfb[i] = max(currMax, bfb[i]) currMax = bfb[i] total = 0 for w in worker: total += bfb[w - 1] return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: ws = sorted(worker, reverse=True) dp = sorted(zip(difficulty, profit), key=lambda x: x[1], reverse=True) i = 0 total = 0 for w in ws: while dp[i][0] > w: i = i + 1 if i >= len(dp): return total total = total + dp[i][1] return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: for i in range(len(difficulty)): difficulty[i] = difficulty[i], profit[i] difficulty.sort(key=lambda x: x[0]) i, L = 0, len(difficulty) ans, most = 0, 0 for wker in sorted(worker): while i < L and difficulty[i][0] <= wker: most = max(most, difficulty[i][1]) i += 1 ans += most return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: output = 0 job = sorted(zip(difficulty, profit), reverse=True, key=lambda x: x[1]) worker = sorted(worker, reverse=True) jobIndex = 0 for workerCab in worker: while jobIndex < len(job): if job[jobIndex][0] <= workerCab: break else: jobIndex += 1 if jobIndex < len(job): output = output + job[jobIndex][1] return output
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: mDifficulty = {} for i in range(len(difficulty)): d = difficulty[i] if difficulty[i] not in mDifficulty: mDifficulty[d] = 0 mDifficulty[d] = max(mDifficulty[d], profit[i]) jobs = [] for key, value in mDifficulty.items(): jobs.append([key, value]) jobs.sort(key=lambda job: job[0]) for i in range(1, len(jobs)): jobs[i][1] = max(jobs[i][1], jobs[i - 1][1]) worker.sort() start = len(jobs) - 1 count = 0 for i in range(len(worker) - 1, -1, -1): while worker[i] < jobs[start][0] and start >= 0: start -= 1 if start < 0: break count += jobs[start][1] return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: max_diff = max(difficulty) max_profits = [0] * (max_diff + 1) for diff, profit in zip(difficulty, profit): max_profits[diff] = max(max_profits[diff], profit) for i in range(1, max_diff + 1): max_profits[i] = max(max_profits[i], max_profits[i - 1]) pft = 0 for wskill in worker: pft += max_profits[max_diff if wskill > max_diff else wskill] return pft
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = sorted( [(difficulty[i], profit[i]) for i in range(len(difficulty))], key=lambda x: x[0], ) worker = sorted(worker) jobPointer = 0 workerPointer = 0 maxProfit = 0 totalProfit = 0 while True: if workerPointer >= len(worker): break talent = worker[workerPointer] if jobPointer >= len(jobs) or jobs[jobPointer][0] > talent: totalProfit += maxProfit workerPointer += 1 else: if jobs[jobPointer][1] > maxProfit: maxProfit = jobs[jobPointer][1] jobPointer += 1 return totalProfit
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: best = 0 tot = 0 worker.sort() for d, p in sorted(zip(difficulty, profit)): while worker and d > worker[0]: tot += best worker.pop(0) best = max(best, p) tot += best * len(worker) return tot
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: tup = [] dic = {} for i in range(0, len(profit)): tup.append((profit[i], difficulty[i])) worker.sort(reverse=True) tup.sort(reverse=True) indx = 0 t_indx = 0 res = 0 while indx < len(worker): while t_indx < len(profit) and worker[indx] < tup[t_indx][1]: t_indx += 1 if t_indx < len(profit): res += tup[t_indx][0] indx += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: def find_difficulty(d, w): n = len(d) lt, rt = 0, n while lt < rt: mid = lt + (rt - lt) // 2 if d[mid] == w: return mid elif d[mid] > w: rt = mid else: lt = mid + 1 return rt - 1 difficulty1 = sorted(difficulty) difficulty = list(zip(difficulty, profit)) difficulty.sort(key=lambda x: x[0]) profit = {} max_profit = 0 for i in range(len(difficulty)): if ( difficulty[i][0] not in profit or profit[difficulty[i][0]] < difficulty[i][1] ): profit[difficulty[i][0]] = difficulty[i][1] max_profit = max(max_profit, profit[difficulty[i][0]]) profit[difficulty[i][0]] = max_profit total = 0 pre = -1 for i in range(len(worker)): pre = find_difficulty(difficulty1, worker[i]) if pre != -1: total += profit[difficulty1[pre]] return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: val_dict = dict() for i in range(len(difficulty)): val_dict[difficulty[i]] = max(val_dict.get(difficulty[i], 0), profit[i]) key_list = list(val_dict.keys()) key_list = sorted(key_list) max_val = 0 max_list = [0] * len(key_list) for i in range(len(key_list)): max_val = max(max_val, val_dict[key_list[i]]) max_list[i] = max_val total = 0 for w in worker: total += self.bisect(w, key_list, max_list) return total def bisect(self, w, key_list, max_list): left, right = 0, len(key_list) - 1 if w < key_list[0]: return 0 if w >= key_list[right]: return max_list[right] while left != right - 1: mid = (left + right) // 2 if w > key_list[mid]: left = mid elif w < key_list[mid]: right = mid else: return max_list[mid] return max_list[left]
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: max_v = max(max(difficulty), max(worker)) n = len(difficulty) best = [0] * (max_v + 1) for i in range(n): d = difficulty[i] p = profit[i] best[d] = max(best[d], p) for i in range(1, max_v + 1): best[i] = max(best[i - 1], best[i]) s = 0 for d in worker: s += best[d] return s
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: dp_dict = dict(list(zip(difficulty, profit))) l = len(difficulty) for idx in range(l): d = difficulty[idx] if profit[idx] > dp_dict[d]: dp_dict[d] = profit[idx] dp_items = sorted(list(dp_dict.items()), key=lambda x: x[0]) darr = [x[0] for x in dp_items] max_p = 0 parr = list() for d, p in dp_items: if p > max_p: max_p = dp_dict[d] parr.append(max_p) ret = 0 def get_nearest(w, arr): l = len(arr) if w < arr[0]: return None if w > arr[-1]: return l - 1 def binary_search(w, i, j): if i >= l or j >= l or w < arr[i] or w > arr[j]: return None if w == arr[i]: return i if w == arr[j]: return j k = (i + j) // 2 if arr[k] <= w < arr[k + 1]: return k prev = binary_search(w, i, k) return binary_search(w, k + 1, j) if prev is None else prev return binary_search(w, 0, l - 1) for w in worker: floor_idx = get_nearest(w, darr) if floor_idx is not None: ret += parr[floor_idx] return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NONE IF VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NONE IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NONE FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: worker.sort() difprof = [(difficulty[i], profit[i]) for i in range(len(difficulty))] difprof.sort() total = 0 index = 0 maxProfit = 0 for w in worker: while index < len(difprof) and w >= difprof[index][0]: maxProfit = max(maxProfit, difprof[index][1]) index += 1 total += maxProfit return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = list(zip(difficulty, profit)) jobs.sort(key=lambda x: x[1], reverse=True) jobs.sort(key=lambda x: x[0]) bestjobs = [] res = 0 for a, b in jobs: if not bestjobs: bestjobs.append((a, b)) elif bestjobs[-1][1] < b: bestjobs.append((a, b)) print(bestjobs) for wo in worker: lo, hi = 0, len(bestjobs) - 1 while lo < hi: mid = (lo + hi + 1) // 2 if bestjobs[mid][0] > wo: hi = mid - 1 else: lo = mid if bestjobs[lo][0] <= wo: res += bestjobs[lo][1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: def findProfit(ppl, goodJobs): l = 0 r = len(goodJobs) - 1 while l < r - 1: test = int((l + r) / 2) if goodJobs[test][1] > ppl: l = test else: r = test if goodJobs[r][1] > ppl: return 0 elif goodJobs[l][1] > ppl: return goodJobs[r][0] return goodJobs[l][0] goodJobs = [] jobs = [(profit[i], difficulty[i]) for i in range(len(difficulty))] jobs = sorted(jobs, key=lambda job: job[0], reverse=True) goodJobs.append(jobs[0]) for i in range(1, len(jobs)): if jobs[i][1] < goodJobs[-1][1]: goodJobs.append(jobs[i]) ans = 0 print((goodJobs, worker)) for ppl in worker: ans = ans + findProfit(ppl, goodJobs) print(ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR NUMBER VAR RETURN VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: n = max(worker) s = [(0) for i in range(n)] for i in range(len(difficulty)): if difficulty[i] <= n: s[difficulty[i] - 1] = max(s[difficulty[i] - 1], profit[i]) for i in range(1, n): if s[i] < s[i - 1]: s[i] = s[i - 1] ans = sum([s[worker[i] - 1] for i in range(len(worker))]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: l = sorted(zip(profit, difficulty), reverse=True) worker.sort(reverse=True) p = 0 for w in worker: while l and w < l[0][1]: l.pop(0) if not l: break p += l[0][0] return p
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: def bs(arr, s, e, target): while s <= e: mid = s + (e - s) // 2 if arr[mid][0] <= target: s = mid + 1 else: e = mid - 1 return e arr = [] for pd in sorted(zip(difficulty, profit)): if arr and arr[-1][1] >= pd[1]: continue arr.append(pd) res = 0 N = len(arr) for w in worker: idx = bs(arr, 0, N - 1, w) if idx >= 0: res += arr[idx][1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = [[difficulty[i], profit[i]] for i in range(len(difficulty))] jobs.sort() worker.sort() ans, best, i = 0, 0, 0 for skill in worker: while i < len(jobs) and skill >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: profit = [i[1] for i in sorted(zip(difficulty, profit))] difficulty.sort() for i in range(1, len(profit)): profit[i] = max(profit[i], profit[i - 1]) difficulty.append(float("inf")) tot = 0 for w in range(len(worker)): low = 0 high = len(difficulty) - 1 while low < high: mid = low + (high - low) // 2 if difficulty[mid] > worker[w]: high = mid else: low = mid + 1 low -= 1 if low < 0: continue tot += profit[low] return tot
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
def createListOfProfitDiff(profit, difficulty): profit_diff = [] for i in range(len(profit)): profit_diff.append((profit[i], difficulty[i])) return profit_diff class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: w = sorted(worker) w.reverse() dp = [] for i in range(len(profit)): dp.append((profit[i], difficulty[i])) dp.sort(key=lambda x: x[0]) dp.reverse() i = 0 count = 0 for p in dp: while i < len(w) and p[1] <= w[i]: count += p[0] i += 1 return count
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution(object): def maxProfitAssignment(self, difficulty, profit, worker): jobs = zip(difficulty, profit) jobs = sorted(jobs) ans = i = best = 0 for skill in sorted(worker): while i < len(jobs) and skill >= jobs[i][0]: best = max(best, jobs[i][1]) i += 1 ans += best return ans
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: arr = [(0) for _ in range(10**5 + 1)] for i in range(len(profit)): arr[difficulty[i]] = max(profit[i], arr[difficulty[i]]) for i in range(1, 10**5 + 1): arr[i] = max(arr[i - 1], arr[i]) ans = 0 for i in worker: ans += arr[i] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: temp = sorted(zip(difficulty, profit)) d = collections.defaultdict(int) for u, v in temp: d[u] = v maxdifficulty = max(worker) for i in range(1, maxdifficulty + 1): d[i] = max(d[i], d[i - 1]) ans = 0 for i in worker: ans += d[i] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: jobs = [[0, 0]] + sorted( [[difficulty[i], profit[i]] for i in range(len(profit))] ) for i in range(1, len(jobs)): jobs[i][1] = max(jobs[i - 1][1], jobs[i][1]) res, workerCounts = 0, collections.Counter(worker) def binarySearch(n): l, r = 0, len(jobs) - 1 while l < r - 1: mid = (l + r) // 2 if jobs[mid][0] > n: r = mid - 1 else: l = mid return jobs[l][1] if jobs[r][0] > n else jobs[r][1] for work, count in list(workerCounts.items()): res += binarySearch(work) * count return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER FUNC_CALL VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: arr = [] for i in range(len(difficulty)): arr.append((difficulty[i], profit[i])) arr = sorted(arr, key=lambda x: x[0]) maxProfit, maxProfits = 0, [] for _, gain in arr: maxProfit = max(maxProfit, gain) maxProfits.append(maxProfit) total = 0 for person in worker: idx = self.uppperBsearch(arr, person) - 1 if idx > -1: total += maxProfits[idx] return total def uppperBsearch(self, arr, target): left = 0 right = len(arr) while left < right: mid = (left + right) // 2 if target >= arr[mid][0]: left = mid + 1 else: right = mid return right
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def binarySearch(self, l, r, x, li): if r - l <= 1: if li[r] <= x: return r else: return l m = (l + r + 1) // 2 if li[m] <= x: return self.binarySearch(m, r, x, li) else: return self.binarySearch(l, m - 1, x, li) def maxProfitAssignment(self, difficulty, profit, worker): difficulty.append(0) profit.append(0) jobs = sorted(zip(difficulty, profit)) J = len(jobs) jobDifficulty = [j[0] for j in jobs] mostProfit = [jobs[0][1] for j in jobs] for i in range(1, J): mostProfit[i] = max(jobs[i][1], mostProfit[i - 1]) totProfit = 0 for w in worker: maxJob = self.binarySearch(0, J - 1, w, jobDifficulty) totProfit += mostProfit[maxJob] return totProfit
CLASS_DEF FUNC_DEF IF BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: tup_list = sorted(list(zip(difficulty, profit))) filtered_list = [] best = 0 for d, p in tup_list: if p > best: best = p filtered_list.append((d, p)) worker = sorted(worker) profit = 0 dix = 0 for w in worker: if w < filtered_list[dix][0]: continue while dix + 1 < len(filtered_list) and filtered_list[dix + 1][0] <= w: dix += 1 profit += filtered_list[dix][1] return profit
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: difficulty, profit = zip(*sorted(zip(difficulty, profit))) profit_table = {} for i, (d, p) in enumerate(zip(difficulty, profit)): if i > 0: profit_table[d] = max(p, profit_table[difficulty[i - 1]]) else: profit_table[d] = p value = 0 d_index = len(difficulty) - 1 for w in reversed(sorted(worker)): while d_index >= 0 and difficulty[d_index] > w: d_index -= 1 if d_index >= 0: value += profit_table[difficulty[d_index]] return value
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: dp = {} for i, d in enumerate(difficulty): dp[d] = max(profit[i], dp.get(d, 0)) dp = sorted([(d, p) for d, p in dp.items()]) d2p = [] pre = 0 for d, p in dp: pre = max(pre, p) d2p.append((d, pre)) def getjob(work, pv, pw, pi): l, r = 0, len(d2p) - 1 if d2p[l][0] > work: return 0, 0 if d2p[r][0] <= work: return d2p[r][1], r l = pi while l <= r: i = (l + r) // 2 if d2p[i][0] == work: return max(d2p[i][1], pv), i if d2p[i][0] > work: r = i - 1 else: l = i + 1 if i == r: return max(d2p[i][1], pv), i return max(d2p[i - 1][1], pv), i - 1 rs = 0 pv = 0 pw = 0 pi = 0 for w in sorted(worker): pv, pi = getjob(w, pv, pw, pi) pw = w rs += pv return rs
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN NUMBER NUMBER IF VAR VAR NUMBER VAR RETURN VAR VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: if difficulty is None: return 0 thislist = [] for i in range(0, len(difficulty)): thislist.append((difficulty[i], profit[i])) thislist.sort() worker.sort() res = 0 curr_max = 0 worker_index = 0 list_index = 0 while worker_index < len(worker) and list_index < len(thislist): if thislist[list_index][0] <= worker[worker_index]: curr_max = max(curr_max, thislist[list_index][1]) list_index += 1 else: res += curr_max worker_index += 1 if worker_index < len(worker): res += curr_max * (len(worker) - worker_index) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment(self, d: List[int], p: List[int], work: List[int]) -> int: maxd = max(d) cmb1 = defaultdict(int) for i in range(len(p)): cmb1[d[i]] = max(cmb1[d[i]], p[i]) cmb = sorted(cmb1.items(), key=lambda x: x[0]) NN = len(cmb) dd = [0] * (maxd + 1) val = j = 0 for i in range(maxd + 1): if j < NN and i == cmb[j][0]: val = max(val, cmb[j][1]) j += 1 dd[i] = val ans = 0 for w in work: if w > maxd: ans += dd[maxd] else: ans += dd[w] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: pairs = [(a, b) for a, b in zip(difficulty, profit)] sorted_pairs = sorted(pairs, key=lambda p: p[0]) benifit = [] current_max = 0 for diff, pro in sorted_pairs: current_max = max(pro, current_max) benifit.append((diff, current_max)) sorted_workers = sorted(worker) ans = 0 current_difficulty_index = worker_index = 0 while worker_index < len(worker): if sorted_workers[worker_index] < benifit[current_difficulty_index][0]: worker_index += 1 continue while ( current_difficulty_index + 1 < len(worker) and benifit[current_difficulty_index + 1][0] <= sorted_workers[worker_index] ): current_difficulty_index += 1 ans += benifit[current_difficulty_index][1] worker_index += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: difficulty_price = sorted( list(zip(difficulty, profit)), key=lambda x: (x[0], x[1]) ) max_prices = [] n = -1 curr_price = 0 for i in range(max(difficulty) + 1): if i == difficulty_price[n + 1][0]: n += 1 while n < len(difficulty_price) - 1 and i == difficulty_price[n + 1][0]: n += 1 if difficulty_price[n][1] > curr_price: curr_price = difficulty_price[n][1] max_prices.append(curr_price) profit_sum = 0 for sub_worker in worker: if sub_worker >= len(max_prices): profit_sum += max_prices[-1] else: profit_sum += max_prices[sub_worker] return profit_sum
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: worker = sorted(worker) jobs = sorted( [(-1, 0)] + [(difficulty[i], profit[i]) for i in range(len(difficulty))] ) job_nums = len(jobs) maxProfit = 0 job_index = 0 res = 0 for ability in worker: while job_index < job_nums - 1 and ability >= jobs[job_index + 1][0]: job_index += 1 maxProfit = max(maxProfit, jobs[job_index][1]) res += maxProfit return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: def binary_search(arr, target): l, r = 0, len(arr) - 1 res = -1 while l <= r: mid = (l + r) // 2 if arr[mid][0] <= target: res = mid l = mid + 1 else: r = mid - 1 return res jobs = sorted([d, p] for d, p in zip(difficulty, profit)) for i in range(1, len(jobs)): jobs[i][1] = max(jobs[i][1], jobs[i - 1][1]) res = 0 for w in worker: i = binary_search(jobs, w) if i == -1: continue res += jobs[i][1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: m = len(difficulty) difficulty, profit = zip(*sorted(zip(difficulty, profit))) difficulty = list(difficulty) profit = list(profit) best_profit = m * [0] best_profit[0] = profit[0] ind = 0 prev_v = difficulty[0] for i in range(1, m): if prev_v != difficulty[i]: ind += 1 difficulty[ind] = prev_v = difficulty[i] best_profit[ind] = best_profit[ind - 1] best_profit[ind] = max(best_profit[ind], profit[i]) m = ind + 1 def bins(target): start = 0 end = m - 1 bsres = 0 while start <= end: middle = start + (end - start) // 2 if difficulty[middle] == target: return best_profit[middle] if difficulty[middle] < target: bsres = best_profit[middle] start = middle + 1 else: end = middle - 1 return bsres res = 0 h = dict() for w in worker: if w not in h: h[w] = bins(w) res += h[w] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
from itertools import chain class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: li = list(chain(zip(profit, difficulty))) li.sort() cutoffs = [li[-1][1]] profits = [li[-1][0]] for i in range(len(profit) - 2, -1, -1): if li[i][1] < cutoffs[0]: cutoffs.insert(0, li[i][1]) profits.insert(0, li[i][0]) cutoffs.insert(0, 0) profits.insert(0, 0) ans = 0 n = len(cutoffs) for ability in worker: l = 0 r = n - 1 while r >= l: mid = (l + r) // 2 if ability == cutoffs[mid]: break elif ability > cutoffs[mid]: l = mid + 1 else: r = mid - 1 mid = mid - 1 for i in range(3): if mid + 1 < n: if ability >= cutoffs[mid + 1]: mid += 1 ans += profits[mid] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR VAR
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith job.  Now we have some workers. worker[i] is the ability of the ith worker, which means that this worker can only complete a job with difficulty at most worker[i].  Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if 3 people attempt the same job that pays $1, then the total profit will be $3.  If a worker cannot complete any job, his profit is $0. What is the most profit we can make? Example 1: Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7] Output: 100 Explanation: Workers are assigned jobs of difficulty [4,4,6,6] and they get profit of [20,20,30,30] seperately. Notes: 1 <= difficulty.length = profit.length <= 10000 1 <= worker.length <= 10000 difficulty[i], profit[i], worker[i]  are in range [1, 10^5]
class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: _max = max(worker) + 1 make = [0] * _max num = [ (difficulty[i], profit[i]) for i in range(len(profit)) if difficulty[i] < _max ] num.sort(key=lambda x: x[0]) p_max = 0 for i in range(len(num)): if num[i][1] >= p_max: make[num[i][0]] = num[i][1] p_max = num[i][1] pre = -1 for i in range(len(make)): if make[i] != 0: pre = make[i] elif pre != -1: make[i] = pre ans = 0 for w in worker: ans += make[w] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR RETURN VAR VAR
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated). The weight of a is defined as the maximum number of elements you can remove. You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a? Input The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array. The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n). Output Print q lines, i-th line should contain a single integer — the answer to the i-th query. Examples Input 13 5 2 2 3 9 5 4 6 5 7 8 3 11 13 3 1 0 0 2 4 5 0 0 12 Output 5 11 6 1 0 Input 5 2 1 4 1 2 4 0 0 1 0 Output 2 0 Note Explanation of the first query: After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity). Here is a strategy that removes 5 elements (the element removed is colored in red): * [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×] * [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×] * [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×] * [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×] * [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×] * [×, ×, ×, 9, 4, 5, 3, ×] (final state) It is impossible to remove more than 5 elements, hence the weight is 5.
from sys import stdin def bitadd(a, w, bit): x = a while x <= len(bit) - 1: bit[x] += w x += x & -1 * x def bitsum(a, bit): ret = 0 x = a while x > 0: ret += bit[x] x -= x & -1 * x return ret class RangeBIT: def __init__(self, N, indexed): self.bit1 = [0] * (N + 2) self.bit2 = [0] * (N + 2) self.mode = indexed def bitadd(self, a, w, bit): x = a while x <= len(bit) - 1: bit[x] += w x += x & -1 * x def bitsum(self, a, bit): ret = 0 x = a while x > 0: ret += bit[x] x -= x & -1 * x return ret def add(self, l, r, w): l = l + (1 - self.mode) r = r + (1 - self.mode) self.bitadd(l, -1 * w * l, self.bit1) self.bitadd(r, w * r, self.bit1) self.bitadd(l, w, self.bit2) self.bitadd(r, -1 * w, self.bit2) def sum(self, l, r): l = l + (1 - self.mode) r = r + (1 - self.mode) ret = self.bitsum(r, self.bit1) + r * self.bitsum(r, self.bit2) ret -= self.bitsum(l, self.bit1) + l * self.bitsum(l, self.bit2) return ret n, q = map(int, stdin.readline().split()) a = list(map(int, stdin.readline().split())) qs = [[] for i in range(n + 1)] ans = [None] * q BIT = [0] * (n + 1) for loop in range(q): x, y = map(int, stdin.readline().split()) l = x + 1 r = n - y qs[r].append((l, loop)) for r in range(1, n + 1): b = r - a[r - 1] if b >= 0: L = 1 R = r + 1 while R - L != 1: M = (L + R) // 2 if bitsum(M, BIT) >= b: L = M else: R = M if bitsum(L, BIT) >= b: bitadd(1, 1, BIT) bitadd(L + 1, -1, BIT) for ql, qind in qs[r]: ans[qind] = bitsum(ql, BIT) for i in ans: print(i)
FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Let a_1, …, a_n be an array of n positive integers. In one operation, you can choose an index i such that a_i = i, and remove a_i from the array (after the removal, the remaining parts are concatenated). The weight of a is defined as the maximum number of elements you can remove. You must answer q independent queries (x, y): after replacing the x first elements of a and the y last elements of a by n+1 (making them impossible to remove), what would be the weight of a? Input The first line contains two integers n and q (1 ≤ n, q ≤ 3 ⋅ 10^5) — the length of the array and the number of queries. The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — elements of the array. The i-th of the next q lines contains two integers x and y (x, y ≥ 0 and x+y < n). Output Print q lines, i-th line should contain a single integer — the answer to the i-th query. Examples Input 13 5 2 2 3 9 5 4 6 5 7 8 3 11 13 3 1 0 0 2 4 5 0 0 12 Output 5 11 6 1 0 Input 5 2 1 4 1 2 4 0 0 1 0 Output 2 0 Note Explanation of the first query: After making first x = 3 and last y = 1 elements impossible to remove, a becomes [×, ×, ×, 9, 5, 4, 6, 5, 7, 8, 3, 11, ×] (we represent 14 as × for clarity). Here is a strategy that removes 5 elements (the element removed is colored in red): * [×, ×, ×, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, ×] * [×, ×, ×, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, ×] * [×, ×, ×, 9, 4, \color{red}{6}, 5, 7, 8, 3, ×] * [×, ×, ×, 9, 4, 5, 7, \color{red}{8}, 3, ×] * [×, ×, ×, 9, 4, 5, \color{red}{7}, 3, ×] * [×, ×, ×, 9, 4, 5, 3, ×] (final state) It is impossible to remove more than 5 elements, hence the weight is 5.
class Fenwick: def __init__(self, size): self.size = size self.tree = [0] * (size + 1) def add(self, idx, val): idx = int(idx) while idx <= self.size: self.tree[idx] += val idx += idx & -idx def sum(self, idx): ret = 0 idx = int(idx) while idx > 0: ret += self.tree[idx] idx -= idx & -idx return ret n, q = map(int, input().split()) A = [int(x) for x in input().split()] A = [(A[i] - (i + 1)) for i in range(n)] query = [[] for _ in range(n + 1)] for i in range(q): x, y = map(int, input().split()) l, r = x, n - y - 1 query[r].append((l, i)) ft = Fenwick(n + 1) ans = [(0) for _ in range(q + 3)] for r in range(n): ob = A[r] if ob <= 0: if ft.sum(1) >= -ob: low, high = 0, r while low + 1 < high: mid = low + high >> 1 if ft.sum(mid + 1) >= -ob: low = mid else: high = mid idx = high if ft.sum(high + 1) >= -ob else low ft.add(1, 1) ft.add(idx + 2, -1) for qr in query[r]: ans[qr[1]] = ft.sum(qr[0] + 1) for _ in range(q): print(ans[_])
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given an array $a$ of $n$ integers, find a range of values $[x, y]$ ($x \le y$), and split $a$ into exactly $k$ ($1 \le k \le n$) subarrays in such a way that: Each subarray is formed by several continuous elements of $a$, that is, it is equal to $a_l, a_{l+1}, \ldots, a_r$ for some $l$ and $r$ ($1 \leq l \leq r \leq n$). Each element from $a$ belongs to exactly one subarray. In each subarray the number of elements inside the range $[x, y]$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $i$ is inside the range $[x, y]$ if and only if $x \le a_i \le y$. Print any solution that minimizes $y - x$. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 3 \cdot 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the array $a$ and the number of subarrays required in the partition. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) where $a_i$ is the $i$-th element of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, print $k+1$ lines. In the first line, print $x$ and $y$ — the limits of the found range. Then print $k$ lines, the $i$-th should contain $l_i$ and $r_i$ ($1\leq l_i \leq r_i \leq n$) — the limits of the $i$-th subarray. You can print the subarrays in any order. -----Examples----- Input 3 2 1 1 2 4 2 1 2 2 2 11 3 5 5 5 1 5 5 1 5 5 5 1 Output 1 2 1 2 2 2 1 3 4 4 5 5 1 1 2 2 3 11 -----Note----- In the first test, there should be only one subarray, which must be equal to the whole array. There are $2$ elements inside the range $[1, 2]$ and $0$ elements outside, if the chosen range is $[1, 1]$, there will be $1$ element inside ($a_1$) and $1$ element outside ($a_2$), and the answer will be invalid. In the second test, it is possible to choose the range $[2, 2]$, and split the array in subarrays $(1, 3)$ and $(4, 4)$, in subarray $(1, 3)$ there are $2$ elements inside the range ($a_2$ and $a_3$) and $1$ element outside ($a_1$), in subarray $(4, 4)$ there is only $1$ element ($a_4$), and it is inside the range. In the third test, it is possible to choose the range $[5, 5]$, and split the array in subarrays $(1, 4)$, $(5, 7)$ and $(8, 11)$, in the subarray $(1, 4)$ there are $3$ elements inside the range and $1$ element outside, in the subarray $(5, 7)$ there are $2$ elements inside and $1$ element outside and in the subarray $(8, 11)$ there are $3$ elements inside and $1$ element outside.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = a[:] b.sort() needed = (n + k + 1) // 2 - 1 md, ml, mr = b[needed] - b[0], 0, needed for i in range(needed, n): cd = b[i] - b[i - needed] if cd < md: md, ml, mr = cd, i - needed, i print(b[ml], b[mr]) balance = 0 splits = [] for i in range(n): if len(splits) >= k - 1: break if b[ml] <= a[i] <= b[mr]: balance += 1 else: balance -= 1 if balance > 0: balance = 0 splits.append(i) if k > 1: print(1, splits[0] + 1) for i in range(1, k - 1): print(splits[i - 1] + 2, splits[i] + 1) print(splits[-1] + 2, n) else: print(1, n)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR
Given an array $a$ of $n$ integers, find a range of values $[x, y]$ ($x \le y$), and split $a$ into exactly $k$ ($1 \le k \le n$) subarrays in such a way that: Each subarray is formed by several continuous elements of $a$, that is, it is equal to $a_l, a_{l+1}, \ldots, a_r$ for some $l$ and $r$ ($1 \leq l \leq r \leq n$). Each element from $a$ belongs to exactly one subarray. In each subarray the number of elements inside the range $[x, y]$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $i$ is inside the range $[x, y]$ if and only if $x \le a_i \le y$. Print any solution that minimizes $y - x$. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 3 \cdot 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the array $a$ and the number of subarrays required in the partition. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) where $a_i$ is the $i$-th element of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, print $k+1$ lines. In the first line, print $x$ and $y$ — the limits of the found range. Then print $k$ lines, the $i$-th should contain $l_i$ and $r_i$ ($1\leq l_i \leq r_i \leq n$) — the limits of the $i$-th subarray. You can print the subarrays in any order. -----Examples----- Input 3 2 1 1 2 4 2 1 2 2 2 11 3 5 5 5 1 5 5 1 5 5 5 1 Output 1 2 1 2 2 2 1 3 4 4 5 5 1 1 2 2 3 11 -----Note----- In the first test, there should be only one subarray, which must be equal to the whole array. There are $2$ elements inside the range $[1, 2]$ and $0$ elements outside, if the chosen range is $[1, 1]$, there will be $1$ element inside ($a_1$) and $1$ element outside ($a_2$), and the answer will be invalid. In the second test, it is possible to choose the range $[2, 2]$, and split the array in subarrays $(1, 3)$ and $(4, 4)$, in subarray $(1, 3)$ there are $2$ elements inside the range ($a_2$ and $a_3$) and $1$ element outside ($a_1$), in subarray $(4, 4)$ there is only $1$ element ($a_4$), and it is inside the range. In the third test, it is possible to choose the range $[5, 5]$, and split the array in subarrays $(1, 4)$, $(5, 7)$ and $(8, 11)$, in the subarray $(1, 4)$ there are $3$ elements inside the range and $1$ element outside, in the subarray $(5, 7)$ there are $2$ elements inside and $1$ element outside and in the subarray $(8, 11)$ there are $3$ elements inside and $1$ element outside.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) sa = sorted(a) cnt = [(0) for i in range(n + 1)] for i in range(n): cnt[a[i]] += 1 for i in range(2, n + 1): cnt[i] += cnt[i - 1] ans = -1, n need = (n + k + 1) // 2 for i in range(n - need + 1): new_ans = sa[i], sa[i + need - 1] if new_ans[1] - new_ans[0] < ans[1] - ans[0]: ans = new_ans print(ans[0], ans[1]) lst = 0 cnt = 0 for i in range(n): if k == 1: break if ans[0] <= a[i] <= ans[1]: cnt += 1 else: cnt -= 1 if cnt > 0: print(lst + 1, i + 1) lst = i + 1 cnt = 0 k -= 1 print(lst + 1, n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Given an array $a$ of $n$ integers, find a range of values $[x, y]$ ($x \le y$), and split $a$ into exactly $k$ ($1 \le k \le n$) subarrays in such a way that: Each subarray is formed by several continuous elements of $a$, that is, it is equal to $a_l, a_{l+1}, \ldots, a_r$ for some $l$ and $r$ ($1 \leq l \leq r \leq n$). Each element from $a$ belongs to exactly one subarray. In each subarray the number of elements inside the range $[x, y]$ (inclusive) is strictly greater than the number of elements outside the range. An element with index $i$ is inside the range $[x, y]$ if and only if $x \le a_i \le y$. Print any solution that minimizes $y - x$. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 3 \cdot 10^4$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the length of the array $a$ and the number of subarrays required in the partition. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) where $a_i$ is the $i$-th element of the array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$. -----Output----- For each test case, print $k+1$ lines. In the first line, print $x$ and $y$ — the limits of the found range. Then print $k$ lines, the $i$-th should contain $l_i$ and $r_i$ ($1\leq l_i \leq r_i \leq n$) — the limits of the $i$-th subarray. You can print the subarrays in any order. -----Examples----- Input 3 2 1 1 2 4 2 1 2 2 2 11 3 5 5 5 1 5 5 1 5 5 5 1 Output 1 2 1 2 2 2 1 3 4 4 5 5 1 1 2 2 3 11 -----Note----- In the first test, there should be only one subarray, which must be equal to the whole array. There are $2$ elements inside the range $[1, 2]$ and $0$ elements outside, if the chosen range is $[1, 1]$, there will be $1$ element inside ($a_1$) and $1$ element outside ($a_2$), and the answer will be invalid. In the second test, it is possible to choose the range $[2, 2]$, and split the array in subarrays $(1, 3)$ and $(4, 4)$, in subarray $(1, 3)$ there are $2$ elements inside the range ($a_2$ and $a_3$) and $1$ element outside ($a_1$), in subarray $(4, 4)$ there is only $1$ element ($a_4$), and it is inside the range. In the third test, it is possible to choose the range $[5, 5]$, and split the array in subarrays $(1, 4)$, $(5, 7)$ and $(8, 11)$, in the subarray $(1, 4)$ there are $3$ elements inside the range and $1$ element outside, in the subarray $(5, 7)$ there are $2$ elements inside and $1$ element outside and in the subarray $(8, 11)$ there are $3$ elements inside and $1$ element outside.
ans = [] for _ in range(int(input())): n, k = map(int, input().split()) x = list(map(int, input().split())) a, b = 1, n kol = (n + k + 1) // 2 used = [0] * (n + 2) for i in range(n): used[x[i]] += 1 l, r = 1, 1 m = 0 for r in range(1, n + 1): m += used[r] while m - used[l] >= kol: m -= used[l] l += 1 if m >= kol and r - l < b - a: a, b = l, r otv = [] otv.append([a, b]) l = 0 k1 = 0 k2 = 0 for i in range(0, n): if k == 1: otv.append([i + 1, n]) break if a <= x[i] <= b: k1 += 1 else: k2 += 1 if k1 > k2: otv.append([l + 1, i + 1]) l = i + 1 k -= 1 k1, k2 = 0, 0 ans.append(otv) for i in range(len(ans)): for j in range(len(ans[i])): print(ans[i][j][0], ans[i][j][1])
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) while t: n = int(input()) s = input() arr = [] pb = arr.append cnt, last = 1, s[0] for i in range(1, n): if last == s[i]: cnt += 1 else: pb(cnt) cnt = 1 last = s[i] pb(cnt) i, j, ans = 0, 0, 0 n = len(arr) while i < n: while j < n and (j <= i or arr[j] == 1): j += 1 ans += 1 if arr[i] == 1: if j < n: arr[j] -= 1 else: i += 1 i += 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You have a string $s$ consisting of $n$ characters. Each character is either 0 or 1. You can perform operations on the string. Each operation consists of two steps: select an integer $i$ from $1$ to the length of the string $s$, then delete the character $s_i$ (the string length gets reduced by $1$, the indices of characters to the right of the deleted one also get reduced by $1$); if the string $s$ is not empty, delete the maximum length prefix consisting of the same characters (the indices of the remaining characters and the string length get reduced by the length of the deleted prefix). Note that both steps are mandatory in each operation, and their order cannot be changed. For example, if you have a string $s =$ 111010, the first operation can be one of the following: select $i = 1$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 2$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 3$: we'll get 111010 $\rightarrow$ 11010 $\rightarrow$ 010; select $i = 4$: we'll get 111010 $\rightarrow$ 11110 $\rightarrow$ 0; select $i = 5$: we'll get 111010 $\rightarrow$ 11100 $\rightarrow$ 00; select $i = 6$: we'll get 111010 $\rightarrow$ 11101 $\rightarrow$ 01. You finish performing operations when the string $s$ becomes empty. What is the maximum number of operations you can perform? -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the string $s$. The second line contains string $s$ of $n$ characters. Each character is either 0 or 1. It's guaranteed that the total sum of $n$ over test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer — the maximum number of operations you can perform. -----Example----- Input 5 6 111010 1 0 1 1 2 11 6 101010 Output 3 1 1 1 3 -----Note----- In the first test case, you can, for example, select $i = 2$ and get string 010 after the first operation. After that, you can select $i = 3$ and get string 1. Finally, you can only select $i = 1$ and get empty string.
t = int(input()) for i in range(t): n = int(input()) s = input() string = [] flag = False temp = "" stack = [] for i in range(n): if not stack: stack.append(s[i]) elif s[i] == stack[-1]: stack.append(s[i]) else: string.append(len(stack)) stack = [s[i]] if stack: string.append(len(stack)) i = 0 j = 0 c = 0 n = len(string) flag = False while i < n: if not flag: if string[j] >= 2: if i == j: string[j] = 0 else: string[j] -= 1 i += 1 c += 1 else: j += 1 if j == n: flag = True else: a = n - 1 - i + 1 c += a // 2 if a % 2 != 0: c += 1 break print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR