Top 150 Interview簡単

四角形の検出 ---パイセップ--- 「四角形の検出」問題の詳細なガイドと __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.

問題提起

簡単

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

Example 1
Input
["DetectSquares","add","add","add","count","count","add","count"], [[],[3,10],[11,2],[3,2],[11,10],[14,8],[11,2],[11,10]]
Output
[None,None,None,None,1,0,None,2]
Explanation

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?
Consider using Math & Geometry-specific data structures like sets or heaps.
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.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。