TEACHIFY Telegram 3973
✔️ چالش برنامه‌نویسی: Two Sum

یکی از سوالات پرطرفدار در مصاحبه‌های الگوریتمی، مسئله‌ی Two Sum است.

در این مسئله، یک لیست از اعداد و یک مقدار هدف (target) داده می‌شود. باید دو عددی که مجموع آن‌ها برابر مقدار هدف است را پیدا کنیم و ایندکس‌هایشان را برگردانیم.

مثال‌ها:
input: nums = [2,7,11,15], target = 9
output: [0,1] ( nums[0] + nums[1] = 9)
input: nums = [3,2,4], target = 6
output: [1,2]
input: nums = [3,3], target = 6
output: [0,1]

حل بهینه در پایتون (O(n)): استفاده از دیکشنری برای کاهش پیچیدگی زمانی!
def two_sum(nums, target):
    hash_map = {}
   
    for i, num in enumerate(nums):
        complement = target - num
        if complement in hash_map:
            return [hash_map[complement], i]
        hash_map[num] = i
   
    return []
]

+ مقدار مورد نیاز برای رسیدن به target را حساب کن.
+ اگر قبلاً در hash_map ذخیره شده بود، ایندکس‌ها را برگردان.
+ در غیر این صورت، مقدار فعلی را در hash_map ذخیره کن.

#برنامه‌نویسی #الگوریتم #پایتون #مصاحبه_شغلی
@Teachify | برنامه نویسی
Please open Telegram to view this post
VIEW IN TELEGRAM
1👍9❤‍🔥21



tgoop.com/teachify/3973
Create:
Last Update:

✔️ چالش برنامه‌نویسی: Two Sum

یکی از سوالات پرطرفدار در مصاحبه‌های الگوریتمی، مسئله‌ی Two Sum است.

در این مسئله، یک لیست از اعداد و یک مقدار هدف (target) داده می‌شود. باید دو عددی که مجموع آن‌ها برابر مقدار هدف است را پیدا کنیم و ایندکس‌هایشان را برگردانیم.

مثال‌ها:
input: nums = [2,7,11,15], target = 9
output: [0,1] ( nums[0] + nums[1] = 9)
input: nums = [3,2,4], target = 6
output: [1,2]
input: nums = [3,3], target = 6
output: [0,1]

حل بهینه در پایتون (O(n)): استفاده از دیکشنری برای کاهش پیچیدگی زمانی!

def two_sum(nums, target):
    hash_map = {}
   
    for i, num in enumerate(nums):
        complement = target - num
        if complement in hash_map:
            return [hash_map[complement], i]
        hash_map[num] = i
   
    return []
]

+ مقدار مورد نیاز برای رسیدن به target را حساب کن.
+ اگر قبلاً در hash_map ذخیره شده بود، ایندکس‌ها را برگردان.
+ در غیر این صورت، مقدار فعلی را در hash_map ذخیره کن.

#برنامه‌نویسی #الگوریتم #پایتون #مصاحبه_شغلی
@Teachify | برنامه نویسی

BY برنامه نویسی | Teachify




Share with your friend now:
tgoop.com/teachify/3973

View MORE
Open in Telegram


Telegram News

Date: |

ZDNET RECOMMENDS While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc. Although some crypto traders have moved toward screaming as a coping mechanism, several mental health experts call this therapy a pseudoscience. The crypto community finds its way to engage in one or the other way and share its feelings with other fellow members. Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation. How to build a private or public channel on Telegram?
from us


Telegram برنامه نویسی | Teachify
FROM American