- Choose the invariant that makes a window valid or useful.
- Advance the right boundary and add the new element.
- Move the left boundary only as needed while maintaining the invariant and updating the answer.
Code notes
- 94 lines of Python from the credited upstream file 3017.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution:2 3 def countOfPairs(self, n: int, x: int, y: int) -> list[int]:4 if x > y:5 x, y = y, x6 7 def bothInRing(ringLen: int) -> list[int]:8 """9 Returns the contribution from the scenario where two houses are located10 in the ring.11 """12 res = [0] * n13 for k in range(1, (ringLen - 1) 2 + 1):14 res[k - 1] += ringLen15 if ringLen % 2 == 0:16 res[ringLen 2 - 1] += ringLen 217 return res18 19 def bothInTheSameLine(lineLen: int) -> list[int]:20 """21 Returns the contribution from the scenario where two houses are either22 located in the left line [1, x) or the right line (y, n].23 """24 res = [0] * n25 for k in range(1, lineLen + 1):26 res[k - 1] += lineLen - k27 return res28 29 def lineToRing(lineLen: int, ringLen: int) -> list[int]:30 """31 Returns the contribution from the scenario where one house is either32 located in the left line [1, x) or the right line (y, n] and the33 other house is located in the cycle.34 """35 res = [0] * n36 for k in range(1, lineLen + ringLen):37 38 39 40 41 42 maxInRingLen = min(k - 1, ringLen 2)43 44 minInRingLen = max(0, k - lineLen)45 if minInRingLen <= maxInRingLen:46 47 48 49 50 51 52 res[k - 1] += (maxInRingLen - minInRingLen + 1) * 253 if minInRingLen == 0:54 55 res[k - 1] -= 156 if maxInRingLen * 2 == ringLen:57 58 59 60 res[k - 1] -= 161 return res62 63 def lineToLine(leftLineLen: int, rightLineLen: int) -> list[int]:64 """65 Returns the contribution from the scenario where one house is in the left66 line [1, x) and the other house is in the right line (y, n].67 """68 res = [0] * n69 for k in range(leftLineLen + rightLineLen + 2):70 71 72 73 74 75 maxInLeft = min(leftLineLen, k - 1 - (x < y))76 77 minInLeft = max(1, k - rightLineLen - (x < y))78 if minInLeft <= maxInLeft:79 res[k - 1] += maxInLeft - minInLeft + 180 return res81 82 ringLen = y - x + 183 leftLineLen = x - 184 rightLineLen = (n - y)85 86 ans = [0] * n87 ans = list(map(operator.add, ans, bothInRing(ringLen)))88 ans = list(map(operator.add, ans, bothInTheSameLine(leftLineLen)))89 ans = list(map(operator.add, ans, bothInTheSameLine(rightLineLen)))90 ans = list(map(operator.add, ans, lineToRing(leftLineLen, ringLen)))91 ans = list(map(operator.add, ans, lineToRing(rightLineLen, ringLen)))92 ans = list(map(operator.add, ans, lineToLine(leftLineLen, rightLineLen)))93 return [freq * 2 for freq in ans]94