diff --git a/README.md b/README.md index 79fb745..9c20889 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # LeetCode Python Solutions -![LeetCode Logo](https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png) + + +

+ This repository contains Python solutions for various LeetCode problems. Each problem is organized in its own directory under the `Solution/` folder, with a `readme.md` file providing the problem description, examples, constraints, and solutions in multiple programming languages. diff --git a/Solution/1071. Greatest Common Divisor of Strings/1071. Greatest Common Divisor of Strings.py b/Solution/1071. Greatest Common Divisor of Strings/1071. Greatest Common Divisor of Strings.py new file mode 100644 index 0000000..5e38c88 --- /dev/null +++ b/Solution/1071. Greatest Common Divisor of Strings/1071. Greatest Common Divisor of Strings.py @@ -0,0 +1,16 @@ +class Solution(object): + def gcdOfStrings(self, str1, str2): + """ + :type str1: str + :type str2: str + :rtype: str + """ + if str1 + str2 != str2 + str1: + return "" + + def gcd(a, b): + while b: + a, b = b, a % b + return a + + return str1[:gcd(len(str1), len(str2))] \ No newline at end of file diff --git a/Solution/1071. Greatest Common Divisor of Strings/readme.md b/Solution/1071. Greatest Common Divisor of Strings/readme.md new file mode 100644 index 0000000..d38af79 --- /dev/null +++ b/Solution/1071. Greatest Common Divisor of Strings/readme.md @@ -0,0 +1,118 @@ + + + + +# [1071. Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings) + +--- +- **comments**: true +- **difficulty**: Easy +- **rating**: 1397 +- **source**: Weekly Contest 139 Q1 +- **tags**: + - Math + - String +--- + +## Description + + + +

For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).

+ +

Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.

+ +

 

+

Example 1:

+ +
+Input: str1 = "ABCABC", str2 = "ABC"
+Output: "ABC"
+
+ +

Example 2:

+ +
+Input: str1 = "ABABAB", str2 = "ABAB"
+Output: "AB"
+
+ +

Example 3:

