{"id":5027,"date":"2022-06-19T11:01:00","date_gmt":"2022-06-19T16:01:00","guid":{"rendered":"https:\/\/wanderin.dev\/?p=5027"},"modified":"2024-10-06T10:10:07","modified_gmt":"2024-10-06T15:10:07","slug":"two-sum-leetcode-1","status":"publish","type":"post","link":"https:\/\/wanderin.dev\/coding-challenges\/two-sum-leetcode-1\/","title":{"rendered":"Two Sum (LeetCode #1)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"h-challenge-statement\">Challenge Statement<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Given an array of integers,&nbsp;<em>nums,<\/em>&nbsp;and an integer target,&nbsp;<em>target<\/em>, return&nbsp;<em>indices of the two numbers such that they add up to target<\/em>.<\/li><li>You may assume that each input would have&nbsp;<strong><em>exactly<\/em>&nbsp;one solution<\/strong>, and you may not use the&nbsp;<em>same<\/em>&nbsp;element twice.<\/li><li>You can return the answer in any order.<\/li><li>This challenge corresponds to&nbsp;<a href=\"https:\/\/leetcode.com\/problems\/two-sum\/\" target=\"_blank\" rel=\"noreferrer noopener\">LeetCode #1<\/a>.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Constraints<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= len(nums) &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup> &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>-10<sup>9<\/sup> &lt;= target &lt;= 10<sup>9<\/sup><\/code><\/li><li><strong>Only one valid answer exists.<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1:<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong><code>&nbsp;nums = [2, 7, 11, 15]<\/code>, <code>target = 9<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;<code>[0, 1]<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong>&nbsp;Because <code>nums[0] + nums[1] == 9<\/code>, we return<code> [0, 1]<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2:<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong>&nbsp;<code>nums = [3, 2, 4]<\/code>, <code>target = 6<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;<code>[1, 2]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3:<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong><code>&nbsp;nums = [3, 3]<\/code>, <code>target = 6<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong>&nbsp;<code>[0, 1]<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-solution\">Solution<\/h2>\n\n\n\n<p>Below is my solution and some test cases.  This solution has a <strong>linear time complexity O(n)<\/strong> and a <strong>linear space complexity O(n)<\/strong>, where <em>n<\/em> is the length of the input list <em>nums<\/em>.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/wanderindev\/decff1bf8a1695529af5b694dd4f7de4.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>This post contains my solution to LeetCode&#8217;s problem #1, Two Sum.<\/p>\n","protected":false},"author":152069353,"featured_media":5280,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[4504556,4504566],"tags":[4504555,4504569,4504572],"class_list":["post-5027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding-challenges","category-easy","tag-coding-challenge","tag-easy","tag-hashing"],"jetpack_featured_media_url":"https:\/\/wanderin.dev\/wp-content\/uploads\/2022\/07\/1.png","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/5027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/users\/152069353"}],"replies":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/comments?post=5027"}],"version-history":[{"count":15,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/5027\/revisions"}],"predecessor-version":[{"id":5327,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/posts\/5027\/revisions\/5327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/media\/5280"}],"wp:attachment":[{"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/media?parent=5027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/categories?post=5027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wanderin.dev\/wp-json\/wp\/v2\/tags?post=5027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}