Leet Code

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

242. Valid Anagram Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

1. Create 1 dictionary for each string (2 total) 2. Iterate through each character and add to dictionary, use letter as key and number of occurrences as the value 3. See if the two dictionaries are equal, return true if so, else return false sdict = {} tdict = {} for c in s: if c not in sdict: sdict[c] = 1 else: sdict[c] += 1 for c in t: if c not in tdict: tdict[c] = 1 else: tdict[c] += 1 if sdict == tdict: return True else: return False

1. Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

1. create a dictionary 2. loop through the array of integers 3. for each element, check if it is in the dictionary 4. if in the dictionary, return the index of the element as well as the value received when the element is used as a key for the dictionary 4. otherwise, add the complementary value to the dictionary by using (target - element) as the key and the index as the value def twoSum(self, nums, target): dict = {} for i in range(len(nums)): if nums[i] in dict: return [i, dict[nums[i]]] else: dict[target - nums[i]] = i

704. Binary Search Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.

1. create a right and left pointer. The right = len(nums) - 1 while the left = 0 2. Utilize while loop and find the mid element of the array. 3. If the mid == target, return mid 4. If the target is greater than the middle element, reassign the left pointer to the mid + 1 5. If the target is less than the middle element, reassign the right pointer to the mid - 1 def search(self, nums, target): lowerIndx, upperIndx = 0, len(nums) - 1 while (lowerIndx <= upperIndx): mid = lowerIndx + (upperIndx - lowerIndx) // 2 if nums[mid] == target: return mid if target > nums[mid]: lowerIndx = mid + 1 else: upperIndx = mid - 1 return -1

169. Majority Element Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

1. create empty dictionary 2. iterate through the array and check if each element exists in the dictionary (as a key) 3. if the key exists, add 1 to the value of the key ( dict[elem] ) 4. if the key doesn't exist, assign the value 1 to the dict[elem] key. 5. return the key with the max value using: max(dict, key = dict.get) def majorityElement(self, nums): dict = {} for elem in nums: if elem in dict: dict[elem] = dict[elem] + 1 else: dict[elem] = 1 return max(dict, key =dict.get)

121. 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.

1. loop through the array prices 2. choose the first element as the min and adjust when a smaller min is encountered 3. compare min to every next element to see which yields max profit def maxProfit(self, prices): maxProfit = 0 min = prices[0] for i in range(1, len(prices)): if prices[i] < min: min = prices[i] profit = prices[i] - min if profit > maxProfit: maxProfit = profit return maxProfit


Kaugnay na mga set ng pag-aaral

Reward Systems Management Final (CH 1-6)

View Set

reading comprehension - The Midwife's Apprenticeby Karen Cushman

View Set

Curriculum Design and Implementation

View Set

Medical Terminology: Chapter 2 Exercise 2-2

View Set