Compare commits

...

5 Commits

Author SHA1 Message Date
Deven cf8b99d511 Time: 43 ms (93.63%), Space: 18.2 MB (59.46%) - LeetHub 2025-11-30 22:34:41 -05:00
Deven c90ee7f934 Time: 43 ms (93.63%), Space: 18.2 MB (59.46%) - LeetHub 2025-11-30 22:34:13 -05:00
Deven 10ff3a0da5 Create README - LeetHub 2025-11-30 22:34:12 -05:00
Deven 29c783e08b Time: 810 ms (40.98%), Space: 18.7 MB (34.3%) - LeetHub 2025-11-30 20:48:35 -05:00
Deven 79b324dd16 Create README - LeetHub 2025-11-30 20:48:34 -05:00
4 changed files with 133 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
"""
Complexities:
Time: O(mn)
Space: O(m)
where n = len(coins), m = amount
"""
dp = [-1] * (amount + 1)
dp[0] = 0
for i in range(amount + 1):
for coinValue in coins:
if dp[i] == -1 or i + coinValue > amount:
continue
elif dp[i + coinValue] == -1:
dp[i + coinValue] = dp[i] + 1
else:
dp[i + coinValue] = min(dp[i + coinValue], dp[i] + 1)
print(dp)
return dp[amount]
+37
View File
@@ -0,0 +1,37 @@
<h2><a href="https://leetcode.com/problems/coin-change">322. Coin Change</a></h2><h3>Medium</h3><hr><p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>
<p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p>
<p>You may assume that you have an infinite number of each kind of coin.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> coins = [1,2,5], amount = 11
<strong>Output:</strong> 3
<strong>Explanation:</strong> 11 = 5 + 5 + 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> coins = [2], amount = 3
<strong>Output:</strong> -1
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> coins = [1], amount = 0
<strong>Output:</strong> 0
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= coins.length &lt;= 12</code></li>
<li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li>
</ul>
@@ -0,0 +1,17 @@
class Solution:
def bestClosingTime(self, customers: str) -> int:
penalty = 0
minPenalty = 0
bestHour = 0
for hour, hasCustomers in enumerate(customers):
if hasCustomers == "Y":
penalty -= 1
else:
penalty += 1
if penalty < minPenalty:
minPenalty = penalty
bestHour = hour + 1
return bestHour
+55
View File
@@ -0,0 +1,55 @@
<h2><a href="https://leetcode.com/problems/minimum-penalty-for-a-shop">2483. Minimum Penalty for a Shop</a></h2><h3>Medium</h3><hr><p>You are given the customer visit log of a shop represented by a <strong>0-indexed</strong> string <code>customers</code> consisting only of characters <code>&#39;N&#39;</code> and <code>&#39;Y&#39;</code>:</p>
<ul>
<li>if the <code>i<sup>th</sup></code> character is <code>&#39;Y&#39;</code>, it means that customers come at the <code>i<sup>th</sup></code> hour</li>
<li>whereas <code>&#39;N&#39;</code> indicates that no customers come at the <code>i<sup>th</sup></code> hour.</li>
</ul>
<p>If the shop closes at the <code>j<sup>th</sup></code> hour (<code>0 &lt;= j &lt;= n</code>), the <strong>penalty</strong> is calculated as follows:</p>
<ul>
<li>For every hour when the shop is open and no customers come, the penalty increases by <code>1</code>.</li>
<li>For every hour when the shop is closed and customers come, the penalty increases by <code>1</code>.</li>
</ul>
<p>Return<em> the <strong>earliest</strong> hour at which the shop must be closed to incur a <strong>minimum</strong> penalty.</em></p>
<p><strong>Note</strong> that if a shop closes at the <code>j<sup>th</sup></code> hour, it means the shop is closed at the hour <code>j</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> customers = &quot;YYNY&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- Closing the shop at the 0<sup>th</sup> hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1<sup>st</sup> hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2<sup>nd</sup> hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3<sup>rd</sup> hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4<sup>th</sup> hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2<sup>nd</sup> or 4<sup>th</sup> hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> customers = &quot;NNNNN&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> It is best to close the shop at the 0<sup>th</sup> hour as no customers arrive.</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> customers = &quot;YYYY&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> It is best to close the shop at the 4<sup>th</sup> hour as customers arrive at each hour.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= customers.length &lt;= 10<sup>5</sup></code></li>
<li><code>customers</code> consists only of characters <code>&#39;Y&#39;</code> and <code>&#39;N&#39;</code>.</li>
</ul>