Skip to content

Commit c04cabd

Browse files
committed
Daily
1 parent 1b3788b commit c04cabd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1165
-594
lines changed

.Readme Updater

README.md

+91-89
Large diffs are not rendered by default.

markdowns/Daily_Questions.md

+40-39
Large diffs are not rendered by default.

markdowns/Questions_By_Code_Length.md

+89-87
Large diffs are not rendered by default.

markdowns/Questions_By_Recent.md

+89-87
Large diffs are not rendered by default.

markdowns/Topics.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
------
66

77
- [Array](<by_topic/Array.md>) (176 completed)
8-
- [String](<by_topic/String.md>) (86 completed)
9-
- [Hash Table](<by_topic/Hash Table.md>) (85 completed)
8+
- [String](<by_topic/String.md>) (87 completed)
9+
- [Hash Table](<by_topic/Hash Table.md>) (86 completed)
1010
- [Depth-First Search](<by_topic/Depth-First Search.md>) (68 completed)
1111
- [Tree](<by_topic/Tree.md>) (66 completed)
1212
- [Binary Tree](<by_topic/Binary Tree.md>) (61 completed)
1313
- [Sorting](<by_topic/Sorting.md>) (55 completed)
1414
- [Breadth-First Search](<by_topic/Breadth-First Search.md>) (47 completed)
15-
- [Math](<by_topic/Math.md>) (46 completed)
15+
- [Math](<by_topic/Math.md>) (47 completed)
1616
- [Two Pointers](<by_topic/Two Pointers.md>) (43 completed)
1717
- [Linked List](<by_topic/Linked List.md>) (38 completed)
1818
- [Stack](<by_topic/Stack.md>) (35 completed)
1919
- [Greedy](<by_topic/Greedy.md>) (31 completed)
2020
- [Matrix](<by_topic/Matrix.md>) (30 completed)
21-
- [Simulation](<by_topic/Simulation.md>) (25 completed)
21+
- [Simulation](<by_topic/Simulation.md>) (26 completed)
2222
- [Bit Manipulation](<by_topic/Bit Manipulation.md>) (24 completed)
2323
- [Dynamic Programming](<by_topic/Dynamic Programming.md>) (20 completed)
2424
- [Design](<by_topic/Design.md>) (20 completed)
@@ -30,8 +30,8 @@
3030
- [Recursion](<by_topic/Recursion.md>) (14 completed)
3131
- [Backtracking](<by_topic/Backtracking.md>) (13 completed)
3232
- [Binary Search Tree](<by_topic/Binary Search Tree.md>) (13 completed)
33+
- [Sliding Window](<by_topic/Sliding Window.md>) (11 completed)
3334
- [Prefix Sum](<by_topic/Prefix Sum.md>) (10 completed)
34-
- [Sliding Window](<by_topic/Sliding Window.md>) (10 completed)
3535
- [Monotonic Stack](<by_topic/Monotonic Stack.md>) (9 completed)
3636
- [Queue](<by_topic/Queue.md>) (8 completed)
3737
- [Union Find](<by_topic/Union Find.md>) (8 completed)

