{"card":{"problem":{"id":1,"slug":"two-sum","title":"Two Sum","difficulty":"Easy","companies":{"google":{"30d":{"rank":1,"frequency":100.0},"3m":{"rank":1,"frequency":100.0},"6m":{"rank":1,"frequency":100.0},"all":{"rank":1,"frequency":100.0}},"meta":{"30d":{"rank":3,"frequency":93.2},"3m":{"rank":1,"frequency":100.0},"6m":{"rank":1,"frequency":100.0},"all":{"rank":6,"frequency":92.9}},"amazon":{"30d":{"rank":1,"frequency":100.0},"3m":{"rank":1,"frequency":100.0},"6m":{"rank":1,"frequency":100.0},"all":{"rank":1,"frequency":100.0}},"microsoft":{"30d":{"rank":1,"frequency":100.0},"3m":{"rank":1,"frequency":100.0},"6m":{"rank":1,"frequency":100.0},"all":{"rank":1,"frequency":100.0}},"apple":{"6m":{"rank":14,"frequency":68.6},"all":{"rank":2,"frequency":85.8}},"nvidia":{"6m":{"rank":14,"frequency":73.7},"all":{"rank":4,"frequency":85.4}},"bloomberg":{"30d":{"rank":1,"frequency":100.0},"3m":{"rank":1,"frequency":100.0},"6m":{"rank":1,"frequency":100.0},"all":{"rank":1,"frequency":100.0}},"uber":{"all":{"rank":59,"frequency":54.6}},"tiktok":{"3m":{"rank":7,"frequency":80.9},"6m":{"rank":17,"frequency":62.1},"all":{"rank":26,"frequency":66.5}},"oracle":{"3m":{"rank":1,"frequency":100.0},"6m":{"rank":2,"frequency":93.7},"all":{"rank":2,"frequency":85.3}},"salesforce":{"all":{"rank":102,"frequency":35.3}},"linkedin":{"all":{"rank":58,"frequency":51.0}},"adobe":{"all":{"rank":1,"frequency":100.0}},"snowflake":{"all":{"rank":24,"frequency":66.8}},"doordash":{"all":{"rank":35,"frequency":44.9}},"airbnb":{"all":{"rank":19,"frequency":66.3}},"atlassian":{"all":{"rank":41,"frequency":34.3}},"tesla":{"all":{"rank":20,"frequency":61.5}},"databricks":{"all":{"rank":25,"frequency":36.9}}},"url":"https://leetcode.com/problems/two-sum","topics":["Array","Hash Table"],"premium":false,"description":"<p>You are given an array of integers <code>nums</code> and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>\n\n<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>\n\n<p>You can return the answer in any order.</p>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,7,11,15], target = 9\n<strong>Output:</strong> [0,1]\n<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,4], target = 6\n<strong>Output:</strong> [1,2]\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,3], target = 6\n<strong>Output:</strong> [0,1]\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>\n\t<li><strong>Only one valid answer exists.</strong></li>\n</ul>\n\n<p> </p>\n<strong>Follow-up: </strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?"},"approach":{"insight":"A hash map gives each complement lookup in constant time.","steps":["Make an empty map from value to index.","For each index `i`, find `target - nums[i]` in the map.","If the map has it, return both indices.","Else, put `nums[i]` in the map with index `i`."],"time":"O(n)","space":"O(n)","hints":["For each number, the partner value `target - x` is fixed, so ask whether that partner appeared earlier."]},"state":null,"note":null},"remaining":793,"topics":["Array","String","Hash Table","Math","Dynamic Programming","Sorting","Greedy","Depth-First Search","Binary Search","Matrix","Breadth-First Search","Tree","Two Pointers","Bit Manipulation","Prefix Sum","Heap (Priority Queue)","Simulation","Binary Tree","Stack","Database","Graph Theory","Counting","Sliding Window","Design","Enumeration","Backtracking","Union-Find","Linked List","Number Theory","Ordered Set","Monotonic Stack","Segment Tree","Divide and Conquer","Recursion","Trie","Queue","Combinatorics","Binary Search Tree","Bitmask","Geometry","Memoization","Topological Sort","Hash Function","Binary Indexed Tree","Shortest Path","JavaScript","String Matching","DP on Trees","Game Theory","Interactive","Data Stream","Rolling Hash","Greatest Common Divisor","Euclidean Algorithm","Directed Acyclic Graph","Monotonic Queue","Dijkstra's Algorithm","Bracket Sequences","Brainteaser","Minimax","Knapsack Problem","Doubly-Linked List","Merge Sort","Binary Lifting","Prime Factorization","Randomized","Zero-Sum Game","Z Algorithm","Lowest Common Ancestor","Knuth–Morris–Pratt Algorithm","Iterator","Counting Sort","Longest Increasing Subsequence","Fermat's Little Theorem","Pandas","Sweep Line","0-1 Knapsack","Bipartite Graph","Bidirectional Search","Primality Test","Sieve Theory","Quickselect","Flow Network","Concurrency","Quicksort","Floyd's Cycle Finding Algorithm","Bucket Sort","Treap","Polygons","Longest Common Subsequence","Suffix Array","Minimum Spanning Tree","Boyer–Moore String-Search Algorithm","Pigeonhole Principle","Prime Number Sieve","Complete Knapsack","A* Search","Heuristic Search","Cartesian Tree","Graph Coloring","Least Common Multiple","Matching (Graph)","Bubble Sort","Shell","Impartial Game","Sqrt Decomposition","Reservoir Sampling","Probability and Statistics","Aho–Corasick Algorithm","Kosaraju's Algorithm","Tarjan's SCC Algorithm","Meet in the Middle","Successive Shortest Path Algorithm","Suffix Automaton","Suffix Tree","Algorithm X","Radix Sort","Boyer–Moore Majority Vote Algorithm","Nim Game","Linear Algebra","Eulerian Circuit","Eulerian Path","Inclusion-Exclusion Principle","Hungarian Algorithm","Minimum-Cost Flow","Prim's Algorithm","Kruskal's Algorithm","Borůvka's Algorithm","Edmonds–Karp Algorithm","MPM Algorithm","Push-Relabel Algorithm","Dinic's Algorithm","Manacher","Range Minimum/Maximum Query","Brute-Force Search","Sprague–Grundy Theorem","Semi-Eulerian Graph","Bézout's Lemma","Extended Euclidean Algorithm","Bellman–Ford Algorithm","Floyd–Warshall Algorithm","Rejection Sampling","Closest Pair of Points","Ternary Search","Hamiltonian Path","Maximum Flow","Maximum Matching","0-1 BFS","Strongly Connected Component","Tournament Sort","Dancing Links","Newton's Method","Euler's Totient Function","Euler's Theorem","Convex Hull","Eulerian Graph","Timsort","Lexicographically Minimal String Rotation","Planar Graph","K-D Tree","Triangulation","Persistent Data Structure","Lyndon Factorization","Biconnected Component","Bridge (Graph)","Minimum Cut","Articulation Point","Splay Tree","Mixed Knapsack","Minimum Enclosing Circle","Perfect Matching","K Shortest Path"]}