四角形の検出 ---パイセップ--- 「四角形の検出」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- X-Y 平面上の一連の点が与えられます。次のようなデータ構造を設計します。 - ストリームから新しいポイントを追加します。重複したポイントは許可されており、別のポイントとして扱う必要があります。 - クエリ点が与えられると、3 つの点とクエリ点が正の面積を持つ軸に沿った正方形を形成するように、データ構造から 3 つの点を選択する方法の数をカウントします。 軸に沿った正方形とは、辺がすべて同じ長さで、x 軸と y 軸に平行または垂直な正方形です。 関数 __PYCODE_0__ を実装します。操作は 'DetectSquares'、'add'、または 'count' で、引数は対応するパラメーターです。結果のリストを返します (コンストラクターと追加の場合はなし)。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 数学と幾何学 ---パイセップ--- 「正方形の検出」問題は、数学と幾何学セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 四角形の検出のロジック フローを視覚化します。 ---パイセップ--- 四角形の検出の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- 数学と幾何学のアプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準の数学および幾何学問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどの数学と幾何学に固有のデータ構造の使用を検討してください。 ---パイセップ--- サブセット ---パイセップ--- 「サブセット」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- 一意の要素の整数配列を指定すると、考えられるすべてのサブセット (べき集合) を返します。 ソリューション セットには重複したサブセットが含まれていてはなりません。任意の順序で解を返します。 関数 __PYCODE_0__ を実装します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- 後戻り ---パイセップ--- 「サブセット」問題は、バックトラッキングセクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- サブセットのロジック フローを視覚化します。 ---パイセップ--- サブセットの問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Detect Squares' problem.
1. 学ぶ
The 'Detect Squares' problem is a key challenge in the Math & Geometry section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Detect Squares.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Detect Squares carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
問題提起
You are given a stream of points on the X-Y plane. Design a data structure that:
- Adds new points from the stream. Duplicate points are allowed and should be treated as different points.
- Given a query point, counts the number of ways to choose three points from the data structure such that the three points and the query point form an axis-aligned square with positive area.
An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and the y-axis.
Implement a function detectSquares(operations: list, arguments: list) -> list where operations are 'DetectSquares', 'add', or 'count', and arguments are the corresponding parameters. Returns a list of results (None for constructor and add).
- •point.length == 2
- •0 <= x, y <= 1000
- •At most 3000 calls in total will be made to add and count
例
["DetectSquares","add","add","add","count","count","add","count"], [[],[3,10],[11,2],[3,2],[11,10],[14,8],[11,2],[11,10]]
[None,None,None,None,1,0,None,2]
After adding (3,10), (11,2), (3,2): count(11,10) finds 1 square with corners (3,10),(11,10),(11,2),(3,2). count(14,8) finds 0. After adding another (11,2): count(11,10) finds 2 squares (using each copy of (11,2)).
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
解決する準備はできましたか?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
インタビューの洞察とバリエーション
複雑さの分析の内訳
なぜ時間がかかるのか: Directly evaluates all possibilities.
なぜ宇宙なのか: Uses standard local memory.
なぜ時間がかかるのか: Optimized paths reduce total operations.
なぜ宇宙なのか: May trade memory for speed.
最適化されたソリューションの Python コード
最適化されたソリューションの Python コード
from collections import defaultdict
class DetectSquaresOpt:
def __init__(self):
self.ptsCount = defaultdict(int)
self.pts = []
def add(self, point: list[int]) -> None:
self.ptsCount[tuple(point)] += 1
self.pts.append(point)
def count(self, point: list[int]) -> int:
res = 0
px, py = point
for x, y in self.pts:
if abs(px - x) == abs(py - y) and px != x and py != y:
res += self.ptsCount[(x, py)] * self.ptsCount[(px, y)]
return resブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
class DetectSquaresBrute:
def __init__(self):
self.points = []
def add(self, point: list[int]) -> None:
self.points.append(point)
def count(self, point: list[int]) -> int:
res = 0
px, py = point
for x, y in self.points:
if abs(px - x) == abs(py - y) and px != x and py != y:
c1 = self.points.count([x, py])
c2 = self.points.count([px, y])
res += c1 * c2
return resAlgorithm Pattern Checklist
When dealing with Math & Geometry data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Math & Geometry problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python ループ
Python ループを使用してデータを反復処理する方法を学びます。インタラクティブな例を使用して、for ループ、while ループ、ブレーク、継続、ループのベスト プラクティスをマスターします。
Python でリストを並べ替える方法
sort() メソッドとsorted() 関数を使用して、Python でリストを並べ替える方法を学びます。カスタムキーの並べ替えと逆順の例をご覧ください。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。