Skip to content

Commit

Permalink
Daily
Browse files Browse the repository at this point in the history
  • Loading branch information
Zanger67 committed Jul 6, 2024
1 parent 1b3788b commit c04cabd
Show file tree
Hide file tree
Showing 62 changed files with 1,165 additions and 594 deletions.
2 changes: 1 addition & 1 deletion .Readme Updater
180 changes: 91 additions & 89 deletions README.md

Large diffs are not rendered by default.

79 changes: 40 additions & 39 deletions markdowns/Daily_Questions.md

Large diffs are not rendered by default.

176 changes: 89 additions & 87 deletions markdowns/Questions_By_Code_Length.md

Large diffs are not rendered by default.

176 changes: 89 additions & 87 deletions markdowns/Questions_By_Recent.md

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions markdowns/Topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
------

- [Array](<by_topic/Array.md>) (176 completed)
- [String](<by_topic/String.md>) (86 completed)
- [Hash Table](<by_topic/Hash Table.md>) (85 completed)
- [String](<by_topic/String.md>) (87 completed)
- [Hash Table](<by_topic/Hash Table.md>) (86 completed)
- [Depth-First Search](<by_topic/Depth-First Search.md>) (68 completed)
- [Tree](<by_topic/Tree.md>) (66 completed)
- [Binary Tree](<by_topic/Binary Tree.md>) (61 completed)
- [Sorting](<by_topic/Sorting.md>) (55 completed)
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (47 completed)
- [Math](<by_topic/Math.md>) (46 completed)
- [Math](<by_topic/Math.md>) (47 completed)
- [Two Pointers](<by_topic/Two Pointers.md>) (43 completed)
- [Linked List](<by_topic/Linked List.md>) (38 completed)
- [Stack](<by_topic/Stack.md>) (35 completed)
- [Greedy](<by_topic/Greedy.md>) (31 completed)
- [Matrix](<by_topic/Matrix.md>) (30 completed)
- [Simulation](<by_topic/Simulation.md>) (25 completed)
- [Simulation](<by_topic/Simulation.md>) (26 completed)
- [Bit Manipulation](<by_topic/Bit Manipulation.md>) (24 completed)
- [Dynamic Programming](<by_topic/Dynamic Programming.md>) (20 completed)
- [Design](<by_topic/Design.md>) (20 completed)
Expand All @@ -30,8 +30,8 @@
- [Recursion](<by_topic/Recursion.md>) (14 completed)
- [Backtracking](<by_topic/Backtracking.md>) (13 completed)
- [Binary Search Tree](<by_topic/Binary Search Tree.md>) (13 completed)
- [Sliding Window](<by_topic/Sliding Window.md>) (11 completed)
- [Prefix Sum](<by_topic/Prefix Sum.md>) (10 completed)
- [Sliding Window](<by_topic/Sliding Window.md>) (10 completed)
- [Monotonic Stack](<by_topic/Monotonic Stack.md>) (9 completed)
- [Queue](<by_topic/Queue.md>) (8 completed)
- [Union Find](<by_topic/Union Find.md>) (8 completed)
Expand Down
114 changes: 114 additions & 0 deletions markdowns/_2582. Pass the Pillow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 2582. [Pass the Pillow](<https://leetcode.com/problems/pass-the-pillow>)

*[Back to top](<../README.md>)*

------

> *First completed : July 05, 2024*
>
> *Last updated : July 05, 2024*

------

> **Related Topics** : **[Math](<by_topic/Math.md>), [Simulation](<by_topic/Simulation.md>)**
>
> **Acceptance Rate** : **45.847 %**

------

*To see the question prompt, click the title.*

## Solutions

- [e2582 Daily.py](<../my-submissions/e2582 Daily.py>)
- [e2582.c](<../my-submissions/e2582.c>)
- [e2582.cpp](<../my-submissions/e2582.cpp>)
- [e2582.cs](<../my-submissions/e2582.cs>)
- [e2582.java](<../my-submissions/e2582.java>)
- [e2582.js](<../my-submissions/e2582.js>)
### Python
#### [e2582 Daily.py](<../my-submissions/e2582 Daily.py>)
```Python
class Solution:
def passThePillow(self, n: int, time: int) -> int:
time %= 2 * (n - 1) # Remove there and backs
if time < n :
return time + 1
time -= n - 1
return n - time
```

### C
#### [e2582.c](<../my-submissions/e2582.c>)
```C
int passThePillow(int n, int time) {
time %= 2 * (n - 1);
if (time < n)
return time + 1;
time -= n - 1;
return n - time;
}
```
### C++
#### [e2582.cpp](<../my-submissions/e2582.cpp>)
```C++
class Solution {
public:
int passThePillow(int n, int time) {
time %= 2 * (n - 1);
if (time < n)
return time + 1;
time -= n - 1;
return n - time;
}
};
```

### C#
#### [e2582.cs](<../my-submissions/e2582.cs>)
```C#
public class Solution {
public int PassThePillow(int n, int time) {
time %= 2 * (n - 1);
if (time < n)
return time + 1;
time -= n - 1;
return n - time;
}
}
```

### Java
#### [e2582.java](<../my-submissions/e2582.java>)
```Java
class Solution {
public int passThePillow(int n, int time) {
time %= 2 * (n - 1);
if (time < n)
return time + 1;
time -= n - 1;
return n - time;
}
}
```

### JavaScript
#### [e2582.js](<../my-submissions/e2582.js>)
```JavaScript
/**
* @param {number} n
* @param {number} time
* @return {number}
*/
var passThePillow = function(n, time) {
time %= 2 * (n - 1);
if (time < n)
return time + 1;
time -= n - 1;
return n - time;
};
```

222 changes: 222 additions & 0 deletions markdowns/_424. Longest Repeating Character Replacement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
# 424. [Longest Repeating Character Replacement](<https://leetcode.com/problems/longest-repeating-character-replacement>)

*[Back to top](<../README.md>)*

------

> *First completed : July 05, 2024*
>
> *Last updated : July 05, 2024*

------

> **Related Topics** : **[Hash Table](<by_topic/Hash Table.md>), [String](<by_topic/String.md>), [Sliding Window](<by_topic/Sliding Window.md>)**
>
> **Acceptance Rate** : **54.193 %**

------

*To see the question prompt, click the title.*

> #### Version 5 (Optimal)
> ```
> The final optimization that had to be made was not updating the max frequency
> downwards at any given moment. Since we're only looking at the context of our
> window, the maximal window will require the most repeated characters, since all
> others must be changed to match them. Therefore, if the max frequency goes down,
> we know for certain that that window won't be valid, so we should just
> shift over an index for both the left and right pointers.
>
> This brought the runtime down to around 75ms, in line with all the
> submitted optimal solutions and perfectly within the bell curve.
> ```
>
> #### Version 4
> ```
> Here, I changed out the way I'd calculate the total number of
> non-max frequency characters to a mathematical approach. I had overlooked
> it in my haste initially, but the number of characters that would have to be
> changed was simply the length - the frequency of the most frequent character.
>
> The effect was essentially unnoticable however based on how LeetCode
> runs their tests and the time remained similar to Version 3.
> ```
>
> #### Version 3
> ```
> Compared to Version 2, the improvment here was minimal. I adjusted the
> tracking variable for the longest window to start at length k, since we'd
> at minimum find a case where every letter is unique, thus giving us a result
> of k+1 length.
>
> This reduced the time down to around 180ms and the 11% region.
> ```
>
> #### Version 2
> ```
> In this version, I made the slight adjustment to only check windows
> that were larger than the previous window size. This was a step
> in the right direction, and was based on a similar idea as the optimal
> solution as it turns out.
>
> This small adjustment resulted in a drop to 320ms per run.
> ```
>
> #### Version 1
> ```
> This was my first attempt at the question which passed without
> any TLE issues first try in under 9 minutes. However, it was on
> the brink of a TLE. On average, the runtime was measured at the
> bottom 5%, taking 520ms approx. The idea was correct,
> but it did need some modifications.
> ```
------
## Solutions
- [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>)
- [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>)
- [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>)
- [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>)
- [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>)
### Python
#### [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>)
```Python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
cnt = Counter(s[0])
longest = 0
left, right = 0, 0
while right < len(s) :
maxxChar = max(cnt, key=lambda x: cnt[x])
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])
if sumOthers > k :
cnt[s[left]] -= 1
left += 1
else :
longest = max(longest, sumOthers + cnt[maxxChar])
right += 1
if right < len(s) :
cnt[s[right]] += 1
return longest
```
#### [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>)
```Python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
cnt = Counter(s[0])

longest = 0
left, right = 0, 0
while right < len(s) :
if right - left + 1 <= longest :
right += 1
if right < len(s) :
cnt[s[right]] += 1
continue

maxxChar = max(cnt, key=lambda x: cnt[x])
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])

if sumOthers > k :
cnt[s[left]] -= 1
left += 1

else :
longest = right - left + 1
right += 1
if right < len(s) :
cnt[s[right]] += 1

return longest
```

#### [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>)
```Python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
cnt = Counter(s[0])

longest = min(k, len(s))
left, right = 0, 0
while right < len(s) :
if right - left + 1 <= longest :
right += 1
if right < len(s) :
cnt[s[right]] += 1
continue

maxxChar = max(cnt, key=lambda x: cnt[x])
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])

if sumOthers > k :
cnt[s[left]] -= 1
left += 1

else :
longest = right - left + 1
right += 1
if right < len(s) :
cnt[s[right]] += 1

return longest
```

#### [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>)
```Python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
cnt = Counter(s[0])

longest = min(k, len(s))
left, right = 0, 0
while right < len(s) :
if right - left + 1 <= longest :
right += 1
if right < len(s) :
cnt[s[right]] += 1
continue

maxxChar = max(cnt, key=lambda x: cnt[x])
sumOthers = right - left + 1 - cnt[maxxChar]

if sumOthers > k :
cnt[s[left]] -= 1
left += 1

else :
longest = right - left + 1
right += 1
if right < len(s) :
cnt[s[right]] += 1

return longest
```

#### [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>)
```Python
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
cnt = Counter()

left, right = 0, 0
maxFreq = 0
for right in range(len(s)) :
cnt[s[right]] += 1
maxFreq = max(maxFreq, cnt[s[right]])

if right - left + 1 - maxFreq > k :
cnt[s[left]] -= 1
left += 1

return right - left + 1
```

Loading

0 comments on commit c04cabd

Please sign in to comment.