Skip to content

Commit 77feea7

Browse files
authored
Merge pull request #6 from iamAntimPal/Leetcode-75
adding award
2 parents ec9c539 + 9806792 commit 77feea7

File tree

15 files changed

+895
-1
lines changed

15 files changed

+895
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# LeetCode Python Solutions
22

3-
![LeetCode Logo](https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png)
3+
<!-- <p align="center"><img src="https://upload.wikimedia.org/wikipedia/commons/1/19/LeetCode_logo_black.png"></p> -->
4+
5+
<p align="center"><img src="./assets/LeetCode_75.gif"></p>
6+
47

58
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.
69

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def gcdOfStrings(self, str1, str2):
3+
"""
4+
:type str1: str
5+
:type str2: str
6+
:rtype: str
7+
"""
8+
if str1 + str2 != str2 + str1:
9+
return ""
10+
11+
def gcd(a, b):
12+
while b:
13+
a, b = b, a % b
14+
return a
15+
16+
return str1[:gcd(len(str1), len(str2))]
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
3+
<!-- problem:start -->
4+
5+
# [1071. Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings)
6+
7+
---
8+
- **comments**: true
9+
- **difficulty**: Easy
10+
- **rating**: 1397
11+
- **source**: Weekly Contest 139 Q1
12+
- **tags**:
13+
- Math
14+
- String
15+
---
16+
17+
## Description
18+
19+
<!-- description:start -->
20+
21+
<p>For two strings <code>s</code> and <code>t</code>, we say &quot;<code>t</code> divides <code>s</code>&quot; if and only if <code>s = t + t + t + ... + t + t</code> (i.e., <code>t</code> is concatenated with itself one or more times).</p>
22+
23+
<p>Given two strings <code>str1</code> and <code>str2</code>, return <em>the largest string </em><code>x</code><em> such that </em><code>x</code><em> divides both </em><code>str1</code><em> and </em><code>str2</code>.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> str1 = &quot;ABCABC&quot;, str2 = &quot;ABC&quot;
30+
<strong>Output:</strong> &quot;ABC&quot;
31+
</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> str1 = &quot;ABABAB&quot;, str2 = &quot;ABAB&quot;
37+
<strong>Output:</strong> &quot;AB&quot;
38+
</pre>
39+
40+
<p><strong class="example">Example 3:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> str1 = &quot;LEET&quot;, str2 = &quot;CODE&quot;
44+
<strong>Output:</strong> &quot;&quot;
45+
</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= str1.length, str2.length &lt;= 1000</code></li>
52+
<li><code>str1</code> and <code>str2</code> consist of English uppercase letters.</li>
53+
</ul>
54+
55+
<!-- description:end -->
56+
57+
## Solutions
58+
59+
<!-- solution:start -->
60+
61+
### Solution 1
62+
63+
<!-- tabs:start -->
64+
65+
#### Python3
66+
67+
```python
68+
class Solution:
69+
def gcdOfStrings(self, str1: str, str2: str) -> str:
70+
def check(a, b):
71+
c = ""
72+
while len(c) < len(b):
73+
c += a
74+
return c == b
75+
76+
for i in range(min(len(str1), len(str2)), 0, -1):
77+
t = str1[:i]
78+
if check(t, str1) and check(t, str2):
79+
return t
80+
return ''
81+
```
82+
83+
#### Java
84+
85+
```java
86+
class Solution {
87+
public String gcdOfStrings(String str1, String str2) {
88+
if (!(str1 + str2).equals(str2 + str1)) {
89+
return "";
90+
}
91+
int len = gcd(str1.length(), str2.length());
92+
return str1.substring(0, len);
93+
}
94+
95+
private int gcd(int a, int b) {
96+
return b == 0 ? a : gcd(b, a % b);
97+
}
98+
}
99+
```
100+
101+
#### C++
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
string gcdOfStrings(string str1, string str2) {
107+
if (str1 + str2 != str2 + str1) return "";
108+
int n = __gcd(str1.size(), str2.size());
109+
return str1.substr(0, n);
110+
}
111+
};
112+
```
113+
114+
<!-- tabs:end -->
115+
116+
<!-- solution:end -->
117+
118+
<!-- problem:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
class Solution:
4+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
5+
# Find the maximum number of candies that any child currently has
6+
max_candies = max(candies)
7+
8+
# Create a list of boolean values, where each value indicates whether a child
9+
# can have the greatest number of candies by adding the extraCandies to their current amount
10+
can_have_most_candies = [
11+
(child_candies + extraCandies) >= max_candies for child_candies in candies
12+
]
13+
14+
# Return the list of boolean values
15+
return can_have_most_candies
16+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
2+
3+
<!-- problem:start -->
4+
5+
# [1431. Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies)
6+
7+
---
8+
- **comments**: true
9+
- **difficulty**: Easy
10+
- **rating**: 1176
11+
- **source**: Biweekly Contest 25 Q1
12+
- **tags**:
13+
- Array
14+
---
15+
16+
## Description
17+
18+
<!-- description:start -->
19+
20+
<p>There are <code>n</code> kids with candies. You are given an integer array <code>candies</code>, where each <code>candies[i]</code> represents the number of candies the <code>i<sup>th</sup></code> kid has, and an integer <code>extraCandies</code>, denoting the number of extra candies that you have.</p>
21+
22+
<p>Return <em>a boolean array </em><code>result</code><em> of length </em><code>n</code><em>, where </em><code>result[i]</code><em> is </em><code>true</code><em> if, after giving the </em><code>i<sup>th</sup></code><em> kid all the </em><code>extraCandies</code><em>, they will have the <strong>greatest</strong> number of candies among all the kids</em><em>, or </em><code>false</code><em> otherwise</em>.</p>
23+
24+
<p>Note that <strong>multiple</strong> kids can have the <strong>greatest</strong> number of candies.</p>
25+
26+
<p>&nbsp;</p>
27+
<p><strong class="example">Example 1:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> candies = [2,3,5,1,3], extraCandies = 3
31+
<strong>Output:</strong> [true,true,true,false,true]
32+
<strong>Explanation:</strong> If you give all extraCandies to:
33+
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
34+
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
35+
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
36+
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
37+
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
38+
</pre>
39+
40+
<p><strong class="example">Example 2:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> candies = [4,2,1,1,2], extraCandies = 1
44+
<strong>Output:</strong> [true,false,false,false,false]
45+
<strong>Explanation:</strong> There is only 1 extra candy.
46+
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
47+
</pre>
48+
49+
<p><strong class="example">Example 3:</strong></p>
50+
51+
<pre>
52+
<strong>Input:</strong> candies = [12,1,12], extraCandies = 10
53+
<strong>Output:</strong> [true,false,true]
54+
</pre>
55+
56+
<p>&nbsp;</p>
57+
<p><strong>Constraints:</strong></p>
58+
59+
<ul>
60+
<li><code>n == candies.length</code></li>
61+
<li><code>2 &lt;= n &lt;= 100</code></li>
62+
<li><code>1 &lt;= candies[i] &lt;= 100</code></li>
63+
<li><code>1 &lt;= extraCandies &lt;= 50</code></li>
64+
</ul>
65+
66+
<!-- description:end -->
67+
68+
## Solutions
69+
70+
<!-- solution:start -->
71+
72+
### Solution 1
73+
74+
<!-- tabs:start -->
75+
76+
#### Python3
77+
78+
```python
79+
class Solution:
80+
def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
81+
mx = max(candies)
82+
return [candy + extraCandies >= mx for candy in candies]
83+
```
84+
85+
#### Java
86+
87+
```java
88+
class Solution {
89+
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
90+
int mx = 0;
91+
for (int candy : candies) {
92+
mx = Math.max(mx, candy);
93+
}
94+
List<Boolean> res = new ArrayList<>();
95+
for (int candy : candies) {
96+
res.add(candy + extraCandies >= mx);
97+
}
98+
return res;
99+
}
100+
}
101+
```
102+
103+
#### C++
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
109+
int mx = *max_element(candies.begin(), candies.end());
110+
vector<bool> res;
111+
for (int candy : candies) {
112+
res.push_back(candy + extraCandies >= mx);
113+
}
114+
return res;
115+
}
116+
};
117+
```
118+
119+
<!-- tabs:end -->
120+
121+
<!-- solution:end -->
122+
123+
<!-- problem:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findPeakElement(self, nums: List[int]) -> int:
3+
left, right = 0, len(nums) - 1
4+
while left < right:
5+
mid = (left + right) >> 1
6+
if nums[mid] > nums[mid + 1]:
7+
right = mid
8+
else:
9+
left = mid + 1
10+
return left

0 commit comments

Comments
 (0)