Problem : 3015 Easy to understand solution | Beats 100%🔥time and space
Intuition\n\uD83E\uDD14 The problem seems to involve finding pairs with a specific cost in a grid-like structure. The cost depends on the distance between the p
# Intuition\n\uD83E\uDD14 The problem seems to involve finding pairs with a specific cost in a grid-like structure. The cost depends on the distance between the pairs in various ways. We need to count the number of pairs for each possible cost.\n\n# Approach\n\uD83D\uDEE0\uFE0F I will iterate through all possible pairs of points and calculate the cost according to the provided conditions. I will use a defaultdict to store pairs based on their costs. After that, I will iterate through the costs and count the number of pairs for each cost.\n\n# Complexity\n- Time complexity: \uD835\uDC42(\uD835\uDC5B^2), where \uD835\uDC5B is the number of points.\n- Space complexity: \uD835\uDC42(\uD835\uDC5B^2), as we store pairs for each cost.\n\n# Code\n```python\nfrom typing import List\nfrom collections import defaultdict\n\nclass Solution:\n    def countOfPairs(self, n: int, x: int, y: int) -> List[int]:\n        result = [0] * n\n        min_cost = defaultdict(list)\n\n        for i in range(1, n + 1):\n            for j in range(1, n + 1):\n                if (i, j) != (x, y) and (j, i) != (x, y):\n                    cost = min(abs(j - i), abs(x - i) + abs(y - j) + 1, abs(y - i) + abs(x - j) + 1)\n                    min_cost[cost].append((i, j))\n\n        min_cost[1].append((x, y))\n        min_cost[1].append((y, x))\n        \n        for k in range(1, n + 1):\n            for pair in min_cost[k]:\n                i, j = pair\n                cost = min(abs(j - i), abs(x - i) + abs(y - j) + 1, abs(y - i) + abs(x - j) + 1)\n                if cost == k:\n                    result[k - 1] += 1\n\n        return result\n```
['Python3']