- 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
- 93 lines of Python from the credited upstream file 3015.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 def countOfPairs(self, n: int, x: int, y: int) -> list[int]:3 if x > y:4 x, y = y, x5 6 def bothInRing(ringLen: int) -> list[int]:7 """8 Returns the contribution from the scenario where two houses are located9 in the ring.10 """11 res = [0] * n12 for k in range(1, (ringLen - 1) 2 + 1):13 res[k - 1] += ringLen14 if ringLen % 2 == 0:15 res[ringLen 2 - 1] += ringLen 216 return res17 18 def bothInTheSameLine(lineLen: int) -> list[int]:19 """20 Returns the contribution from the scenario where two houses are either21 located in the left line [1, x) or the right line (y, n].22 """23 res = [0] * n24 for k in range(1, lineLen + 1):25 res[k - 1] += lineLen - k26 return res27 28 def lineToRing(lineLen: int, ringLen: int) -> list[int]:29 """30 Returns the contribution from the scenario where one house is either31 located in the left line [1, x) or the right line (y, n] and the32 other house is located in the cycle.33 """34 res = [0] * n35 for k in range(1, lineLen + ringLen):36 37 38 39 40 41 maxInRingLen = min(k - 1, ringLen 2)42 43 minInRingLen = max(0, k - lineLen)44 if minInRingLen <= maxInRingLen:45 46 47 48 49 50 51 res[k - 1] += (maxInRingLen - minInRingLen + 1) * 252 if minInRingLen == 0:53 54 res[k - 1] -= 155 if maxInRingLen * 2 == ringLen:56 57 58 59 res[k - 1] -= 160 return res61 62 def lineToLine(leftLineLen: int, rightLineLen: int) -> list[int]:63 """64 Returns the contribution from the scenario where one house is in the left65 line [1, x) and the other house is in the right line (y, n].66 """67 res = [0] * n68 for k in range(leftLineLen + rightLineLen + 2):69 70 71 72 73 74 maxInLeft = min(leftLineLen, k - 1 - (x < y))75 76 minInLeft = max(1, k - rightLineLen - (x < y))77 if minInLeft <= maxInLeft:78 res[k - 1] += maxInLeft - minInLeft + 179 return res80 81 ringLen = y - x + 182 leftLineLen = x - 183 rightLineLen = (n - y)84 85 ans = [0] * n86 ans = list(map(operator.add, ans, bothInRing(ringLen)))87 ans = list(map(operator.add, ans, bothInTheSameLine(leftLineLen)))88 ans = list(map(operator.add, ans, bothInTheSameLine(rightLineLen)))89 ans = list(map(operator.add, ans, lineToRing(leftLineLen, ringLen)))90 ans = list(map(operator.add, ans, lineToRing(rightLineLen, ringLen)))91 ans = list(map(operator.add, ans, lineToLine(leftLineLen, rightLineLen)))92 return [freq * 2 for freq in ans]93