From 9eec4bc9f2d5240ecaecba3586a8973ea4dcb68c Mon Sep 17 00:00:00 2001 From: Bianca Henderson Date: Sun, 7 May 2023 11:54:20 -0400 Subject: [PATCH 1/5] More practice things --- Puzzles/PracticeMay2023/03_most_candies.py | 33 +++++++ .../PracticeMay2023/04_can_place_flowers.py | 63 +++++++++++++ Puzzles/PracticeMay2023/README.md | 88 ++++++++++++++++++- 3 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 Puzzles/PracticeMay2023/03_most_candies.py create mode 100644 Puzzles/PracticeMay2023/04_can_place_flowers.py diff --git a/Puzzles/PracticeMay2023/03_most_candies.py b/Puzzles/PracticeMay2023/03_most_candies.py new file mode 100644 index 0000000..9231859 --- /dev/null +++ b/Puzzles/PracticeMay2023/03_most_candies.py @@ -0,0 +1,33 @@ +# There are n kids with candies. You are given an integer array of candies, +# where each candies[i] represents the number of candies the ith kid has, +# and an integer extraCandies, denoting the number of extra candies that +# you have. + +# Return a boolean array result of length n, where result[i] is true if, +# after giving the ith kid all the extraCandies, they will have the greatest +# number of candies among all the kids, or false otherwise. + +# Note that multiple kids can have the greatest number of candies. + +import numpy as np +from types import List + +class Solution: + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + sorted_list = sorted(candies) + highest_number = sorted_list[-1] + determination = [] + for i in range(len(candies)): + if (candies[i] + extraCandies) >= highest_number: + determination.append(True) + continue + if (candies[i] + extraCandies) < highest_number: + determination.append(False) + continue + return determination + + +class BetterSolution: + # A better solution than the above 🤯 + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + return np.array(candies) + extraCandies >= np.array(candies).max() diff --git a/Puzzles/PracticeMay2023/04_can_place_flowers.py b/Puzzles/PracticeMay2023/04_can_place_flowers.py new file mode 100644 index 0000000..64e4158 --- /dev/null +++ b/Puzzles/PracticeMay2023/04_can_place_flowers.py @@ -0,0 +1,63 @@ +# You have a long flowerbed in which some of the plots are planted, +# and some are not. However, flowers cannot be planted in adjacent plots. + +# Given an integer array flowerbed containing 0's and 1's, where 0 means +# empty and 1 means not empty, and an integer n, return true if n new flowers +# can be planted in the flowerbed without violating the no-adjacent-flowers +# rule and false otherwise. + +import numpy as np +from types import List + +class Solution: + def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: + if n <= 0: + return True + elif len(flowerbed) == 1: + if flowerbed == [0]: + return True + else: + return False + # Create an array that is 1 where a is 0, and pad each end with an extra 0. + iszero = np.concatenate(([0], np.equal(flowerbed, 0).view(np.int8), [0])) + absdiff = np.abs(np.diff(iszero)) + # Runs start and end where absdiff is 1. + ranges = np.where(absdiff == 1)[0].reshape(-1, 2) + for [x, y] in ranges: + difference = y - x + if difference == 2: + # if the plot size is 2 but at either edge, then a flower + # can be planted there + if x == 0 or y == len(flowerbed): + n = n - 1 + else: + pass + elif difference <= 2: + # if the potential plot is 2 or smaller but not at the edge, + # the automatic answer is "can't plant a flower here" + pass + elif (difference % 2) == 0: + # this takes care of even-numbered cases larger than 4 + if x == 0 or y == len(flowerbed): + d = difference // 2 + n = n - d + else: + d = difference // 2 + d = d - 1 + n -= d + elif (difference % 2) != 0: + # this takes care of odd-numbered cases larger than 3 + if x == 0 or y == len(flowerbed): + d = difference // 2 + d = d + 1 + n -= d + else: + d = difference // 2 + n -= d + else: + d = difference // 2 + n -= d + if n <= 0: + return True + else: + return False diff --git a/Puzzles/PracticeMay2023/README.md b/Puzzles/PracticeMay2023/README.md index ca1dcd5..bbdf97f 100644 --- a/Puzzles/PracticeMay2023/README.md +++ b/Puzzles/PracticeMay2023/README.md @@ -49,8 +49,8 @@ merged: a p b q c d ### Constraints: -`1 <= word1.length`, `word2.length <= 100` -`word1` and `word2` consist of lowercase English letters. +* `1 <= word1.length`, `word2.length <= 100` +* `word1` and `word2` consist of lowercase English letters. ### Acceptance Rate: `82.8%` @@ -92,3 +92,87 @@ Output: "" ### Acceptance Rate `56.5%` + + +* * * + + +## 3. Kids With the Greatest Number of Candies + +### Status: ✅ + +There are `n` kids with candies. You are given an integer array of candies, where each `candies[i]` represents the number of candies the `i`th kid has, and an integer `extraCandies`, denoting the number of extra candies that you have. + +_Return a boolean array result of length `n`, where `result[i]` is true if, after giving the ith kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or false otherwise._ + +> Note that multiple kids can have the greatest number of candies. + +### Example 1: +``` +Input: candies = [2,3,5,1,3], extraCandies = 3 +Output: [true,true,true,false,true] +``` +Explanation: If you give all `extraCandies` to: +- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids. +- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids. +- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids. +- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids. +- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids. + +### Example 2: +``` +Input: candies = [4,2,1,1,2], extraCandies = 1 +Output: [true,false,false,false,false] +``` +Explanation: There is only 1 extra candy. + +Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy. + +### Example 3: +``` +Input: candies = [12,1,12], extraCandies = 10 +Output: [true,false,true] +``` + +### Constraints: +* `n == candies.length` +* `2 <= n <= 100` +* `1 <= candies[i] <= 100` +* `1 <= extraCandies <= 50` + +### Acceptance Rate +`88.0%` + + +* * * + + +## 4. Can Place Flowers + +### Status: ✅ + +You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. + +Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return true if `n` new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise. + +### Example 1: +``` +Input: flowerbed = [1,0,0,0,1], n = 1 +Output: true +``` + +### Example 2: +``` +Input: flowerbed = [1,0,0,0,1], n = 2 +Output: false +``` + +### Constraints: + +* `1 <= flowerbed.length <= 2 * 104` +* `flowerbed[i]` is `0` or `1`. +* There are no two adjacent flowers in `flowerbed`. +* `0 <= n <= flowerbed.length` + +### Acceptance Rate +`32.7%` From 457ae53a2fa4ad328b51a7d63da18fa59404b721 Mon Sep 17 00:00:00 2001 From: Bianca Henderson Date: Mon, 22 May 2023 22:16:30 -0400 Subject: [PATCH 2/5] Adding more chapters --- Puzzles/PracticeMay2023/05_reverse_vowels.py | 0 Puzzles/PracticeMay2023/06_reverse_words.py | 17 +++++++++++ Puzzles/PracticeMay2023/README.md | 31 ++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 Puzzles/PracticeMay2023/05_reverse_vowels.py create mode 100644 Puzzles/PracticeMay2023/06_reverse_words.py diff --git a/Puzzles/PracticeMay2023/05_reverse_vowels.py b/Puzzles/PracticeMay2023/05_reverse_vowels.py new file mode 100644 index 0000000..e69de29 diff --git a/Puzzles/PracticeMay2023/06_reverse_words.py b/Puzzles/PracticeMay2023/06_reverse_words.py new file mode 100644 index 0000000..294239a --- /dev/null +++ b/Puzzles/PracticeMay2023/06_reverse_words.py @@ -0,0 +1,17 @@ +class Solution: + def reverseWords(self, s: str) -> str: + word_list = s.split(" ") + reversed = [] + reversed_string = "" + for word in word_list: + if word: + reversed.append(word) + reversed.reverse() + for i in reversed: + reversed_string = reversed_string + " " + i + return reversed_string.strip() + +# This is a better solution than the above 🤯 +class BetterSolution: + def reverseWords(self, s: str) -> str: + return " ".join(s.split()[::-1]) \ No newline at end of file diff --git a/Puzzles/PracticeMay2023/README.md b/Puzzles/PracticeMay2023/README.md index bbdf97f..d0c30a0 100644 --- a/Puzzles/PracticeMay2023/README.md +++ b/Puzzles/PracticeMay2023/README.md @@ -176,3 +176,34 @@ Output: false ### Acceptance Rate `32.7%` + +* * * + + +## 5. Reverse Vowels of a String + +### Status: ✅ + +Given a string `s`, reverse only all the vowels in the string and return it. + +The vowels are `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`, and they can appear in both lower and upper cases, more than once. + +### Example 1: +``` +Input: s = "hello" +Output: "holle" +``` + +### Example 2: +``` +Input: s = "leetcode" +Output: "leotcede" +``` + +### Constraints: + +* `1 <= s.length <= 3 * 105` +* `s` consist of printable ASCII characters + +### Acceptance Rate +`50.3%` From 130a67256ca59290e310d3437d521b7f502fe8a0 Mon Sep 17 00:00:00 2001 From: Bianca Henderson Date: Fri, 2 Jun 2023 22:26:40 -0400 Subject: [PATCH 3/5] =?UTF-8?q?Add=20more=20puzzles=20=F0=9F=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Puzzles/PracticeMay2023/05_reverse_vowels.py | 26 ++++ .../PracticeMay2023/07_product_of_array.py | 10 ++ Puzzles/PracticeMay2023/08_move_zeroes.py | 9 ++ Puzzles/PracticeMay2023/README.md | 128 ++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 Puzzles/PracticeMay2023/07_product_of_array.py create mode 100644 Puzzles/PracticeMay2023/08_move_zeroes.py diff --git a/Puzzles/PracticeMay2023/05_reverse_vowels.py b/Puzzles/PracticeMay2023/05_reverse_vowels.py index e69de29..a9e7298 100644 --- a/Puzzles/PracticeMay2023/05_reverse_vowels.py +++ b/Puzzles/PracticeMay2023/05_reverse_vowels.py @@ -0,0 +1,26 @@ +class Solution: + def reverseVowels(self, s: str) -> str: + """ + Given a string s, reverse only all the vowels in the string and return it. + """ + # list all possible vowels (in lower case) + vowels = ['a', 'e', 'i', 'o', 'u'] + # make input string lower case so it can be validated against the list + to_replace = [] + index = [] + # iterate over every letter in s to see if it's a vowel; + # if it is, store it in reverse order in a placeholder list + for i in range(len(s)): + if s[i].lower() in vowels: + to_replace.append(s[i]) + index.append(i) + else: + pass + # reverse the list of vowels + to_replace.reverse() + # iterate over the list of vowels and replace the vowels in s + # with the reversed vowels + for i in range(len(to_replace)): + s = s[:index[i]] + to_replace[i] + s[index[i]+1:] + return s + \ No newline at end of file diff --git a/Puzzles/PracticeMay2023/07_product_of_array.py b/Puzzles/PracticeMay2023/07_product_of_array.py new file mode 100644 index 0000000..6a68ada --- /dev/null +++ b/Puzzles/PracticeMay2023/07_product_of_array.py @@ -0,0 +1,10 @@ +from functools import reduce + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + target_list = [] + list_size = len(nums) + if list_size: + for index in range(list_size): + target_list.append(reduce((lambda x, y: x * y), nums[:index] + nums[index + 1:])) + return target_list diff --git a/Puzzles/PracticeMay2023/08_move_zeroes.py b/Puzzles/PracticeMay2023/08_move_zeroes.py new file mode 100644 index 0000000..39ac67f --- /dev/null +++ b/Puzzles/PracticeMay2023/08_move_zeroes.py @@ -0,0 +1,9 @@ +class Solution: + def moveZeroes(self, nums: List[int]) -> None: + """ + Do not return anything, modify nums in-place instead. + """ + for x in nums: + if x == 0: + nums.remove(x) + nums.append(x) diff --git a/Puzzles/PracticeMay2023/README.md b/Puzzles/PracticeMay2023/README.md index d0c30a0..0460b19 100644 --- a/Puzzles/PracticeMay2023/README.md +++ b/Puzzles/PracticeMay2023/README.md @@ -207,3 +207,131 @@ Output: "leotcede" ### Acceptance Rate `50.3%` + + +* * * + + +## 6. Reverse Words in a String + +### Status: ✅ + +Given an input string `s`, reverse the order of the words. + +A word is defined as a sequence of non-space characters. The words in `s` will be separated by at least one space. + +Return a string of the words in reverse order concatenated by a single space. + +Note that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. + +### Example 1: +``` +Input: s = "the sky is blue" +Output: "blue is sky the" +``` + +### Example 2: +``` +Input: s = " hello world " +Output: "world hello" +``` +**Explanation:** Your reversed string should not contain leading or trailing spaces. + +### Example 3: +``` +Input: s = "a good example" +Output: "example good a" +``` +**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string. + +### Constraints: + +* `1 <= s.length <= 104` +* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`. +* There is at least one word in `s`. + +**Follow-up:** If the string data type is mutable in your language, can you solve it in-place with `O(1)` extra space? + +### Acceptance Rate +`33.3%` + + +* * * + + +## 7. Product of Array Except Self + +### Status: ❌ + +Given an input string `s`, reverse the order of the words. + +A word is defined as a sequence of non-space characters. The words in `s` will be separated by at least one space. + +Return a string of the words in reverse order concatenated by a single space. + +Note that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. + +### Example 1: +``` +Input: s = "the sky is blue" +Output: "blue is sky the" +``` + +### Example 2: +``` +Input: s = " hello world " +Output: "world hello" +``` +**Explanation:** Your reversed string should not contain leading or trailing spaces. + +### Example 3: +``` +Input: s = "a good example" +Output: "example good a" +``` +**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string. + +### Constraints: + +* `1 <= s.length <= 104` +* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`. +* There is at least one word in `s`. + +**Follow-up:** If the string data type is mutable in your language, can you solve it in-place with `O(1)` extra space? + +### Acceptance Rate +`33.3%` + + +* * * + + +## 8. Move Zeroes + +### Status: ✅ + +Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements. + +Note that you must do this in-place without making a copy of the array. + +### Example 1: +``` +Input: nums = [0,1,0,3,12] +Output: [1,3,12,0,0] +``` + +### Example 2: +``` +Input: nums = [0] +Output: [0] +``` + +### Constraints: + +* `1 <= nums.length <= 104` +* `-231 <= nums[i] <= 231 - 1` + +**Follow-up:** Could you minimize the total number of operations done? + +### Acceptance Rate +`61.4%` From a91020f5da94de74878b1bfcd3f0a0b2d50216f5 Mon Sep 17 00:00:00 2001 From: Bianca Henderson Date: Wed, 26 Jul 2023 20:05:51 -0400 Subject: [PATCH 4/5] =?UTF-8?q?Add=20more=20puzzles=20=F0=9F=A7=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Puzzles/PracticeMay2023/09_is_subsequence.py | 48 +++++++++++++++++++ .../PracticeMay2023/10_max_avg_subarray.py | 45 +++++++++++++++++ .../PracticeMay2023/11_find_max_altitude.py | 7 +++ 3 files changed, 100 insertions(+) create mode 100644 Puzzles/PracticeMay2023/09_is_subsequence.py create mode 100644 Puzzles/PracticeMay2023/10_max_avg_subarray.py create mode 100644 Puzzles/PracticeMay2023/11_find_max_altitude.py diff --git a/Puzzles/PracticeMay2023/09_is_subsequence.py b/Puzzles/PracticeMay2023/09_is_subsequence.py new file mode 100644 index 0000000..48b87c2 --- /dev/null +++ b/Puzzles/PracticeMay2023/09_is_subsequence.py @@ -0,0 +1,48 @@ +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + m = len(s) + n = len(t) + if m == 0: + return True + elif n == 0: + return False + elif m == n: + if s == t: + return True + else: + return False + + index = 0 + for l in s: + new_index = t.find(l) + if new_index > 0 and new_index == index: + return False + elif new_index >= index: + index = new_index + continue + elif index > new_index: + new_index = t[slice(index, len(t), 1)].find(l) + if new_index < 0: + return False + else: + continue + else: + return False + if index: + return True + else: + return False + + +class BetterSolution: + def isSubsequence(self, s: str, t: str) -> bool: + if not s: + return True + + s_index = 0 + for l in t: + if l == s[s_index]: + s_index += 1 + if s_index == len(s): + return True + return False \ No newline at end of file diff --git a/Puzzles/PracticeMay2023/10_max_avg_subarray.py b/Puzzles/PracticeMay2023/10_max_avg_subarray.py new file mode 100644 index 0000000..a243062 --- /dev/null +++ b/Puzzles/PracticeMay2023/10_max_avg_subarray.py @@ -0,0 +1,45 @@ +class Solution: + def findMaxAverage(self, nums: List[int], k: int) -> float: + if k > len(nums): + return 0 + if k == 0: + return 0 + if not nums: + return 0 + get_max = [] + index1 = 0 + index2 = k + while index2 <= len(nums): + get_max.append(sum(nums[index1:index2])) + index1 += 1 + index2 += 1 + if get_max: + biggest = max(get_max) + print(biggest) + result = (biggest / k) + print(result) + return result + else: + return 0 + +class BetterSolution: + def findMaxAverage(self, nums: List[int], k: int) -> float: + if k > len(nums): + return 0 + if k == 0: + return 0 + if not nums: + return 0 + index1 = 0 + index2 = k + biggest = sum(nums[index1:index2]) + while index2 <= len(nums): + if sum(nums[index1:index2]) > biggest: + biggest = sum(nums[index1:index2]) + index1 += 1 + index2 += 1 + if biggest: + result = (biggest / k) + return result + else: + return 0 \ No newline at end of file diff --git a/Puzzles/PracticeMay2023/11_find_max_altitude.py b/Puzzles/PracticeMay2023/11_find_max_altitude.py new file mode 100644 index 0000000..6c1aef9 --- /dev/null +++ b/Puzzles/PracticeMay2023/11_find_max_altitude.py @@ -0,0 +1,7 @@ +class Solution: + def largestAltitude(self, gain: List[int]) -> int: + starting = [0] + for a in gain: + starting.append(starting[-1] + a) + return max(starting) + From 7e98b641b005a8988ad8e1271124ff2bb4e10768 Mon Sep 17 00:00:00 2001 From: Bianca Henderson Date: Wed, 26 Jul 2023 20:08:39 -0400 Subject: [PATCH 5/5] Fix whitespace --- Puzzles/PracticeMay2023/09_is_subsequence.py | 2 +- Puzzles/PracticeMay2023/10_max_avg_subarray.py | 2 +- Puzzles/PracticeMay2023/11_find_max_altitude.py | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Puzzles/PracticeMay2023/09_is_subsequence.py b/Puzzles/PracticeMay2023/09_is_subsequence.py index 48b87c2..51dea62 100644 --- a/Puzzles/PracticeMay2023/09_is_subsequence.py +++ b/Puzzles/PracticeMay2023/09_is_subsequence.py @@ -45,4 +45,4 @@ def isSubsequence(self, s: str, t: str) -> bool: s_index += 1 if s_index == len(s): return True - return False \ No newline at end of file + return False diff --git a/Puzzles/PracticeMay2023/10_max_avg_subarray.py b/Puzzles/PracticeMay2023/10_max_avg_subarray.py index a243062..507381f 100644 --- a/Puzzles/PracticeMay2023/10_max_avg_subarray.py +++ b/Puzzles/PracticeMay2023/10_max_avg_subarray.py @@ -42,4 +42,4 @@ def findMaxAverage(self, nums: List[int], k: int) -> float: result = (biggest / k) return result else: - return 0 \ No newline at end of file + return 0 diff --git a/Puzzles/PracticeMay2023/11_find_max_altitude.py b/Puzzles/PracticeMay2023/11_find_max_altitude.py index 6c1aef9..1c8f9d9 100644 --- a/Puzzles/PracticeMay2023/11_find_max_altitude.py +++ b/Puzzles/PracticeMay2023/11_find_max_altitude.py @@ -4,4 +4,3 @@ def largestAltitude(self, gain: List[int]) -> int: for a in gain: starting.append(starting[-1] + a) return max(starting) -