Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions challenge-1/two_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Two_sum:
def twosum(self,target,nums):
prev_map=dict()
for i,num in enumerate(nums):
if target - num in prev_map:
return[i,prev_map[target-num]]
prev_map[num] = i

two_sum = Two_sum()
print(two_sum.twosum(9,[2,7,11,15,9]))



29 changes: 29 additions & 0 deletions challenge-2/Best_time_to_buyandsell_stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
"""


class Solution:
def maxProfit(self, prices):
#Two-pointer
left,right=0,1
profit=0

while right < len(prices):
#profitable
if prices[right] > prices[left]:
profit=max(profit,prices[right] - prices[left])
else:
#nonprofitable
left=right
right += 1
return profit

stock= Solution()
print(stock.maxProfit([7,1,5,3,6,4]))
29 changes: 29 additions & 0 deletions challenge-3/Contains_Duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example 1:

Input: nums = [1,2,3,1]

Output: true

Explanation:

The element 1 occurs at the indices 0 and 3.
"""

class Solution:
def containsDuplicate(self, nums):
hash_set=set()
for num in nums:
if num in hash_set:
return True
else:
hash_set.add(num)
return False

duplicate = Solution()
print(duplicate.containsDuplicate([1,2,3,1]))
print(duplicate.containsDuplicate([1,2,3,4]))



18 changes: 18 additions & 0 deletions challenge-4/Sum_of_Two_Integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Given two integers a and b, return the sum of the two integers without using the operators + and -.

# Example 1:
# Input: a = 1, b = 2
# Output: 3

class Solution:
def getSum(self, a, b) :
bitshortner = 0xFFFFFFFF

while (b & bitshortner) > 0:
carry = (a & b) << 1
a = a ^ b
b = carry
return (a & bitshortner) if b > 0 else a

sum = Solution()
print(sum.getSum(1,2))
21 changes: 21 additions & 0 deletions challenge-5/Product_of_Array_Except_Self.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://leetcode.com/problems/product-of-array-except-self/

# Product of Array Except Self


class Solution:
def productExceptSelf(self, nums):
res=[1] * (len(nums))
prefix=1
for i in range(len(nums)):
res[i] = prefix
prefix *= nums[i]
postfix=1
for i in range(len(nums)-1, -1, -1):
res[i] *= postfix
postfix *= nums[i]
return res


product = Solution()
print(product.productExceptSelf([1,2,3,4]))