Amazon SDE1 Solutions.py

(Arrays & Strings — Easy)

  1. Two Sum

    def two_sum(nums: List[int], target: int) -> List[int]:
    	m = {}
    	for i, x in enumerate(nums):
    		if target - x in m:
    			return [m[target - x], i]
    		m[x] = i
    	return []
    
  2. Best Time to Buy and Sell Stock

    def max_profit(prices: List[int]) -> int:
    	min_price, max_profit = float('inf'), 0
    	for p in prices:
    		min_price = min(min_price, p)
    		max_profit = max(max_profit, p - min_price)
    	return max_profit
    
  3. Valid Anagram

    def is_anagram(s: str, t:str) -> bool:
    	return Counter(s) == Counter(t)
    
  4. Remove Duplicates from Sorted Array

    
    
  5. String Compression


(Medium)

  1. Longest Substring Without Repeating Characters
  2. Product of Array Except Self
  3. 3Sum
  4. Subarray Sum Equals K
  5. Minimum Window Substring
  6. Longest Palindromic Substring
  7. Rotate Array
  8. Matrix — Number of Islands
  9. Search in Rotated Sorted Array
  10. Top K Frequent Elements