+ +
+Input: str1 = "LEET", str2 = "CODE"
+Output: ""
+
+ +

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python +class Solution: + def gcdOfStrings(self, str1: str, str2: str) -> str: + def check(a, b): + c = "" + while len(c) < len(b): + c += a + return c == b + + for i in range(min(len(str1), len(str2)), 0, -1): + t = str1[:i] + if check(t, str1) and check(t, str2): + return t + return '' +``` + +#### Java + +```java +class Solution { + public String gcdOfStrings(String str1, String str2) { + if (!(str1 + str2).equals(str2 + str1)) { + return ""; + } + int len = gcd(str1.length(), str2.length()); + return str1.substring(0, len); + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + string gcdOfStrings(string str1, string str2) { + if (str1 + str2 != str2 + str1) return ""; + int n = __gcd(str1.size(), str2.size()); + return str1.substr(0, n); + } +}; +``` + + + + + + \ No newline at end of file diff --git a/Solution/1431. Kids With the Greatest Number of Candies/1431. Kids With the Greatest Number of Candies.py b/Solution/1431. Kids With the Greatest Number of Candies/1431. Kids With the Greatest Number of Candies.py new file mode 100644 index 0000000..1d0a079 --- /dev/null +++ b/Solution/1431. Kids With the Greatest Number of Candies/1431. Kids With the Greatest Number of Candies.py @@ -0,0 +1,16 @@ +from typing import List + +class Solution: + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + # Find the maximum number of candies that any child currently has + max_candies = max(candies) + + # Create a list of boolean values, where each value indicates whether a child + # can have the greatest number of candies by adding the extraCandies to their current amount + can_have_most_candies = [ + (child_candies + extraCandies) >= max_candies for child_candies in candies + ] + + # Return the list of boolean values + return can_have_most_candies + \ No newline at end of file diff --git a/Solution/1431. Kids With the Greatest Number of Candies/readme.md b/Solution/1431. Kids With the Greatest Number of Candies/readme.md new file mode 100644 index 0000000..70fb1d3 --- /dev/null +++ b/Solution/1431. Kids With the Greatest Number of Candies/readme.md @@ -0,0 +1,123 @@ + + + + +# [1431. Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies) + +--- +- **comments**: true +- **difficulty**: Easy +- **rating**: 1176 +- **source**: Biweekly Contest 25 Q1 +- **tags**: + - Array +--- + +## Description + + + +

There are n kids with candies. You are given an integer array 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.

+ +

 

+

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:

+ + + + + +## Solutions + + + +### Solution 1 + + + +#### Python3 + +```python +class Solution: + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + mx = max(candies) + return [candy + extraCandies >= mx for candy in candies] +``` + +#### Java + +```java +class Solution { + public List kidsWithCandies(int[] candies, int extraCandies) { + int mx = 0; + for (int candy : candies) { + mx = Math.max(mx, candy); + } + List res = new ArrayList<>(); + for (int candy : candies) { + res.add(candy + extraCandies >= mx); + } + return res; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector kidsWithCandies(vector& candies, int extraCandies) { + int mx = *max_element(candies.begin(), candies.end()); + vector res; + for (int candy : candies) { + res.push_back(candy + extraCandies >= mx); + } + return res; + } +}; +``` + + + + + + \ No newline at end of file diff --git a/Solution/162. Find Peak Element/162. Find Peak Element.py b/Solution/162. Find Peak Element/162. Find Peak Element.py new file mode 100644 index 0000000..cae9134 --- /dev/null +++ b/Solution/162. Find Peak Element/162. Find Peak Element.py @@ -0,0 +1,10 @@ +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + left, right = 0, len(nums) - 1 + while left < right: + mid = (left + right) >> 1 + if nums[mid] > nums[mid + 1]: + right = mid + else: + left = mid + 1 + return left \ No newline at end of file diff --git a/Solution/162. Find Peak Element/readme.md b/Solution/162. Find Peak Element/readme.md new file mode 100644 index 0000000..130ebe0 --- /dev/null +++ b/Solution/162. Find Peak Element/readme.md @@ -0,0 +1,162 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/0100-0199/0162.Find%20Peak%20Element/README_EN.md +tags: + - Array + - Binary Search +--- + + + +# [162. Find Peak Element](https://leetcode.com/problems/find-peak-element) + +[中文文档](/solution/0100-0199/0162.Find%20Peak%20Element/README.md) + +## Description + + + +

A peak element is an element that is strictly greater than its neighbors.

+ +

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

+ +

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

+ +

You must write an algorithm that runs in O(log n) time.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,2,3,1]
+Output: 2
+Explanation: 3 is a peak element and your function should return the index number 2.
+ +

Example 2:

+ +
+Input: nums = [1,2,1,3,5,6,4]
+Output: 5
+Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • nums[i] != nums[i + 1] for all valid i.
  • +
+ + + +## Solutions + + + +### Solution 1: Binary Search + +We define the left boundary of binary search as $left=0$ and the right boundary as $right=n-1$, where $n$ is the length of the array. In each step of binary search, we find the middle element $mid$ of the current interval, and compare the values of $mid$ and its right neighbor $mid+1$: + +- If the value of $mid$ is greater than the value of $mid+1$, there exists a peak element on the left side, and we update the right boundary $right$ to $mid$. +- Otherwise, there exists a peak element on the right side, and we update the left boundary $left$ to $mid+1$. +- Finally, when the left boundary $left$ is equal to the right boundary $right$, we have found the peak element of the array. + +The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. Each step of binary search can reduce the search interval by half, so the time complexity is $O(\log n)$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def findPeakElement(self, nums: List[int]) -> int: + left, right = 0, len(nums) - 1 + while left < right: + mid = (left + right) >> 1 + if nums[mid] > nums[mid + 1]: + right = mid + else: + left = mid + 1 + return left +``` + +#### Java + +```java +class Solution { + public int findPeakElement(int[] nums) { + int left = 0, right = nums.length - 1; + while (left < right) { + int mid = (left + right) >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int findPeakElement(vector& nums) { + int left = 0, right = nums.size() - 1; + while (left < right) { + int mid = left + right >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } +}; +``` + +#### Go + +```go +func findPeakElement(nums []int) int { + left, right := 0, len(nums)-1 + for left < right { + mid := (left + right) >> 1 + if nums[mid] > nums[mid+1] { + right = mid + } else { + left = mid + 1 + } + } + return left +} +``` + +#### TypeScript + +```ts +function findPeakElement(nums: number[]): number { + let [left, right] = [0, nums.length - 1]; + while (left < right) { + const mid = (left + right) >> 1; + if (nums[mid] > nums[mid + 1]) { + right = mid; + } else { + left = mid + 1; + } + } + return left; +} +``` + + + + + + \ No newline at end of file diff --git a/Solution/1768. Merge Strings Alternately/1768. Merge Strings Alternately.py b/Solution/1768. Merge Strings Alternately/1768. Merge Strings Alternately.py new file mode 100644 index 0000000..ec9f181 --- /dev/null +++ b/Solution/1768. Merge Strings Alternately/1768. Merge Strings Alternately.py @@ -0,0 +1,3 @@ +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue='')) \ No newline at end of file diff --git a/Solution/1768. Merge Strings Alternately/readme.md b/Solution/1768. Merge Strings Alternately/readme.md new file mode 100644 index 0000000..5f79b5c --- /dev/null +++ b/Solution/1768. Merge Strings Alternately/readme.md @@ -0,0 +1,129 @@ + + +# [1768. Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately) + + +- **comments**: true +- **difficulty**: Easy +- **rating**: 1166 +- **source**: Weekly Contest 229 Q1 +- **tags**: + - Two Pointers + - String +--- +## Description + + + +

You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.

+ +

Return the merged string.

+ +

 

+

Example 1:

+ +
+Input: word1 = "abc", word2 = "pqr"
+Output: "apbqcr"
+Explanation: The merged string will be merged as so:
+word1:  a   b   c
+word2:    p   q   r
+merged: a p b q c r
+
+ +

Example 2:

+ +
+Input: word1 = "ab", word2 = "pqrs"
+Output: "apbqrs"
+Explanation: Notice that as word2 is longer, "rs" is appended to the end.
+word1:  a   b 
+word2:    p   q   r   s
+merged: a p b q   r   s
+
+ +

Example 3:

+ +
+Input: word1 = "abcd", word2 = "pq"
+Output: "apbqcd"
+Explanation: Notice that as word1 is longer, "cd" is appended to the end.
+word1:  a   b   c   d
+word2:    p   q 
+merged: a p b q c   d
+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= word1.length, word2.length <= 100
  • +
  • word1 and word2 consist of lowercase English letters.
  • +
+ + + +## Solutions + + + +### Solution 1: Direct Simulation + +We traverse the two strings `word1` and `word2`, take out the characters one by one, and append them to the result string. The Python code can be simplified into one line. + +The time complexity is $O(m + n)$, where $m$ and $n$ are the lengths of the two strings respectively. Ignoring the space consumption of the answer, the space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def mergeAlternately(self, word1: str, word2: str) -> str: + return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue='')) +``` + +#### Java + +```java +class Solution { + public String mergeAlternately(String word1, String word2) { + int m = word1.length(), n = word2.length(); + StringBuilder ans = new StringBuilder(); + for (int i = 0; i < m || i < n; ++i) { + if (i < m) { + ans.append(word1.charAt(i)); + } + if (i < n) { + ans.append(word2.charAt(i)); + } + } + return ans.toString(); + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + string mergeAlternately(string word1, string word2) { + int m = word1.size(), n = word2.size(); + string ans; + for (int i = 0; i < m || i < n; ++i) { + if (i < m) ans += word1[i]; + if (i < n) ans += word2[i]; + } + return ans; + } +}; +``` + + + + + + + + \ No newline at end of file diff --git a/Solution/2300. Successful Pairs of Spells and Potions/2300. Successful Pairs of Spells and Potions.py b/Solution/2300. Successful Pairs of Spells and Potions/2300. Successful Pairs of Spells and Potions.py new file mode 100644 index 0000000..ded2efd --- /dev/null +++ b/Solution/2300. Successful Pairs of Spells and Potions/2300. Successful Pairs of Spells and Potions.py @@ -0,0 +1,7 @@ +class Solution: + def successfulPairs( + self, spells: List[int], potions: List[int], success: int + ) -> List[int]: + potions.sort() + m = len(potions) + return [m - bisect_left(potions, success / v) for v in spells] \ No newline at end of file diff --git a/Solution/2300. Successful Pairs of Spells and Potions/readme.md b/Solution/2300. Successful Pairs of Spells and Potions/readme.md new file mode 100644 index 0000000..aaa897b --- /dev/null +++ b/Solution/2300. Successful Pairs of Spells and Potions/readme.md @@ -0,0 +1,177 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/2300-2399/2300.Successful%20Pairs%20of%20Spells%20and%20Potions/README_EN.md +rating: 1476 +source: Biweekly Contest 80 Q2 +tags: + - Array + - Two Pointers + - Binary Search + - Sorting +--- + + + +# [2300. Successful Pairs of Spells and Potions](https://leetcode.com/problems/successful-pairs-of-spells-and-potions) + +[中文文档](/solution/2300-2399/2300.Successful%20Pairs%20of%20Spells%20and%20Potions/README.md) + +## Description + + + +

You are given two positive integer arrays spells and potions, of length n and m respectively, where spells[i] represents the strength of the ith spell and potions[j] represents the strength of the jth potion.

+ +

You are also given an integer success. A spell and potion pair is considered successful if the product of their strengths is at least success.

+ +

Return an integer array pairs of length n where pairs[i] is the number of potions that will form a successful pair with the ith spell.

+ +

 

+

Example 1:

+ +
+Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
+Output: [4,0,3]
+Explanation:
+- 0th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
+- 1st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
+- 2nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
+Thus, [4,0,3] is returned.
+
+ +

Example 2:

+ +
+Input: spells = [3,1,2], potions = [8,5,8], success = 16
+Output: [2,0,2]
+Explanation:
+- 0th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
+- 1st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful. 
+- 2nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful. 
+Thus, [2,0,2] is returned.
+
+ +

 

+

Constraints:

+ +
    +
  • n == spells.length
  • +
  • m == potions.length
  • +
  • 1 <= n, m <= 105
  • +
  • 1 <= spells[i], potions[i] <= 105
  • +
  • 1 <= success <= 1010
  • +
+ + + +## Solutions + + + +### Solution 1: Sorting + Binary Search + +We can sort the potion array, then traverse the spell array. For each spell $v$, we use binary search to find the first potion that is greater than or equal to $\frac{success}{v}$. We mark its index as $i$. The length of the potion array minus $i$ is the number of potions that can successfully combine with this spell. + +The time complexity is $O((m + n) \times \log m)$, and the space complexity is $O(\log n)$. Here, $m$ and $n$ are the lengths of the potion array and the spell array, respectively. + + + +#### Python3 + +```python +class Solution: + def successfulPairs( + self, spells: List[int], potions: List[int], success: int + ) -> List[int]: + potions.sort() + m = len(potions) + return [m - bisect_left(potions, success / v) for v in spells] +``` + +#### Java + +```java +class Solution { + public int[] successfulPairs(int[] spells, int[] potions, long success) { + Arrays.sort(potions); + int n = spells.length, m = potions.length; + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + int left = 0, right = m; + while (left < right) { + int mid = (left + right) >> 1; + if ((long) spells[i] * potions[mid] >= success) { + right = mid; + } else { + left = mid + 1; + } + } + ans[i] = m - left; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector successfulPairs(vector& spells, vector& potions, long long success) { + sort(potions.begin(), potions.end()); + vector ans; + int m = potions.size(); + for (int& v : spells) { + int i = lower_bound(potions.begin(), potions.end(), success * 1.0 / v) - potions.begin(); + ans.push_back(m - i); + } + return ans; + } +}; +``` + +#### Go + +```go +func successfulPairs(spells []int, potions []int, success int64) (ans []int) { + sort.Ints(potions) + m := len(potions) + for _, v := range spells { + i := sort.Search(m, func(i int) bool { return int64(potions[i]*v) >= success }) + ans = append(ans, m-i) + } + return ans +} +``` + +#### TypeScript + +```ts +function successfulPairs(spells: number[], potions: number[], success: number): number[] { + potions.sort((a, b) => a - b); + const m = potions.length; + const ans: number[] = []; + for (const v of spells) { + let left = 0; + let right = m; + while (left < right) { + const mid = (left + right) >> 1; + if (v * potions[mid] >= success) { + right = mid; + } else { + left = mid + 1; + } + } + ans.push(m - left); + } + return ans; +} +``` + + + + + + \ No newline at end of file diff --git a/Solution/605. Can Place Flowers/605. Can Place Flowers.py b/Solution/605. Can Place Flowers/605. Can Place Flowers.py new file mode 100644 index 0000000..9e84d99 --- /dev/null +++ b/Solution/605. Can Place Flowers/605. Can Place Flowers.py @@ -0,0 +1,19 @@ +class Solution: + def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: + # Add empty plots at the start and at the end of the flowerbed to simplify edge case handling + flowerbed = [0] + flowerbed + [0] + + # Iterate over each plot in the flowerbed starting from the first actual plot to the last + for i in range(1, len(flowerbed) - 1): + # Check if the current plot and its adjacent plots are empty + if sum(flowerbed[i - 1: i + 2]) == 0: + # Plant a flower in the current plot + flowerbed[i] = 1 + # Decrement the count of flowers we need to plant + n -= 1 + # If we have planted all required flowers, we can end early + if n == 0: + return True + + # After checking all plots, decide if we were able to plant all flowers + return n <= 0 \ No newline at end of file diff --git a/Solution/605. Can Place Flowers/readme.md b/Solution/605. Can Place Flowers/readme.md new file mode 100644 index 0000000..0d0d734 --- /dev/null +++ b/Solution/605. Can Place Flowers/readme.md @@ -0,0 +1,111 @@ + + + + +# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers) + +- **comments**: true +- **difficulty**: Easy +- **tags**: + - Greedy + - Array +--- + +## Description + + + +

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
  • +
+ + + +## Solutions + + + +### Solution 1: Greedy + +We directly traverse the array $flowerbed$. For each position $i$, if $flowerbed[i]=0$ and its adjacent positions on the left and right are also $0$, then we can plant a flower at this position. Otherwise, we cannot. Finally, we count the number of flowers that can be planted. If it is not less than $n$, we return $true$, otherwise we return $false$. + +The time complexity is $O(n)$, where $n$ is the length of the array $flowerbed$. We only need to traverse the array once. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool: + flowerbed = [0] + flowerbed + [0] + for i in range(1, len(flowerbed) - 1): + if sum(flowerbed[i - 1 : i + 2]) == 0: + flowerbed[i] = 1 + n -= 1 + return n <= 0 +``` + +#### Java + +```java +class Solution { + public boolean canPlaceFlowers(int[] flowerbed, int n) { + int m = flowerbed.length; + for (int i = 0; i < m; ++i) { + int l = i == 0 ? 0 : flowerbed[i - 1]; + int r = i == m - 1 ? 0 : flowerbed[i + 1]; + if (l + flowerbed[i] + r == 0) { + flowerbed[i] = 1; + --n; + } + } + return n <= 0; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + bool canPlaceFlowers(vector& flowerbed, int n) { + int m = flowerbed.size(); + for (int i = 0; i < m; ++i) { + int l = i == 0 ? 0 : flowerbed[i - 1]; + int r = i == m - 1 ? 0 : flowerbed[i + 1]; + if (l + flowerbed[i] + r == 0) { + flowerbed[i] = 1; + --n; + } + } + return n <= 0; + } +}; +``` + + + + + + + \ No newline at end of file diff --git a/assets/LeetCode_75.gif b/assets/LeetCode_75.gif new file mode 100644 index 0000000..086d269 Binary files /dev/null and b/assets/LeetCode_75.gif differ diff --git a/assets/lc75.gif b/assets/lc75.gif new file mode 100644 index 0000000..33044a3 Binary files /dev/null and b/assets/lc75.gif differ