markdowns/_2582. Pass the Pillow.md

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# 2582. [Pass the Pillow](<https://leetcode.com/problems/pass-the-pillow>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 05, 2024*
8+
>
9+
> *Last updated : July 05, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **[Math](<by_topic/Math.md>), [Simulation](<by_topic/Simulation.md>)**
15+
>
16+
> **Acceptance Rate** : **45.847 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
## Solutions
24+
25+
- [e2582 Daily.py](<../my-submissions/e2582 Daily.py>)
26+
- [e2582.c](<../my-submissions/e2582.c>)
27+
- [e2582.cpp](<../my-submissions/e2582.cpp>)
28+
- [e2582.cs](<../my-submissions/e2582.cs>)
29+
- [e2582.java](<../my-submissions/e2582.java>)
30+
- [e2582.js](<../my-submissions/e2582.js>)
31+
### Python
32+
#### [e2582 Daily.py](<../my-submissions/e2582 Daily.py>)
33+
```Python
34+
class Solution:
35+
def passThePillow(self, n: int, time: int) -> int:
36+
time %= 2 * (n - 1) # Remove there and backs
37+
if time < n :
38+
return time + 1
39+
time -= n - 1
40+
return n - time
41+
```
42+
43+
### C
44+
#### [e2582.c](<../my-submissions/e2582.c>)
45+
```C
46+
int passThePillow(int n, int time) {
47+
time %= 2 * (n - 1);
48+
if (time < n)
49+
return time + 1;
50+
time -= n - 1;
51+
return n - time;
52+
}
53+
```
54+
55+
### C++
56+
#### [e2582.cpp](<../my-submissions/e2582.cpp>)
57+
```C++
58+
class Solution {
59+
public:
60+
int passThePillow(int n, int time) {
61+
time %= 2 * (n - 1);
62+
if (time < n)
63+
return time + 1;
64+
time -= n - 1;
65+
return n - time;
66+
}
67+
};
68+
```
69+
70+
### C#
71+
#### [e2582.cs](<../my-submissions/e2582.cs>)
72+
```C#
73+
public class Solution {
74+
public int PassThePillow(int n, int time) {
75+
time %= 2 * (n - 1);
76+
if (time < n)
77+
return time + 1;
78+
time -= n - 1;
79+
return n - time;
80+
}
81+
}
82+
```
83+
84+
### Java
85+
#### [e2582.java](<../my-submissions/e2582.java>)
86+
```Java
87+
class Solution {
88+
public int passThePillow(int n, int time) {
89+
time %= 2 * (n - 1);
90+
if (time < n)
91+
return time + 1;
92+
time -= n - 1;
93+
return n - time;
94+
}
95+
}
96+
```
97+
98+
### JavaScript
99+
#### [e2582.js](<../my-submissions/e2582.js>)
100+
```JavaScript
101+
/**
102+
* @param {number} n
103+
* @param {number} time
104+
* @return {number}
105+
*/
106+
var passThePillow = function(n, time) {
107+
time %= 2 * (n - 1);
108+
if (time < n)
109+
return time + 1;
110+
time -= n - 1;
111+
return n - time;
112+
};
113+
```
114+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
# 424. [Longest Repeating Character Replacement](<https://leetcode.com/problems/longest-repeating-character-replacement>)
2+
3+
*[Back to top](<../README.md>)*
4+
5+
------
6+
7+
> *First completed : July 05, 2024*
8+
>
9+
> *Last updated : July 05, 2024*
10+
11+
12+
------
13+
14+
> **Related Topics** : **[Hash Table](<by_topic/Hash Table.md>), [String](<by_topic/String.md>), [Sliding Window](<by_topic/Sliding Window.md>)**
15+
>
16+
> **Acceptance Rate** : **54.193 %**
17+
18+
19+
------
20+
21+
*To see the question prompt, click the title.*
22+
23+
> #### Version 5 (Optimal)
24+
> ```
25+
> The final optimization that had to be made was not updating the max frequency
26+
> downwards at any given moment. Since we're only looking at the context of our
27+
> window, the maximal window will require the most repeated characters, since all
28+
> others must be changed to match them. Therefore, if the max frequency goes down,
29+
> we know for certain that that window won't be valid, so we should just
30+
> shift over an index for both the left and right pointers.
31+
>
32+
> This brought the runtime down to around 75ms, in line with all the
33+
> submitted optimal solutions and perfectly within the bell curve.
34+
> ```
35+
>
36+
> #### Version 4
37+
> ```
38+
> Here, I changed out the way I'd calculate the total number of
39+
> non-max frequency characters to a mathematical approach. I had overlooked
40+
> it in my haste initially, but the number of characters that would have to be
41+
> changed was simply the length - the frequency of the most frequent character.
42+
>
43+
> The effect was essentially unnoticable however based on how LeetCode
44+
> runs their tests and the time remained similar to Version 3.
45+
> ```
46+
>
47+
> #### Version 3
48+
> ```
49+
> Compared to Version 2, the improvment here was minimal. I adjusted the
50+
> tracking variable for the longest window to start at length k, since we'd
51+
> at minimum find a case where every letter is unique, thus giving us a result
52+
> of k+1 length.
53+
>
54+
> This reduced the time down to around 180ms and the 11% region.
55+
> ```
56+
>
57+
> #### Version 2
58+
> ```
59+
> In this version, I made the slight adjustment to only check windows
60+
> that were larger than the previous window size. This was a step
61+
> in the right direction, and was based on a similar idea as the optimal
62+
> solution as it turns out.
63+
>
64+
> This small adjustment resulted in a drop to 320ms per run.
65+
> ```
66+
>
67+
> #### Version 1
68+
> ```
69+
> This was my first attempt at the question which passed without
70+
> any TLE issues first try in under 9 minutes. However, it was on
71+
> the brink of a TLE. On average, the runtime was measured at the
72+
> bottom 5%, taking 520ms approx. The idea was correct,
73+
> but it did need some modifications.
74+
> ```
75+
76+
------
77+
78+
## Solutions
79+
80+
- [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>)
81+
- [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>)
82+
- [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>)
83+
- [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>)
84+
- [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>)
85+
### Python
86+
#### [m424 v1 O(n) but inefficient.py](<../my-submissions/m424 v1 O(n) but inefficient.py>)
87+
```Python
88+
class Solution:
89+
def characterReplacement(self, s: str, k: int) -> int:
90+
cnt = Counter(s[0])
91+
92+
longest = 0
93+
left, right = 0, 0
94+
while right < len(s) :
95+
maxxChar = max(cnt, key=lambda x: cnt[x])
96+
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])
97+
98+
if sumOthers > k :
99+
cnt[s[left]] -= 1
100+
left += 1
101+
102+
else :
103+
longest = max(longest, sumOthers + cnt[maxxChar])
104+
right += 1
105+
if right < len(s) :
106+
cnt[s[right]] += 1
107+
108+
return longest
109+
```
110+
111+
#### [m424 v2 40 percent improvement.py](<../my-submissions/m424 v2 40 percent improvement.py>)
112+
```Python
113+
class Solution:
114+
def characterReplacement(self, s: str, k: int) -> int:
115+
cnt = Counter(s[0])
116+
117+
longest = 0
118+
left, right = 0, 0
119+
while right < len(s) :
120+
if right - left + 1 <= longest :
121+
right += 1
122+
if right < len(s) :
123+
cnt[s[right]] += 1
124+
continue
125+
126+
maxxChar = max(cnt, key=lambda x: cnt[x])
127+
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])
128+
129+
if sumOthers > k :
130+
cnt[s[left]] -= 1
131+
left += 1
132+
133+
else :
134+
longest = right - left + 1
135+
right += 1
136+
if right < len(s) :
137+
cnt[s[right]] += 1
138+
139+
return longest
140+
```
141+
142+
#### [m424 v3 Additional bit of performance.py](<../my-submissions/m424 v3 Additional bit of performance.py>)
143+
```Python
144+
class Solution:
145+
def characterReplacement(self, s: str, k: int) -> int:
146+
cnt = Counter(s[0])
147+
148+
longest = min(k, len(s))
149+
left, right = 0, 0
150+
while right < len(s) :
151+
if right - left + 1 <= longest :
152+
right += 1
153+
if right < len(s) :
154+
cnt[s[right]] += 1
155+
continue
156+
157+
maxxChar = max(cnt, key=lambda x: cnt[x])
158+
sumOthers = sum([cnt[x] for x in cnt if x != maxxChar])
159+
160+
if sumOthers > k :
161+
cnt[s[left]] -= 1
162+
left += 1
163+
164+
else :
165+
longest = right - left + 1
166+
right += 1
167+
if right < len(s) :
168+
cnt[s[right]] += 1
169+
170+
return longest
171+
```
172+
173+
#### [m424 v4 Removed O(k) Summations for Math.py](<../my-submissions/m424 v4 Removed O(k) Summations for Math.py>)
174+
```Python
175+
class Solution:
176+
def characterReplacement(self, s: str, k: int) -> int:
177+
cnt = Counter(s[0])
178+
179+
longest = min(k, len(s))
180+
left, right = 0, 0
181+
while right < len(s) :
182+
if right - left + 1 <= longest :
183+
right += 1
184+
if right < len(s) :
185+
cnt[s[right]] += 1
186+
continue
187+
188+
maxxChar = max(cnt, key=lambda x: cnt[x])
189+
sumOthers = right - left + 1 - cnt[maxxChar]
190+
191+
if sumOthers > k :
192+
cnt[s[left]] -= 1
193+
left += 1
194+
195+
else :
196+
longest = right - left + 1
197+
right += 1
198+
if right < len(s) :
199+
cnt[s[right]] += 1
200+
201+
return longest
202+
```
203+
204+
#### [m424 v5 Optimal.py](<../my-submissions/m424 v5 Optimal.py>)
205+
```Python
206+
class Solution:
207+
def characterReplacement(self, s: str, k: int) -> int:
208+
cnt = Counter()
209+
210+
left, right = 0, 0
211+
maxFreq = 0
212+
for right in range(len(s)) :
213+
cnt[s[right]] += 1
214+
maxFreq = max(maxFreq, cnt[s[right]])
215+
216+
if right - left + 1 - maxFreq > k :
217+
cnt[s[left]] -= 1
218+
left += 1
219+
220+
return right - left + 1
221+
```
222+

0 commit comments

Comments
 (0)