diff --git a/LeetCode/September Challenge/best_time_to_sell_and_buy.py b/LeetCode/September Challenge/best_time_to_sell_and_buy.py new file mode 100644 index 0000000..a0eae28 --- /dev/null +++ b/LeetCode/September Challenge/best_time_to_sell_and_buy.py @@ -0,0 +1,27 @@ +""" +121. Best Time to Buy and Sell Stock +------------------------------------ +Say you have an array for which the ith element is the price of a given stock on day i. +If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. +Note that you cannot sell a stock before you buy one. + +Example 1: +Input: [7,1,5,3,6,4] +Output: 5 +Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. + Not 7-1 = 6, as selling price needs to be larger than buying price. +""" + +class Solution(object): + def maxProfit(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + max_profit = 0 + min_price = float('inf') + + for price in prices: + min_price = min(min_price, price) + max_profit = max(max_profit, price - min_price) + return max_profit \ No newline at end of file diff --git a/LeetCode/September Challenge/bulls_and_cows.py b/LeetCode/September Challenge/bulls_and_cows.py new file mode 100644 index 0000000..9d46b35 --- /dev/null +++ b/LeetCode/September Challenge/bulls_and_cows.py @@ -0,0 +1,37 @@ +""" +299. Bulls and Cows +------------------- +You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint that indicates how many digits in said guess match your secret number exactly in both digit and position (called "bulls") and how many digits match the secret number but locate in the wrong position (called "cows"). Your friend will use successive guesses and hints to eventually derive the secret number. +Write a function to return a hint according to the secret number and friend's guess, use A to indicate the bulls and B to indicate the cows. +Please note that both secret number and friend's guess may contain duplicate digits. + +Example 1: +Input: secret = "1807", guess = "7810" +Output: "1A3B" +Explanation: 1 bull and 3 cows. The bull is 8, the cows are 0, 1 and 7. + +Example 2: +Input: secret = "1123", guess = "0111" +Output: "1A1B" +Explanation: The 1st 1 in friend's guess is a bull, the 2nd or 3rd 1 is a cow. +""" + +class Solution(object): + def getHint(self, secret, guess): + """ + :type secret: str + :type guess: str + :rtype: str + """ + + bulls = 0 + + for idx, char in enumerate(secret): + if char == guess[idx]: + bulls+=1 + + cows = 0 + + cows = sum((Counter(secret)&Counter(guess)).values()) - bulls + + return str(bulls) + "A" + str(cows) + "B" diff --git a/LeetCode/September Challenge/car_pooling.py b/LeetCode/September Challenge/car_pooling.py new file mode 100644 index 0000000..ecd935a --- /dev/null +++ b/LeetCode/September Challenge/car_pooling.py @@ -0,0 +1,48 @@ +""" +Car Pooling +----------- +You are driving a vehicle that has capacity empty seats initially available for passengers. The vehicle only drives east (ie. it cannot turn around and drive west.) +Given a list of trips, trip[i] = [num_passengers, start_location, end_location] contains information about the i-th trip: the number of passengers that must be picked up, and the locations to pick them up and drop them off. The locations are given as the number of kilometers due east from your vehicle's initial location. +Return true if and only if it is possible to pick up and drop off all passengers for all the given trips. + +Example 1: +Input: trips = [[2,1,5],[3,3,7]], capacity = 4 +Output: false + +Example 2: +Input: trips = [[2,1,5],[3,3,7]], capacity = 5 +Output: true + +Example 3: +Input: trips = [[2,1,5],[3,5,7]], capacity = 3 +Output: true + +Example 4: +Input: trips = [[3,2,7],[3,7,9],[8,3,9]], capacity = 11 +Output: true +""" + +class Solution(object): + def carPooling(self, trips, capacity): + """ + :type trips: List[List[int]] + :type capacity: int + :rtype: bool + """ + time = [] + + for trip in trips: + time.append([trip[1], trip[0]]) + time.append([trip[2], - trip[0]]) + + time.sort() + + total_passengers_at_a_time = 0 + + for ele in time: + total_passengers_at_a_time+=ele[1] + if total_passengers_at_a_time > capacity: + return False + + return True + \ No newline at end of file diff --git a/LeetCode/September Challenge/compare_version_number.py b/LeetCode/September Challenge/compare_version_number.py new file mode 100644 index 0000000..93d717e --- /dev/null +++ b/LeetCode/September Challenge/compare_version_number.py @@ -0,0 +1,53 @@ +""" +Compare two version numbers version1 and version2. +If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. +You may assume that the version strings are non-empty and contain only digits and the . character. +The . character does not represent a decimal point and is used to separate number sequences. +For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision. +You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0. + +Example 1: +Input: version1 = "0.1", version2 = "1.1" +Output: -1 + +Example 2: +Input: version1 = "1.0.1", version2 = "1" +Output: 1 + +Example 3: +Input: version1 = "7.5.2.4", version2 = "7.5.3" +Output: -1 + +Example 4: +Input: version1 = "1.01", version2 = "1.001" +Output: 0 +Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1” + +Example 5: +Input: version1 = "1.0", version2 = "1.0.0" +Output: 0 +Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0" + + +Note: +Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes. +Version strings do not start or end with dots, and they will not be two consecutive dots. +""" +class Solution(object): + def compareVersion(self, version1, version2): + """ + :type version1: str + :type version2: str + :rtype: int + """ + versions1 = version1.split(".") + versions2 = version2.split(".") + for i in range(max(len(versions1),len(versions2))): + v1 = int(versions1[i]) if i < len(versions1) else 0 + v2 = int(versions2[i]) if i < len(versions2) else 0 + if v1 > v2: + return 1 + elif v1 =0: + if str(s[i]) == '': + pass + else: + return len(s[i]) + i-=1 + + return 0 \ No newline at end of file diff --git a/LeetCode/September Challenge/partition_list.py b/LeetCode/September Challenge/partition_list.py new file mode 100644 index 0000000..8cac716 --- /dev/null +++ b/LeetCode/September Challenge/partition_list.py @@ -0,0 +1,35 @@ +""" +A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts. + +Example 1: +Input: S = "ababcbacadefegdehijhklij" +Output: [9,7,8] +Explanation: +The partition is "ababcbaca", "defegde", "hijhklij". +This is a partition so that each letter appears in at most one part. +A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts. +""" +class Solution(object): + def partitionLabels(self, S): + """ + :type S: str + :rtype: List[int] + """ + ans = [] + lastIndex = {} + for idx, char in enumerate(S): + lastIndex[char] = idx + start = end = 0 + + for idx, char in enumerate(S): + end = max(end, lastIndex[char]) + if idx == end: + ans.append(idx - start + 1) + start = idx + 1 + + return ans + +""" +Runtime: 20 ms, faster than 98.32% of Python online submissions for Partition Labels. +Memory Usage: 12.7 MB, less than 83.64% of Python online submissions for Partition Labels. +""" \ No newline at end of file diff --git a/LeetCode/September Challenge/repeated_substring_pattern.py b/LeetCode/September Challenge/repeated_substring_pattern.py new file mode 100644 index 0000000..c187a43 --- /dev/null +++ b/LeetCode/September Challenge/repeated_substring_pattern.py @@ -0,0 +1,36 @@ +""" +459. Repeated Substring Pattern +------------------------------- +Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000. + +Example 1: +Input: "abab" +Output: True +Explanation: It's the substring "ab" twice. +""" +class Solution(object): + def repeatedSubstringPattern(self, s): + """ + :type s: str + :rtype: bool + """ + + n = len(s) + + if n < 1: + return False + temp = '' + + idx = 0 + while idx <= (n//2) and idx != (n-1): + temp+=s[idx] + if n % (idx+1) == 0 and temp * (n/(idx+1)) == s: + return True + idx+=1 + + return False + +""" +-> Runtime: 108 ms, faster than 53.80% of Python online submissions for Repeated Substring Pattern. +-> Memory Usage: 12.9 MB, less than 91.17% of Python online submissions for Repeated Substring Pattern. +""" \ No newline at end of file diff --git a/LeetCode/September Challenge/word_pattern.py b/LeetCode/September Challenge/word_pattern.py new file mode 100644 index 0000000..d9528ff --- /dev/null +++ b/LeetCode/September Challenge/word_pattern.py @@ -0,0 +1,33 @@ +""" +Given a pattern and a string str, find if str follows the same pattern. +Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. + +Example 1: +Input: pattern = "abba", str = "dog cat cat dog" +Output: true + +Example 2: +Input:pattern = "abba", str = "dog cat cat fish" +Output: false +""" +class Solution(object): + def wordPattern(self, pattern, str): + words = str.split(' ') + if len(set(list(pattern))) != len(set(words)): + return False + + wordDict = {} + index = 0 + length = len(pattern) + for i in words: + if index >= length: + return False + key = pattern[index] + if key in wordDict and wordDict[key] != i: + return False + elif key not in wordDict: + wordDict[key] = i + index += 1 + return True + + \ No newline at end of file