{"id":33590,"date":"2024-11-01T09:18:14","date_gmt":"2024-11-01T09:18:14","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33590"},"modified":"2024-11-01T12:28:32","modified_gmt":"2024-11-01T12:28:32","slug":"%ed%95%9c%ea%b8%80-%ec%bd%94%eb%94%a9-%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-%ea%b5%ac%ea%b0%84-%ed%95%a9","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33590\/","title":{"rendered":"Python coding test course, interval sum"},"content":{"rendered":"<p><body><\/p>\n<p>Hello, everyone! Today we will tackle the range sum problem, which frequently appears in coding tests using Python. The range sum problem asks how to quickly calculate the sum of a specific range, and it&#8217;s an important concept that forms the basis of many algorithmic problems. We will explore various approaches to solve this problem.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given an array as follows, write a program to efficiently calculate the sum of specific ranges for multiple query pairs.<\/p>\n<pre><code>Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nQuery: [(1, 3), (2, 5), (3, 7)]<\/code><\/pre>\n<p>When the above array and queries are provided, the queries are given in the form of (start index, end index). For example, the query (1, 3) means that we need to calculate the sum from index 1 to index 3 of the array (1-based index). In this case, it should be 2 + 3 + 4 = 9.<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>There are various basic approaches to solve the range sum problem. I will start with the simplest method and explain more efficient ones.<\/p>\n<h3>1. Using Simple Iteration<\/h3>\n<p>The most intuitive method is to calculate the sum for the required range directly. This method is good when there are few queries, but it is inefficient when there are many. We can implement it as follows.<\/p>\n<pre><code>def simple_range_sum(arr, queries):\n    results = []\n    for start, end in queries:\n        results.append(sum(arr[start - 1:end]))  # 1-based index to 0-based index\n    return results\n\n\narr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nqueries = [(1, 3), (2, 5), (3, 7)]\nprint(simple_range_sum(arr, queries))  # Output: [9, 14, 27]<\/code><\/pre>\n<p>When we execute the code above, the results for the queries will output [9, 14, 27]. However, the time complexity of this method is O(Q * N), where Q is the number of queries and N is the size of the array. This becomes inefficient for large inputs.<\/p>\n<h3>2. Using a Cumulative Sum Array<\/h3>\n<p>A more efficient way is to create a cumulative sum array. Once we have the cumulative sum array, we can calculate the sum of each range in O(1) time. The method is as follows.<\/p>\n<pre><code>def prefix_sum(arr):\n    n = len(arr)\n    prefix = [0] * (n + 1)\n    for i in range(1, n + 1):\n        prefix[i] = prefix[i - 1] + arr[i - 1]\n    return prefix\n\n\ndef efficient_range_sum(arr, queries):\n    prefix = prefix_sum(arr)\n    results = []\n    for start, end in queries:\n        results.append(prefix[end] - prefix[start - 1])  # 1-based index adjustment\n    return results\n\n\narr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nqueries = [(1, 3), (2, 5), (3, 7)]\nprint(efficient_range_sum(arr, queries))  # Output: [9, 14, 27]<\/code><\/pre>\n<p>In the above code, calculating the cumulative sum array takes O(N) time, and processing each query takes O(1) time. Thus, the overall time complexity reduces to O(N + Q). This method allows us to handle multiple queries efficiently.<\/p>\n<h2>Problem Optimization and Selection<\/h2>\n<p>The range sum problem can be utilized in various situations, and the algorithm chosen may vary depending on the conditions of the problem. It is essential to choose the optimal method considering the size of the input array, the number of queries, and the length of each query&#8217;s range.<\/p>\n<p>Besides the methods described above, more complex data structures such as segment trees can also be used, but this tutorial focused on basic methods for solving practical problems. In the next lesson, we will address more complicated dynamic query problems.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lesson, we covered the range sum problem using Python. We found that while simple iteration can be inefficient, using a cumulative sum array allows us to solve the problem much more efficiently. The range sum problem is a very useful topic for building basic algorithm skills, so I encourage you to practice it multiple times and try various variations!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, everyone! Today we will tackle the range sum problem, which frequently appears in coding tests using Python. The range sum problem asks how to quickly calculate the sum of a specific range, and it&#8217;s an important concept that forms the basis of many algorithmic problems. We will explore various approaches to solve this problem. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33590\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python coding test course, interval sum&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[145],"tags":[],"class_list":["post-33590","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/33590\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello, everyone! Today we will tackle the range sum problem, which frequently appears in coding tests using Python. The range sum problem asks how to quickly calculate the sum of a specific range, and it&#8217;s an important concept that forms the basis of many algorithmic problems. We will explore various approaches to solve this problem. &hellip; \ub354 \ubcf4\uae30 &quot;Python coding test course, interval sum&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33590\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:28:32+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python coding test course, interval sum\",\"datePublished\":\"2024-11-01T09:18:14+00:00\",\"dateModified\":\"2024-11-01T12:28:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/\"},\"wordCount\":477,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33590\/\",\"name\":\"Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:14+00:00\",\"dateModified\":\"2024-11-01T12:28:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33590\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33590\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python coding test course, interval sum\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/33590\/","og_locale":"ko_KR","og_type":"article","og_title":"Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello, everyone! Today we will tackle the range sum problem, which frequently appears in coding tests using Python. The range sum problem asks how to quickly calculate the sum of a specific range, and it&#8217;s an important concept that forms the basis of many algorithmic problems. We will explore various approaches to solve this problem. &hellip; \ub354 \ubcf4\uae30 \"Python coding test course, interval sum\"","og_url":"https:\/\/atmokpo.com\/w\/33590\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:14+00:00","article_modified_time":"2024-11-01T12:28:32+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33590\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33590\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python coding test course, interval sum","datePublished":"2024-11-01T09:18:14+00:00","dateModified":"2024-11-01T12:28:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33590\/"},"wordCount":477,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33590\/","url":"https:\/\/atmokpo.com\/w\/33590\/","name":"Python coding test course, interval sum - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:14+00:00","dateModified":"2024-11-01T12:28:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33590\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33590\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33590\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python coding test course, interval sum"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33590"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33590\/revisions"}],"predecessor-version":[{"id":38053,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33590\/revisions\/38053"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}