description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
n = len(intervals)
intervals.sort(key=lambda x: (x[0], -x[1]))
removeSet = set()
for i in range(n):
interval1 = intervals[i]
for j in range(i + 1, n):
interval2 = intervals[j]
if interval2[0] >= interval1[1]:
break
if interval2[1] <= interval1[1]:
removeSet.add(j)
return n - len(removeSet) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: (x[0], -x[1]))
covering = 0
used = set()
for i, (si, ei) in enumerate(intervals):
for sj, ej in intervals[i + 1 :]:
if sj > ei:
break
if ej > ei:
continue
if (sj, ej) in used:
continue
used.add((sj, ej))
covering += 1
return len(intervals) - covering | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
int_set = set(tuple(i) for i in intervals)
for i, (a, b) in enumerate(intervals):
for c, d in intervals[i + 1 :]:
if a <= c and d <= b and (c, d) in int_set:
int_set.remove((c, d))
elif c <= a and b <= d and (a, b) in int_set:
int_set.remove((a, b))
return len(int_set) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def compare(self, intervalA: List[int], intervalB: List[int]) -> int:
if intervalA[0] <= intervalB[0] and intervalA[1] >= intervalB[1]:
return 1
if intervalB[0] <= intervalA[0] and intervalB[1] >= intervalA[1]:
return -1
return 0
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[0])
result = len(intervals)
index = 0
while index < len(intervals):
itemA = intervals[index]
removes = []
for i in range(index + 1, len(intervals)):
itemB = intervals[i]
comp = self.compare(itemA, itemB)
if comp == 0:
continue
if comp == 1:
removes.append(i)
elif comp == -1 and index not in removes:
removes.append(index)
if len(removes) == 0:
index += 1
else:
removes.sort()
removes.reverse()
for k in range(len(removes)):
intervals.remove(intervals[removes[k]])
if index not in removes:
index += 1
result -= len(removes)
return result | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
deleted = set()
for idx, interval in enumerate(intervals):
if str(interval) in deleted:
continue
for other in intervals[idx + 1 :]:
if interval[0] <= other[0] and interval[1] >= other[1]:
deleted.add(str(other))
elif interval[0] >= other[0] and interval[1] <= other[1]:
deleted.add(str(interval))
return len(intervals) - len(deleted) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
for i in range(len(intervals) - 1):
for j in range(len(intervals) - i - 1):
if intervals[j][0] > intervals[j + 1][0]:
intervals[j], intervals[j + 1] = intervals[j + 1], intervals[j]
i = 0
while True:
while intervals[i][1] >= intervals[i + 1][1]:
intervals.pop(i + 1)
if i + 1 > len(intervals) - 1:
break
if i + 1 > len(intervals) - 1:
break
if intervals[i][0] == intervals[i + 1][0]:
intervals.pop(i)
if i + 1 > len(intervals) - 1:
break
else:
continue
if i + 1 == len(intervals) - 1:
break
i += 1
return len(intervals) | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
i = 0
j = 1
while i < len(intervals) and j < len(intervals):
if (
intervals[i][0] >= intervals[j][0]
and intervals[i][1] <= intervals[j][1]
):
intervals.pop(i)
i, j = 0, 1
elif (
intervals[i][0] <= intervals[j][0]
and intervals[i][1] >= intervals[j][1]
):
intervals.pop(j)
i, j = 0, 1
else:
j += 1
if j == len(intervals):
i += 1
j = i + 1
return len(intervals) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
def isCovered(interval, candidates):
for c in candidates:
if c[0] <= interval[0] and c[1] >= interval[1]:
return True
return False
cnt = 0
for i, interval in enumerate(intervals):
if not isCovered(interval, intervals[:i] + intervals[i + 1 :]):
cnt += 1
return cnt | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
def overlap(interval1, interval2):
if interval1[0] > interval2[0]:
interval1, interval2 = interval2, interval1
return interval1[1] >= interval2[1]
intervals.sort(key=lambda x: (x[0], -x[1]))
print(intervals)
count = 0
res = [intervals[0]]
for i in intervals[1:]:
print((i, res[-1]))
if not overlap(res[-1], i):
res.append(i)
return len(res) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
count = [0] * len(intervals)
for i in range(len(intervals) - 1):
a = intervals[i][0]
b = intervals[i][1]
for j in range(i + 1, len(intervals)):
x = intervals[j][0]
y = intervals[j][1]
if x <= a and b <= y:
count[i] = 1
elif a <= x and y <= b:
count[j] = 1
ans = 0
for c in count:
if c == 0:
ans += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
notCovered = len(intervals)
covered = len(intervals) * [False]
intervals.sort(key=lambda x: (x[0], -x[1]))
for i in range(len(intervals)):
if not covered[i]:
for j in range(i + 1, len(intervals)):
if not covered[j]:
firstIntervalEnd = intervals[i][1]
secondIntervalEnd = intervals[j][1]
if secondIntervalEnd <= firstIntervalEnd:
covered[j] = True
notCovered -= 1
return notCovered | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda t: [t[0], t[1]])
deleted = set()
i = 0
while i < len(intervals):
boundary = tuple(intervals[i])
if not boundary in deleted:
j = i + 1
stop = False
while not stop and j < len(intervals):
comparedBoundary = tuple(intervals[j])
if comparedBoundary[0] == boundary[0]:
deleted.add(boundary)
stop = True
elif comparedBoundary[0] >= boundary[1]:
stop = True
elif comparedBoundary[1] <= boundary[1]:
deleted.add(comparedBoundary)
j += 1
i += 1
return len(intervals) - len(deleted) | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals: List[List[int]]) -> int:
non_covered_intervals = []
covered_intervals = set()
for i in range(len(intervals)):
for j in range(len(intervals)):
if j == i:
continue
if tuple(intervals[j]) in covered_intervals:
continue
if (
intervals[i][0] >= intervals[j][0]
and intervals[i][1] <= intervals[j][1]
):
covered_intervals.add(tuple(intervals[i]))
break
else:
non_covered_intervals.append(intervals[i])
return len(non_covered_intervals) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a list of intervals, remove all intervals that are covered by another interval in the list.
Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d.
After doing so, return the number of remaining intervals.
Example 1:
Input: intervals = [[1,4],[3,6],[2,8]]
Output: 2
Explanation: Interval [3,6] is covered by [2,8], therefore it is removed.
Example 2:
Input: intervals = [[1,4],[2,3]]
Output: 1
Example 3:
Input: intervals = [[0,10],[5,12]]
Output: 2
Example 4:
Input: intervals = [[3,10],[4,10],[5,11]]
Output: 2
Example 5:
Input: intervals = [[1,2],[1,4],[3,4]]
Output: 1
Constraints:
1 <= intervals.length <= 1000
intervals[i].length == 2
0 <= intervals[i][0] < intervals[i][1] <= 10^5
All the intervals are unique. | class Solution:
def removeCoveredIntervals(self, intervals):
result = 0
for i in range(len(intervals)):
covered = False
for j in range(len(intervals)):
if i == j:
continue
if self.covered(intervals[i], intervals[j]):
covered = True
break
if not covered:
result += 1
return result
def covered(self, i1, i2):
c = i2[0]
d = i2[1]
a = i1[0]
b = i1[1]
return c <= a and b <= d | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR VAR |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
citations.sort(reverse=True)
for idx in range(len(citations)):
if idx + 1 > citations[idx]:
return idx
return len(citations) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
l, r, res = 0, len(citations) - 1, 0
while l <= r:
mid = (l + r) // 2
if len(citations) - mid <= citations[mid]:
res, r = len(citations) - mid, r - 1
else:
l = mid + 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
c = citations
if not c:
return 0
s, e = 0, len(c) - 1
if c[s] >= len(c):
return len(c)
if c[e] < 1:
return 0
while s < e - 1:
m = s + int((e - s) / 2)
if c[m] >= len(c) - m:
e = m
else:
s = m
return len(c) - e | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
citations.sort()
for i in range(len(citations) - 1, -1, -1):
if citations[i] >= len(citations) - i and (
i == 0 or citations[i - 1] <= len(citations) - i
):
return len(citations) - i
return 0 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
if not citations:
return 0
n = len(citations)
left, right = 0, n - 1
while left + 1 < right:
mid = (left + right) // 2
if citations[mid] >= n - mid:
right = mid
else:
left = mid
for mid in (left, right):
if citations[mid] >= n - mid:
return n - mid
return 0 | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution:
def hIndex(self, citations):
l = len(citations)
if l == 0:
return 0
if l == 1:
if citations[0] == 0:
return 0
else:
return 1
if min(citations) >= l:
return l
citations = citations[::-1]
count = 0
thres = 0
i = 0
while i < len(citations):
if thres >= count:
thres = citations[i]
count += 1
i += 1
else:
return count - 1
return count - 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
Example:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had
received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining
two with no more than 3 citations each, her h-index is 3.
Note:
If there are several possible values for h, the maximum one is taken as the h-index.
Follow up:
This is a follow up problem to H-Index, where citations is now guaranteed to be sorted in ascending order.
Could you solve it in logarithmic time complexity? | class Solution(object):
def hIndex(self, citations):
n = len(citations)
l = 0
r = n - 1
while l <= r:
m = (l + r) // 2
if (
m == 0
and citations[m] >= n - m
or citations[m - 1] < n - (m - 1)
and citations[m] >= n - m
):
return n - m
if citations[m] < n - m:
l = m + 1
else:
r = m
return 0 | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL 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 NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN NUMBER |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
w, f = {}, {}
for i in range(len(friends)):
w[i] = watchedVideos[i]
f[i] = friends[i]
cand = []
frontier = [(id, 0)]
seen = {id}
while frontier:
cur, d = frontier.pop()
if d == level:
cand.append(cur)
continue
for friend in f[cur]:
if friend not in seen:
seen.add(friend)
frontier.append((friend, d + 1))
count = collections.Counter()
for c in cand:
for m in w[c]:
count[m] += 1
ans = [(v, k) for k, v in count.items()]
ans.sort()
ans = [x[1] for x in ans]
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def bfs(
self,
id: int,
target_level: int,
friends: List[List[int]],
watchedVideos: List[List[str]],
ans: dict,
):
queue = []
visited = []
valid_index = 0
queue.insert(0, id)
visited.append(id)
for l in range(target_level):
new_q = []
while len(queue) != 0:
k = queue[0]
del queue[0]
for i in friends[k]:
if i not in visited:
visited.append(i)
new_q.append(i)
queue[:] = new_q
for f in queue:
for v in watchedVideos[f]:
if v not in ans:
ans[v] = 1
else:
ans[v] += 1
def dfs(
self,
id: int,
cur_level: int,
target_level: int,
friends: List[List[int]],
watchedVideos: List[List[str]],
ans: dict,
visited: List[int],
):
if id in visited:
return
visited.append(id)
if cur_level == target_level:
for v in watchedVideos[id]:
if v not in ans:
ans[v] = 1
else:
ans[v] += 1
return
for f in friends[id]:
self.dfs(
f, cur_level + 1, target_level, friends, watchedVideos, ans, visited
)
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
ans = {}
self.bfs(id, level, friends, watchedVideos, ans)
ret = [
k for k, v in sorted(list(ans.items()), key=lambda item: (item[1], item[0]))
]
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
n = len(watchedVideos)
visited = [False] * n
que = collections.deque([id])
visited[id] = True
cur = 0
while que and cur < level:
size = len(que)
cur += 1
for _ in range(size):
node = que.popleft()
for f in friends[node]:
if not visited[f]:
visited[f] = True
que.append(f)
cnt = collections.Counter()
for node in que:
for m in watchedVideos[node]:
cnt[m] += 1
videos = list(cnt.keys())
videos.sort(key=lambda x: [cnt[x], x])
return videos | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
visited = {id: 1}
queue = deque()
queue.append((id, 0))
result = []
while queue:
u, dist = queue.popleft()
if dist == level:
result.append(u)
continue
for v in friends[u]:
if v in visited:
continue
visited[v] = 1
queue.append((v, dist + 1))
counter = {}
for u in result:
for video in watchedVideos[u]:
counter[video] = counter.get(video, 0) + 1
result = sorted([(times, videos) for videos, times in list(counter.items())])
return [val[1] for val in result] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self, videos: List[List[str]], friends: List[List[int]], me: int, level: int
) -> List[str]:
visit = set()
queue, arr = [me], []
while level:
level -= 1
size = len(queue)
for i in range(size):
curr = queue.pop(0)
if curr not in visit:
visit.add(curr)
for f in friends[curr]:
queue.append(f)
v = []
for i in queue:
if i not in visit:
visit.add(i)
v += videos[i]
c = collections.Counter(v)
return sorted(sorted(list(set(v))), key=lambda x: c[x]) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST VAR LIST WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
stack = deque([id])
seen = set()
seen.add(id)
for _ in range(level):
for i in range(len(stack)):
f = stack.popleft()
for j in friends[f]:
if j not in seen:
stack.append(j)
seen.add(j)
count = Counter()
for lf in stack:
count += Counter(watchedVideos[lf])
return [
x[0] for x in sorted(count.items(), key=lambda item: (item[1], item[0]))
] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
k_friends = self.findFriends(friends, [id], {id}, level)
watched = {}
for id in k_friends:
for vid in watchedVideos[id]:
if vid in watched:
watched[vid] += 1
else:
watched[vid] = 1
out = []
for key in watched:
out.append((watched[key], key))
out.sort()
return list([x[1] for x in out])
def findFriends(self, friends, ids, seen, level):
if level == 0 or len(ids) == 0:
return ids
new = []
for id in ids:
for friend in friends[id]:
if friend not in seen:
seen.add(friend)
new.append(friend)
return self.findFriends(friends, new, seen, level - 1) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
queue = deque()
queue.append([id, 0])
ans = defaultdict(int)
visited = {id: 1}
while len(queue) > 0:
now, l = queue.popleft()
if l == level:
for v in watchedVideos[now]:
ans[v] += 1
continue
for friend in friends[now]:
if friend not in visited:
queue.append([friend, l + 1])
visited[friend] = 1
ansid = sorted(ans.items(), key=lambda x: (x[1], x[0]))
ansid = [x for x, _ in ansid]
return ansid | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
graph = collections.defaultdict(set)
for i in range(len(friends)):
for friend in friends[i]:
graph[i].add(friend)
graph[friend].add(i)
levelOfFriends = [id]
visited = set([id])
l = 0
while levelOfFriends and l < level:
size = len(levelOfFriends)
l += 1
for _ in range(size):
f = levelOfFriends.pop(0)
for otherF in graph[f]:
if otherF not in visited:
levelOfFriends.append(otherF)
visited.add(otherF)
w = collections.defaultdict(int)
for f in levelOfFriends:
videos = watchedVideos[f]
for v in videos:
w[v] += 1
heap = []
for v in list(w.keys()):
heapq.heappush(heap, (w[v], v))
res = []
for _ in range(len(heap)):
_, v = heapq.heappop(heap)
res.append(v)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
friends = getFriends(friends, id, 1, level, set([id]))
videoCounts = collections.Counter(
[val for i in friends for val in watchedVideos[i]]
)
sortedCounts = sorted(
list(videoCounts.items()), key=lambda video: (video[1], video[0])
)
return [count[0] for count in sortedCounts]
def getFriends(
friends: List[List[int]], index: int, level: int, targetLevel: int, knownFriends
):
currentFriends = set(friends[index]) - knownFriends
if level == targetLevel:
return currentFriends
else:
newKnownFriends = knownFriends | currentFriends
return set(
[
val
for i in currentFriends
for val in getFriends(
friends, i, level + 1, targetLevel, newKnownFriends
)
]
) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def createAdjMatrix(self, friends):
adj_mat = {}
for i in range(len(friends)):
adj_mat[i] = friends[i]
return adj_mat
def calculateFreq(self, level_friend, watchedVideos):
movies_freq = {}
for friend in level_friend:
movies = watchedVideos[friend]
for movie in movies:
movies_freq[movie] = movies_freq.get(movie, 0) + 1
movies_freq = sorted(movies_freq.items(), key=lambda x: (x[1], x[0]))
movies = [i[0] for i in movies_freq]
return movies
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
adj_mat = self.createAdjMatrix(friends)
visited = set()
Q = [(id, 0)]
visited.add(id)
level_friend = []
while Q and level:
id_, lev = Q.pop(0)
if lev == level:
level_friend.append(id_)
if lev > level:
break
friends = adj_mat[id_]
for friend in friends:
if friend not in visited:
Q.append((friend, lev + 1))
visited.add(friend)
return self.calculateFreq(level_friend, watchedVideos) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
stack = [(0, id)]
visited = set([id])
friendsSet = set()
while stack:
lNow, fi = heapq.heappop(stack)
if lNow == level:
friendsSet.add(fi)
else:
for fi1 in friends[fi]:
if fi1 not in visited:
visited.add(fi1)
heapq.heappush(stack, (lNow + 1, fi1))
videoCounter = Counter()
for f in friendsSet:
videoCounter += Counter(watchedVideos[f])
ans = sorted([key for key in videoCounter], key=lambda x: (videoCounter[x], x))
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
bfs, visited = {id}, {id}
for _ in range(level):
bfs = {j for i in bfs for j in friends[i] if j not in visited}
visited |= bfs
freq = collections.Counter([v for idx in bfs for v in watchedVideos[idx]])
return sorted(list(freq.keys()), key=lambda x: (freq[x], x)) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
vis = {id}
q = deque()
q.append(id)
while q and level:
ln = len(q)
for _ in range(ln):
x = q.popleft()
for i in friends[x]:
if i not in vis:
q.append(i)
vis.add(i)
level -= 1
d = defaultdict(int)
for i in q:
for j in watchedVideos[i]:
d[j] += 1
pq = []
res = []
for i in d:
heapq.heappush(pq, (d[i], i))
while pq:
res.append(heapq.heappop(pq)[1])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
g_friends = collections.defaultdict(list)
for i in range(len(friends)):
for f in friends[i]:
g_friends[i].append(f)
queue = collections.deque()
queue.append((id, 0))
visited = set([id])
videos = collections.defaultdict(int)
while queue:
size = len(queue)
for _ in range(size):
curr_id, curr_level = queue.popleft()
if curr_level == level:
for v in watchedVideos[curr_id]:
videos[v] += 1
else:
for f in g_friends[curr_id]:
if f not in visited:
visited.add(f)
queue.append((f, curr_level + 1))
return [v for _, v in sorted([(f, v) for v, f in list(videos.items())])] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
q = deque([id])
visited = set()
visited.add(id)
for _ in range(level):
for _ in range(len(q)):
curr = q.popleft()
for friend in friends[curr]:
if friend not in visited:
q.append(friend)
visited.add(friend)
video_dict = defaultdict(int)
for friend in q:
for video in watchedVideos[friend]:
video_dict[video] += 1
res = [
k
for k, v in sorted(
list(video_dict.items()), key=lambda item: (item[1], item[0])
)
]
return res
def fast(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
):
res = []
visited = [False] * len(friends)
fr_set = set()
q = deque([id])
for _ in range(level):
size = len(q)
for n in range(size):
fId = q.popleft()
for f in friends[fId]:
if visited[f] != True and f != id:
q.append(f)
visited[f] = True
dic = {}
while q:
fid = q.popleft()
for video in watchedVideos[fid]:
dic[video] = dic.get(video, 0) + 1
res = [
k for k, v in sorted(list(dic.items()), key=lambda item: (item[1], item[0]))
]
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
vis = [(0) for i in range(len(friends))]
index = id
lis = set([id])
while level > 0:
temp = []
for i in lis:
if vis[i] == 0:
temp += friends[i]
vis[i] = 1
lis = set(temp)
level -= 1
dic = dict()
for i in lis:
if vis[i] == 0:
for j in watchedVideos[i]:
if j in dic:
dic[j] += 1
else:
dic[j] = 1
dic2 = dict()
for i in dic:
if dic[i] in dic2:
dic2[dic[i]].append(i)
else:
dic2[dic[i]] = [i]
lis = []
for i in sorted(dic2.keys()):
lis += sorted(dic2[i])
return lis | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
graph = defaultdict(list)
for i, v in enumerate(friends):
graph[i].extend(v)
for r in v:
graph[r].append(i)
videos = defaultdict(int)
def bfs(source):
dq = deque([source])
visited = set()
visited.add(source)
nlevels = 0
while dq:
for _ in range(len(dq)):
node = dq.popleft()
if nlevels == level:
for v in watchedVideos[node]:
videos[v] += 1
for nei in graph[node]:
if nei not in visited:
visited.add(nei)
dq.append(nei)
nlevels += 1
bfs(id)
minheap = []
for v in sorted(videos.keys()):
heappush(minheap, (videos[v], v))
res = []
while minheap:
res.append(heappop(minheap)[1])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
n = len(watchedVideos)
a, d, w = [id], [1] * n, {}
d[id] = 0
for _ in range(level):
b = []
for i in a:
for j in friends[i]:
if d[j]:
b.append(j)
d[j] = 0
a = b
for i in a:
for x in watchedVideos[i]:
w[x] = w.get(x, 0) + 1
return sorted(w.keys(), key=lambda x: (w[x], x)) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR BIN_OP LIST NUMBER VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
frontier = [id]
seen = {id}
depth = 0
while frontier:
next_frontier = []
if depth == level - 1:
count = collections.defaultdict(lambda: 0)
for p in frontier:
for fr in friends[p]:
if fr not in seen:
seen.add(fr)
if depth == level - 1:
for video in watchedVideos[fr]:
count[video] += 1
next_frontier.append(fr)
frontier = next_frontier
depth += 1
if depth == level:
return sorted(count.keys(), key=lambda x: [count[x], x])
return [] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR RETURN LIST VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
visited = set()
visited.add(id)
frequency = {}
dictionary = {}
q = deque()
q.append(id)
running_level = 1
solution = []
while q and running_level <= level:
length = len(q)
for index in range(length):
node = q.popleft()
for friend in friends[node]:
if friend in visited:
continue
visited.add(friend)
if running_level == level:
for movie in watchedVideos[friend]:
frequency[movie] = frequency.get(movie, 0) + 1
else:
q.append(friend)
running_level += 1
order = set(sorted(frequency.values()))
for key in frequency:
dictionary[frequency[key]] = dictionary.get(frequency[key], []) + [key]
for rank in order:
solution.extend(sorted(dictionary[rank]))
return solution | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR LIST LIST VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
ID: int,
level: int,
) -> List[str]:
freqs = dict()
visited = set()
visited.add(ID)
q = set()
q.add(ID)
for _ in range(level):
q = {j for i in q for j in friends[i] if j not in visited}
visited |= q
for p in q:
for v in watchedVideos[p]:
freqs[v] = freqs.get(v, 0) + 1
sortedfreqs = sorted([(n, v) for v, n in list(freqs.items())])
return [v for _, v in sortedfreqs] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
n = len(friends)
friend_dist = [math.inf] * n
friend_dist[id] = 0
deque = collections.deque()
deque.append(id)
watched = dict()
while deque:
person = deque.popleft()
if friend_dist[person] == level:
for video in watchedVideos[person]:
watched[video] = watched.get(video, 0) + 1
continue
for friend in friends[person]:
if friend_dist[friend] == math.inf:
friend_dist[friend] = friend_dist[person] + 1
deque.append(friend)
return sorted(watched.keys(), key=lambda v: (watched[v], v)) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.
Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path exactly equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.
Example 1:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
Output: ["B","C"]
Explanation:
You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
Person with id = 1 -> watchedVideos = ["C"]
Person with id = 2 -> watchedVideos = ["B","C"]
The frequencies of watchedVideos by your friends are:
B -> 1
C -> 2
Example 2:
Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
Output: ["D"]
Explanation:
You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).
Constraints:
n == watchedVideos.length == friends.length
2 <= n <= 100
1 <= watchedVideos[i].length <= 100
1 <= watchedVideos[i][j].length <= 8
0 <= friends[i].length < n
0 <= friends[i][j] < n
0 <= id < n
1 <= level < n
if friends[i] contains j, then friends[j] contains i | class Solution:
def watchedVideosByFriends(
self,
watchedVideos: List[List[str]],
friends: List[List[int]],
id: int,
level: int,
) -> List[str]:
n = len(watchedVideos)
dist = [9999] * n
queue = [(id, 0)]
while queue:
idx, depth = queue.pop(0)
if depth < dist[idx]:
dist[idx] = depth
for c in friends[idx]:
queue.append((c, depth + 1))
freq = defaultdict(int)
for i, l in enumerate(dist):
if l == level:
for vid in watchedVideos[i]:
freq[vid] += 1
pairs = sorted([(v, k) for k, v in list(freq.items())])
return [x[-1] for x in pairs] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR VAR VAR VAR |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
stu = [[phy[x], chem[x], math[x]] for x in range(N)]
stu.sort(key=lambda m: [m[0], -m[1], m[2]])
for x in range(N):
phy[x] = stu[x][0]
chem[x] = stu[x][1]
math[x] = stu[x][2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def radix(self, T, x, order):
if order == 0:
T.sort(key=lambda k: k[x])
elif order == 1:
T.sort(key=lambda k: k[x])
T.reverse()
def customSort(self, phy, chem, math, N):
grades = [[] for _ in range(N)]
for i in range(N):
grades[i].append(phy[i])
grades[i].append(chem[i])
grades[i].append(math[i])
self.radix(grades, 2, 1)
self.radix(grades, 1, 1)
self.radix(grades, 0, 0)
for i in range(N):
phy[i] = grades[i][0]
chem[i] = grades[i][1]
math[i] = grades[i][2] | CLASS_DEF FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Node:
def __init__(self, phy, chem, maths):
self.phy = phy
self.chem = chem
self.maths = maths
def __gt__(self, other):
if self.phy < other.phy:
return False
elif self.phy > other.phy:
return True
if self.chem < other.chem:
return True
elif self.chem > other.chem:
return False
if self.maths < other.maths:
return False
elif self.maths > other.maths:
return True
else:
return False
class Solution:
def customSort(self, phy, chem, math, N):
new = []
for i in range(N):
new.append(Node(phy[i], chem[i], maths[i]))
new.sort()
for i in range(N):
phy[i] = new[i].phy
chem[i] = new[i].chem
maths[i] = new[i].maths | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
marks = list(zip(phy, chem, math))
marks.sort(key=lambda x: x[2])
marks.sort(key=lambda x: -x[1])
marks.sort(key=lambda x: x[0])
for i in range(N):
phy[i], chem[i], math[i] = marks[i] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
arr = []
for i in range(N):
arr.append([])
for i in range(N):
arr[i].append(phy[i])
arr[i].append(chem[i])
arr[i].append(math[i])
arr.sort(key=lambda x: x[2])
arr.sort(key=lambda x: x[1], reverse=True)
arr.sort(key=lambda x: x[0])
for i in range(N):
phy[i] = arr[i][0]
chem[i] = arr[i][1]
math[i] = arr[i][2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
mark = []
for i in range(N):
mark.append((phy[i], chem[i], math[i]))
mark.sort(key=lambda x: (x[0], -x[1], x[2]))
for i in range(N):
phy[i], chem[i], math[i] = mark[i] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
d = {}
for i in range(N):
if phy[i] in d.keys():
if chem[i] in d[phy[i]].keys():
d[phy[i]][chem[i]].append(math[i])
else:
d[phy[i]][chem[i]] = [math[i]]
else:
d[phy[i]] = {chem[i]: [math[i]]}
d = sorted(d.items())
for i in range(len(d)):
d[i] = list(d[i])
d[i][1] = sorted(d[i][1].items(), reverse=True)
for k in range(len(d[i][1])):
d[i][1][k] = list(d[i][1][k])
d[i][1][k][1] = sorted(d[i][1][k][1])
z = 0
for i in range(len(d)):
for j in range(len(d[i][1])):
for k in range(len(d[i][1][j][1])):
phy[z] = d[i][0]
chem[z] = d[i][1][j][0]
math[z] = d[i][1][j][1][k]
z += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR IF VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR DICT VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def __init__(self, p=0, c=0, m=0):
self.p = p
self.c = c
self.m = m
def add(self, p, c, m):
self.p = p
self.c = c
self.m = m
def customSort(self, phy, chem, math, N):
arrl = []
for i in range(N):
arrl.append(Solution(phy[i], chem[i], math[i]))
def compare(s1, s2):
if s1.p < s2.p:
return -1
elif s1.p > s2.p:
return 1
elif s1.c < s2.c:
return 1
elif s1.c > s2.c:
return -1
elif s1.m < s2.m:
return -1
elif s1.m > s2.m:
return 1
else:
return 0
arrl.sort(key=lambda x: (x.p, -x.c, x.m))
for i in range(N):
phy[i] = arrl[i].p
chem[i] = arrl[i].c
math[i] = arrl[i].m | CLASS_DEF FUNC_DEF NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR |
You are provided with marks of N students in Physics, Chemistry and Maths.
Perform the following 3 operations:
Sort the students in Ascending order of their Physics marks.
Once this is done, sort the students having same marks in Physics in the descending order of their Chemistry marks.
Once this is also done, sort the students having same marks in Physics and Chemistry in the ascending order of their Maths marks.
Example 1:
Input:
N = 10
phy[] = {4 1 10 4 4 4 1 10 1 10}
chem[] = {5 2 9 6 3 10 2 9 14 10}
math[] = {12 3 6 5 2 10 16 32 10 4}
Output:
1 14 10
1 2 3
1 2 16
4 10 10
4 6 5
4 5 12
4 3 2
10 10 4
10 9 6
10 9 32
Explanation: Firstly, the Physics marks of
students are sorted in ascending order.
Those having same marks in Physics have
their Chemistry marks in descending order.
Those having same marks in both Physics
and Chemistry have their Math marks in
ascending order.
Your Task:
You don't need to read input or print anything. Your task is to complete the function customSort() which takes phy[], chem[], math[] and an integer N denoting the marks and the number of students. The function sorts the marks in the described order and the final changes should be made in the given arrays only.
Expected Time Complexity: O(NLogN).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 10000 | class Solution:
def customSort(self, phy, chem, math, N):
marks = [(phy[i], chem[i], math[i]) for i in range(N)]
marks.sort(key=lambda x: x[2])
marks.sort(key=lambda x: x[1], reverse=True)
marks.sort(key=lambda x: x[0])
for i in range(N):
phy[i] = marks[i][0]
chem[i] = marks[i][1]
math[i] = marks[i][2]
return | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN |
Mr. Modulo lives up to his name and is always busy taking modulo of numbers and making new problems.
Now he comes up with another problem. He has an array of integers with n elements and wants to find a pair of elements {arr[i], arr[j]} such that arr[i] ≥ arr[j] and arr[i] % arr[j] is maximum among all possible pairs where 1 ≤ i, j ≤ n.
Mr. Modulo needs some help to solve this problem. Help him to find this maximum value arr[i] % arr[j].
Example 1:
Input:
n=3
arr[] = {3, 4, 7}
Output: 3
Explanation: There are 3 pairs which satisfies
arr[i] >= arr[j] are:-
4, 3 => 4 % 3 = 1
7, 3 => 7 % 3 = 1
7, 4 => 7 % 4 = 3
Hence maximum value among all is 3.
Example 2:
Input:
n=4
arr[] = {4, 4, 4, 4}
Output: 0
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function maxModValue() that takes array arr and n as parameters and return the required answer.
Expected Time Complexity: O(nLog(n) + Mlog(M)) n is total number of elements and M is maximum value of all the elements.
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ n ≤ 10^{5}
1 ≤ arr[i] ≤ 10^{5} | def binarysearch(arr, l, r, k):
if l <= r:
mid = (l + r) // 2
if arr[mid] < k:
return binarysearch(arr, mid + 1, r, k)
else:
return binarysearch(arr, l, mid - 1, k)
return r
def maxModValue(arr, n):
arr.sort()
m1 = max(arr)
m2 = 0
for i in range(n):
if i > 0 and arr[i] == arr[i - 1]:
continue
ele1 = arr[i]
ele2 = arr[i] * 2
while ele2 <= m1 + ele1:
ele3 = binarysearch(arr, 0, n - 1, ele2)
m2 = max(m2, arr[ele3] % ele1)
ele2 += arr[i]
return m2 | FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR |
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string t_{i} occurs in string s at least k_{i} times or more, he also remembers exactly k_{i} positions where the string t_{i} occurs in string s: these positions are x_{i}, 1, x_{i}, 2, ..., x_{i}, k_{i}. He remembers n such strings t_{i}.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings t_{i} and string s consist of small English letters only.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 10^5) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string t_{i}, then positive integer k_{i}, which equal to the number of times the string t_{i} occurs in string s, and then k_{i} distinct positive integers x_{i}, 1, x_{i}, 2, ..., x_{i}, k_{i} in increasing order — positions, in which occurrences of the string t_{i} in the string s start. It is guaranteed that the sum of lengths of strings t_{i} doesn't exceed 10^6, 1 ≤ x_{i}, j ≤ 10^6, 1 ≤ k_{i} ≤ 10^6, and the sum of all k_{i} doesn't exceed 10^6. The strings t_{i} can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
-----Output-----
Print lexicographically minimal string that fits all the information Ivan remembers.
-----Examples-----
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | from sys import stdin, stdout
sze = 10**6 + 1
n = int(stdin.readline())
challengers = []
strings = []
sze = 10**6 + 1
cnt = [(0) for i in range(sze)]
for i in range(n):
s = stdin.readline().strip().split()
num = int(s[1])
values = list(map(int, s[2:]))
strings.append(s[0])
for j in range(num):
if not cnt[values[j]]:
cnt[values[j]] = i, len(s[0])
elif cnt[values[j]][1] < len(s[0]):
cnt[values[j]] = i, len(s[0])
previous = 1
for i in range(sze):
if not cnt[i]:
continue
ind, s = i, cnt[i][0]
s = strings[s]
if previous < ind:
stdout.write(str("a") * (ind - previous))
previous = ind
if previous > ind + len(s) - 1:
continue
else:
stdout.write(s[previous - ind : len(s)])
previous = ind + len(s) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
sorted_a = sorted(a)
count = 0
j = 0
for i in range(n):
if a[i] == sorted_a[j]:
count += 1
j += 1
print(n - count) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
for _ in range(t):
a = []
n = int(input())
i = 0
for x in input().split():
a.append((int(x), i))
i += 1
a.sort()
ans = 0
ans = n - 1
for i in range(1, n):
if a[i][1] > a[i - 1][1]:
ans -= 1
else:
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | T = int(input())
for t in range(T):
n = int(input())
A = [int(i) for i in input().split()]
B = A[:]
B.sort()
ct = 0
r = 0
mini = max(A) + 1
sufmin = [0] * len(A)
sufmin[-1] = A[-1]
for i in range(n - 2, -1, -1):
sufmin[i] = min(sufmin[i + 1], A[i])
for i in range(len(A) - 1):
if A[i] > sufmin[i]:
mini = min(mini, A[i])
ct = 0
for i in range(len(A)):
if A[i] >= mini:
ct += 1
print(ct) | 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | try:
t = int(input())
for i in range(t):
n = int(input())
c = 0
l = list(map(int, input().split()))
l1 = sorted(l)
for j in range(n):
if l[j] == l1[c]:
c += 1
print(n - c)
except:
pass | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for se in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
l = sorted(arr)
i = j = 0
while i < n and j < n:
if l[i] == arr[j]:
i, j = i + 1, j + 1
else:
j = j + 1
print(n - i) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
if a == b:
print(0)
else:
count = 0
k = 0
for i in range(n):
if a[i] != b[k]:
count += 1
else:
k += 1
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
ans = []
while t:
t = t - 1
n = int(input())
arr = list(map(int, input().split()))
mi = min(arr)
sarr = sorted(arr)
moves = 0
i, j = arr.index(mi), 0
for i in range(arr.index(mi), n):
j += sarr[j] == arr[i]
ans.append(n - j)
for x in ans:
print(x, end="\n") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
b = sorted(a)
i = 0
j = 0
while i < n and j < n:
if b[i] == a[j]:
i += 1
j += 1
else:
j += 1
k = n - i
print(k) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
i = 0
while t > i:
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = x = k = 0
flag = 0
if a == b:
flag = 1
else:
for x in range(n):
if a[x] != b[k]:
c += 1
else:
k += 1
if flag == 1:
print(0)
else:
print(c)
i += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for _ in range(int(input())):
n = int(input())
z = input().split()
a = list(map(int, z[:n]))
b = sorted(a) + [0]
bx = 0
dis = b[-2] + 1
for ai in a:
if ai == b[bx]:
bx += 1
if b[bx] == dis:
break
elif ai < dis:
dis = ai
print(n - bx) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | T = int(input())
for t in range(T):
n = int(input())
A = [int(i) for i in input().split()]
B = A[:]
B.sort()
ct = 0
r = 0
for i in range(len(A)):
if A[i] == B[r]:
r += 1
print(n - r) | 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | li = []
def fun(k):
li.append(int(k))
for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
b = sorted(a)
i = 0
j = 0
while i < n and j < n:
if b[i] == a[j]:
i += 1
j += 1
else:
j += 1
k = n - i
fun(k)
for i in range(len(li)):
print(li[i]) | ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
while t > 0:
t -= 1
flag = 0
n = int(input())
count = 0
a = [int(i) for i in input().strip().split(" ")]
a_sort = sorted(a)
index = 0
for i in range(0, n):
if a_sort[index] == a[i]:
count += 1
index += 1
print(n - count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
l = list(arr)
l.sort()
i = j = 0
while i < n and j < n:
if l[i] == arr[j]:
i = i + 1
j = j + 1
else:
j = j + 1
mov = n - i
print(mov) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | import sys
def handle(a):
b = [(i, v) for i, v in enumerate(a)]
b.sort(key=lambda p: p[1])
cnt = 1
l = len(b)
for i in range(1, l):
if b[i][0] > b[i - 1][0]:
cnt += 1
else:
break
return l - cnt
tline = sys.stdin.readline()
t = int(tline.strip())
for i in range(t):
_line = sys.stdin.readline()
line = sys.stdin.readline()
a = list(map(int, line.strip().split()))
print(handle(a)) | IMPORT FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | T = int(input())
for i in range(T):
N = int(input())
arr = list(map(int, input().split()))
arr2 = [] + arr
arr2.sort()
j = 0
q = 0
while j < N and q < N:
while j < N and arr[j] != arr2[q]:
j += 1
if j < N and arr[j] == arr2[q]:
q += 1
print(N - q) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ind = n - 1
while ind > 0 and a[ind - 1] < a[ind]:
ind = ind - 1
if ind == 0:
print(0)
else:
j = ind
mini = a[j]
value = a[j - 1]
j -= 2
while j >= 0:
if a[j] < mini:
mini = a[j]
elif a[j] < value:
value = a[j]
j = j - 1
chote = 0
for j in range(0, n):
if a[j] < value:
chote += 1
print(n - chote) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | for _ in range(int(input())):
n = int(input())
it = list(map(int, input().split()))
s = {}
tt = sorted(it)
j = 1
for i in tt:
s[i] = j
j += 1
ma = n + 1
mi = 0
for i in it[-1::-1]:
if s[i] > ma:
mi = max(mi, n - s[i] + 1)
ma = min(ma, s[i])
print(mi) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
while t > 0:
m = int(input())
n = [int(x) for x in input().split()]
c = n.copy()
c.sort()
j = 0
for i in n:
if i == c[j]:
j += 1
print(m - j)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
s = list(a)
a.sort()
b = list(a)
b.reverse()
c = 0
for i in s:
x = a[c]
if i == x:
c = c + 1
ans1 = n - c
print(ans1) | 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | t = int(input())
while t:
n = int(input())
arr = list(map(int, input().split()))
m = max(arr)
minr = arr[-1]
for i in range(n - 1, 0, -1):
minr = min(minr, arr[i])
if arr[i - 1] > minr:
m = min(m, arr[i - 1])
if m == max(arr) and arr[-1] == m:
moves = 0
else:
arr.sort()
moves = n - arr.index(m)
print(moves)
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Chef loves sorting! Chef recently learnt about a new form of sorting, endsort!
While performing endsort of an array $A$ of $N$ distinct elements, Chef can move any element to the end of the array.
However, Chef fears that this will take too much time. Help Chef find the number of moves to sort the given array in ascending order using endsort!
------ Input: ------
The first line contains a single integer $T$, the number of test cases.
The first line of each test case contains a single integer $N$, the number of elements of given array $A$.
The next line would contain $N$ space separated integers, the elements of the array.
------ Output: ------
Print a single integer, the minimum number of moves to sort the array in ascending order using endsort.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 100000$
$1 ≤ A_{i} ≤ 10^{9}$
----- Sample Input 1 ------
2
5
1 2 3 4 5
5
1 3 2 5 4
----- Sample Output 1 ------
0
3
----- explanation 1 ------
For the second sample, in the first move, we move 3 to the end of the array. In the second move, we move 4 to the end and finally in the third move, we move 5 to the end. | def number_of_sorting(n, arr):
c = 0
p = 0
l = sorted(arr)
for i in range(n):
if arr[i] == l[p]:
c = c + 1
p = p + 1
return n - c
T = int(input())
for i in range(T):
N = int(input())
arr = [int(x) for x in input().split()]
print(number_of_sorting(N, arr)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
group_x_coords = collections.defaultdict(list)
group_y_coords = collections.defaultdict(set)
for point in points:
group_x_coords[point[0]].append(point[1])
group_y_coords[point[1]].add(point[0])
min_area = float("inf")
for key in group_x_coords.keys():
if len(group_x_coords[key]) < 2:
continue
for i in range(len(group_x_coords[key])):
for j in range(i + 1, len(group_x_coords[key])):
y_val_1 = group_x_coords[key][i]
y_val_2 = group_x_coords[key][j]
common_x_coords = group_y_coords[y_val_1] & group_y_coords[y_val_2]
if len(common_x_coords) > 1:
y_diff = abs(y_val_1 - y_val_2)
x_diff = float("inf")
for ele in common_x_coords:
if ele == key:
continue
if abs(ele - key) < x_diff:
x_diff = abs(ele - key)
area = x_diff * y_diff
if area < min_area:
min_area = area
if min_area == float("inf"):
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | from itertools import product
class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points or not points[0]:
return 0
m, n = len(points), len(points[0])
d_row, d_col = {}, {}
for r, c in points:
d_row[r] = d_row.get(r, []) + [c]
d_col[c] = d_col.get(c, []) + [r]
for r in d_row:
d_row[r] = sorted(d_row[r])
for c in d_col:
d_col[c] = sorted(d_col[c])
area = float("inf")
for r, c in points:
c2s = [x for x in d_row[r] if x > c]
r2s = [x for x in d_col[c] if x > r]
if not c2s or not r2s:
continue
for r2, c2 in product(r2s, c2s):
if c2 in d_row[r2]:
area = min(area, (r2 - r) * (c2 - c))
return area if area < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution(object):
def minAreaRect(self, points):
min_area = sys.maxsize
points_table = set()
for x, y in points:
points_table.add((x, y))
for x1, y1 in points:
for x2, y2 in points:
if x1 > x2 and y1 > y2:
if (x1, y2) in points_table and (x2, y1) in points_table:
area = abs(x1 - x2) * abs(y1 - y2)
if area:
min_area = min(area, min_area)
return 0 if min_area == sys.maxsize else min_area | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
hs = set()
N = len(points)
res = float("inf")
for i in range(N):
x1, y1 = points[i]
for x2, y2 in hs:
if (x1, y2) in hs and (x2, y1) in hs:
res = min(res, abs(x1 - x2) * abs(y1 - y2))
hs.add((x1, y1))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xys = set()
for point in points:
xys.add((point[0], point[1]))
sq_min = float("inf")
for i, pointi in enumerate(points):
xi, yi = pointi[0], pointi[1]
for j in range(i + 1, len(points)):
xj, yj = points[j][0], points[j][1]
if xi != xj and yi != yj and (xi, yj) in xys and (xj, yi) in xys:
sq_min = min(sq_min, abs((xi - xj) * (yi - yj)))
return sq_min if sq_min != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
area = float("inf")
corners = set([tuple(x) for x in points])
for i in range(len(points)):
for j in range(i + 1, len(points)):
if points[i][0] != points[j][0] and points[i][1] != points[j][1]:
if (points[i][0], points[j][1]) in corners and (
points[j][0],
points[i][1],
) in corners:
l, w = abs(points[i][0] - points[j][0]), abs(
points[i][1] - points[j][1]
)
area = min(area, l * w)
return 0 if area == float("inf") else area | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pointSet = set()
for x, y in points:
pointSet.add((x, y))
length = len(points)
minArea = float("inf")
for i in range(length):
for j in range(i, length):
p1 = points[i]
p2 = points[j]
if p1[0] != p2[0] and p1[1] != p2[1]:
if (p1[0], p2[1]) in pointSet and (p2[0], p1[1]) in pointSet:
minArea = min(minArea, abs(p1[0] - p2[0]) * abs(p1[1] - p2[1]))
if minArea == float("inf"):
return 0
return minArea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
area = float("inf")
seen = set()
for x1, y1 in points:
for x2, y2 in seen:
if x1 != x2 and y1 != y2:
if (x1, y2) in seen and (x2, y1) in seen:
l, w = abs(x1 - x2), abs(y1 - y2)
area = min(area, l * w)
seen.add((x1, y1))
return 0 if area == float("inf") else area | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
base_x_to_y = collections.defaultdict(dict)
y_for_base_x = []
base_x = -1
min_area = float("inf")
for x, y in sorted(points):
if x != base_x:
base_x = x
y_for_base_x = []
for prev_base_x in y_for_base_x:
if y in base_x_to_y[prev_base_x]:
w = x - base_x_to_y[prev_base_x][y]
h = y - prev_base_x
area = w * h
min_area = min(min_area, area)
base_x_to_y[prev_base_x][y] = x
y_for_base_x.append(y)
return min_area if min_area < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points_set = set((r, c) for r, c in points)
m = 2**32
for i in range(len(points)):
x1, y1 = points[i]
for j in range(i, len(points)):
x2, y2 = points[j]
if x2 == x1 or y2 == y1:
continue
if (x2, y1) not in points_set:
continue
if (x1, y2) not in points_set:
continue
area = abs(x1 - x2) * abs(y1 - y2)
m = min(m, area)
if m == 2**32:
return 0
else:
return m | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
nx, ny = len(set(x for x, _ in points)), len(set(y for _, y in points))
if nx == n or ny == n:
return 0
p = collections.defaultdict(list)
if nx > ny:
for x, y in points:
p[x].append(y)
else:
for x, y in points:
p[y].append(x)
res = float("inf")
dic_last = {}
for x1, x2 in itertools.combinations(p, 2):
if len(p[x1]) < 2 or len(p[x2]) < 2:
continue
for y1, y2 in itertools.combinations(sorted(set(p[x1]) & set(p[x2])), 2):
res = min(res, abs(x1 - x2) * (y2 - y1))
return res if res < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
cols = defaultdict(list)
for p in points:
cols[p[0]].append(p[1])
last_x = {}
ans = float("inf")
for x in sorted(cols):
for i in range(len(cols[x])):
for j in range(i + 1, len(cols[x])):
if (cols[x][i], cols[x][j]) in last_x:
ans = min(
ans,
(cols[x][j] - cols[x][i])
* (x - last_x[cols[x][i], cols[x][j]]),
)
last_x[cols[x][i], cols[x][j]] = x
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
cache_x = defaultdict(set)
cache_y = defaultdict(set)
cache = set()
for i, p in enumerate(points):
cache_x[p[0]].add(i)
cache_y[p[1]].add(i)
cache.add((p[0], p[1]))
res = float("inf")
for i, (x0, y0) in enumerate(points):
cache_x[x0].remove(i)
cache_y[y0].remove(i)
cache.remove((x0, y0))
for ix in cache_y[y0]:
for iy in cache_x[x0]:
x1, y1 = points[ix][0], points[iy][1]
if (x1, y1) in cache:
res = min(res, abs(x1 - x0) * abs(y1 - y0))
return 0 if res == float("inf") else res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xD = collections.defaultdict(list)
for x, y in points:
xD[x].append(y)
sol = math.inf
lastSeen = collections.defaultdict(int)
for x in sorted(xD.keys()):
for i1 in range(len(xD[x])):
for i2 in range(i1 + 1, len(xD[x])):
pair1, pair2, diff = (
(xD[x][i1], xD[x][i2]),
(xD[x][i2], xD[x][i1]),
abs(xD[x][i1] - xD[x][i2]),
)
if pair1 in lastSeen or pair2 in lastSeen:
sol = min(diff * (x - lastSeen[pair1]), sol)
lastSeen[pair1], lastSeen[pair2] = x, x
return sol if sol != math.inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
min_area = math.inf
seen = set()
for x0, y0 in points:
for x1, y1 in seen:
if (x0, y1) in seen and (x1, y0) in seen:
area = abs(x0 - x1) * abs(y0 - y1)
if area > 0:
min_area = min(min_area, area)
seen.add((x0, y0))
return 0 if min_area == math.inf else min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
res = 2**31
s = {(p[0], p[1]) for p in points}
for i, p1 in enumerate(points):
for j, p2 in enumerate(points[i + 1 :]):
if p1[0] != p2[0] and p1[1] != p2[1]:
if (p1[0], p2[1]) in s and (p2[0], p1[1]) in s:
res = min(res, abs(p1[1] - p2[1]) * abs(p1[0] - p2[0]))
res = res if res < 2**31 else 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution(object):
def minAreaRect(self, points):
seen = set()
res = float("inf")
for x1, y1 in points:
for x2, y2 in seen:
if (x1, y2) in seen and (x2, y1) in seen:
area = abs(x1 - x2) * abs(y1 - y2)
if area and area < res:
res = area
seen.add((x1, y1))
return res if res < float("inf") else 0 | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
maps = set(map(tuple, points))
points.sort()
n = len(points)
result = math.inf
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
if x2 == x1 or y1 == y2:
continue
if (x1, y2) in maps and (x2, y1) in maps:
result = min(result, (x2 - x1) * abs(y2 - y1))
return 0 if math.isinf(result) else result | CLASS_DEF FUNC_DEF 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 VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
_set = {(x, y) for x, y in points}
res = math.inf
for p1, p2 in combinations(points, 2):
arr = [p1, p2]
arr.sort()
(x1, y1), (x2, y2) = arr
l, w = y2 - y1, x2 - x1
if l > 0 and w > 0 and (x1, y1 + l) in _set and (x1 + w, y1) in _set:
res = min(res, l * w)
return 0 if math.isinf(res) else res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
n = len(points)
point_set = {(x, y) for x, y in points}
min_area = math.inf
for i in range(n):
x0, y0 = points[i]
for j in range(i + 1, n):
x1, y1 = points[j]
if (x0, y1) in point_set and (x1, y0) in point_set:
area = abs(x0 - x1) * abs(y0 - y1)
if area > 0:
min_area = min(min_area, area)
return 0 if min_area == math.inf else min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
dictY = collections.defaultdict(list)
for i, (x, y) in enumerate(points):
dictY[y].append(i)
y2Xpair = collections.defaultdict(set)
for y in dictY.keys():
if len(dictY[y]) <= 1:
continue
for p1, p2 in itertools.combinations(dictY[y], 2):
x1, _, x2, _ = points[p1] + points[p2]
y2Xpair[y].add((min(x1, x2), max(x1, x2)))
res = float("inf")
for (y1, p1), (y2, p2) in itertools.combinations(y2Xpair.items(), 2):
h = abs(y1 - y2)
for x1, x2 in p1 & p2:
res = min(res, (x2 - x1) * h)
return 0 if res == float("inf") else res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
h = defaultdict(deque)
v = defaultdict(deque)
p = set([(x, y) for x, y in points])
for x, y in points:
h[x].append(y)
v[y].append(x)
minArea = float("inf")
for x, y in points:
for y1 in h[x]:
if y1 != y:
for x1 in v[y]:
if x1 != x:
if (x1, y1) in p:
minArea = min(minArea, abs(y1 - y) * abs(x1 - x))
if h[x]:
h[x].popleft()
if v[y]:
v[y].popleft()
if minArea == float("inf"):
return 0
return minArea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.