Skip to content

Commit 69b01c8

Browse files
committed
files added
1 parent 0e58922 commit 69b01c8

58 files changed

Lines changed: 3724 additions & 153 deletions

File tree

Some content is hidden

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

docs/app.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,66 @@ function escapeHtml(text) {
239239
);
240240
}
241241

242+
function normalizeRepoPath(basePath, href) {
243+
if (!href) return null;
244+
if (
245+
href.startsWith("http://") ||
246+
href.startsWith("https://") ||
247+
href.startsWith("mailto:") ||
248+
href.startsWith("#")
249+
) {
250+
return null;
251+
}
252+
253+
let raw = href;
254+
try {
255+
raw = decodeURIComponent(href);
256+
} catch {
257+
raw = href;
258+
}
259+
260+
// Strip optional leading slash or current-dir prefix used in markdown links.
261+
raw = raw.replace(/^\.?\//, "");
262+
263+
const baseParts = basePath ? basePath.split("/").slice(0, -1) : [];
264+
const parts = raw.split("/").filter(Boolean);
265+
const resolved = raw.startsWith("/") ? [] : [...baseParts];
266+
267+
for (const part of parts) {
268+
if (part === ".") continue;
269+
if (part === "..") {
270+
resolved.pop();
271+
continue;
272+
}
273+
resolved.push(part);
274+
}
275+
276+
return resolved.join("/");
277+
}
278+
279+
function wireMarkdownLinks(container, currentPath) {
280+
container.querySelectorAll("a[href]").forEach((link) => {
281+
const href = link.getAttribute("href");
282+
const targetPath = normalizeRepoPath(currentPath, href);
283+
if (!targetPath) {
284+
if (/^https?:\/\//.test(href || "")) {
285+
link.target = "_blank";
286+
link.rel = "noreferrer";
287+
}
288+
return;
289+
}
290+
291+
const exists = state.allFiles.some((file) => file.path === targetPath);
292+
if (!exists) return;
293+
294+
link.setAttribute("href", `?path=${encodeURIComponent(targetPath)}`);
295+
link.addEventListener("click", (event) => {
296+
event.preventDefault();
297+
loadFile(targetPath);
298+
});
299+
});
300+
}
301+
242302
function pathFromUrl() {
243303
const params = new URLSearchParams(window.location.search);
244304
return params.get("path") ? decodeURIComponent(params.get("path")) : null;
@@ -717,6 +777,7 @@ async function loadFile(path) {
717777
const html = window.marked.parse(text);
718778
const clean = window.DOMPurify ? window.DOMPurify.sanitize(html) : html;
719779
markdownContent.innerHTML = clean;
780+
wireMarkdownLinks(markdownContent, path);
720781
markdownContent.classList.remove("hidden");
721782
// Re-highlight code blocks inside markdown
722783
markdownContent.querySelectorAll("pre code").forEach((block) => {

docs/content/2 Pointers/strings/151. Reverse Words in a String.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,40 @@ def reverseWords(self, s: str) -> str:
4646
ans.append(s.pop())
4747
return " ".join(ans)
4848

49+
50+
# since strings in python are immutable so have to convert that into the list
4951
class Solution:
50-
def reverseWords(self, s: str) -> str:
51-
left,right=0,len(s)-1
5252

53-
while left<=right and s[left]==" ":
53+
def reverse(self,arr,left,right):
54+
while left<right:
55+
arr[left],arr[right]=arr[right],arr[left]
5456
left+=1
55-
56-
while left<=right and s[right]==" ":
5757
right-=1
5858

59-
d,word=deque(),[]
60-
61-
while left<=right:
62-
if s[left]==" " and word:
63-
d.appendleft("".join(word))
64-
word=[]
65-
elif s[left]!=" ":
66-
word.append(s[left])
67-
left+=1
68-
d.appendleft("".join(word))
69-
70-
return " ".join(d)
59+
def reverseWords(self, s: str) -> str:
60+
arr=list(s)
61+
n=len(arr)
62+
63+
self.reverse(arr,0,n-1)
64+
65+
write=0
66+
read=0
67+
68+
while read<n:
69+
if arr[read]!=' ':
70+
if write!=0:
71+
arr[write]=' '
72+
write+=1
73+
start=write
74+
while read<n and arr[read]!=' ':
75+
arr[write]=arr[read]
76+
write+=1
77+
read+=1
78+
79+
self.reverse(arr,start,write-1)
80+
else:
81+
read+=1
82+
return ''.join(arr[:write])
7183

7284

7385
# Complexity Analysis

docs/content/2 Pointers/strings/5. Longest Palindromic Substring.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,54 @@ def check(i,j):
8888

8989
# Space complexity: O(1)
9090
# We don't count the answer as part of the space complexity. Thus, all we use are a few integer variables.
91+
92+
93+
# Manacher's Algorithm
94+
95+
class Solution:
96+
def longestPalindrome(self,s: str) -> str:
97+
# 1. Insert separators to handle even-length cases uniformly
98+
t = "#" + "#".join(s) + "#"
99+
n = len(t)
100+
101+
# array to store palindrome radii
102+
p = [0] * n
103+
104+
# current right boundary and center of the longest known palindrome
105+
center = right = 0
106+
107+
for i in range(n):
108+
mirror = 2*center - i # mirrored index relative to current center
109+
110+
# 2. Use previously known palindrome if inside boundary
111+
if i < right:
112+
p[i] = min(right - i, p[mirror])
113+
114+
# 3. Try to expand around i
115+
while (i + p[i] + 1 < n and i - p[i] - 1 >= 0 and
116+
t[i + p[i] + 1] == t[i - p[i] - 1]):
117+
p[i] += 1
118+
119+
# 4. Update center + right boundary if extended
120+
if i + p[i] > right:
121+
center = i
122+
right = i + p[i]
123+
124+
# 5. Find longest radius
125+
max_len = max(p)
126+
max_center = p.index(max_len)
127+
128+
# convert center back to original indices (remove '#')
129+
start = (max_center - max_len) // 2
130+
return s[start: start + max_len]
131+
132+
133+
# Complexity Analysis
134+
# Given n as the length of s,
135+
136+
# Time complexity: O(n)
137+
# From Wikipedia (the implementation they describe is slightly different from the above code, but it's the same algorithm):
138+
# The algorithm runs in linear time. This can be seen by noting that Center strictly increases after each outer loop and the sum Center + Radius is non-decreasing. Moreover, the number of operations in the first inner loop is linear in the increase of the sum Center + Radius while the number of operations in the second inner loop is linear in the increase of Center. Since Center ≤ 2n+1 and Radius ≤ n, the total number of operations in the first and second inner loops is O(n) and the total number of operations in the outer loop, other than those in the inner loops, is also O(n). The overall running time is therefore O(n).
139+
140+
# Space complexity: O(n)
141+
# We use sPrime and palindromeRadii, both of length O(n).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1. Minimize the Maximum or Maximize the minimum.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/description/
2+
3+
'''
4+
You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.
5+
6+
You need to distribute all products to the retail stores following these rules:
7+
8+
A store can only be given at most one product type but can be given any amount of it.
9+
After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.
10+
Return the minimum possible x.
11+
12+
13+
14+
Example 1:
15+
16+
Input: n = 6, quantities = [11,6]
17+
Output: 3
18+
Explanation: One optimal way is:
19+
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
20+
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
21+
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
22+
Example 2:
23+
24+
Input: n = 7, quantities = [15,10,10]
25+
Output: 5
26+
Explanation: One optimal way is:
27+
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
28+
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
29+
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
30+
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
31+
Example 3:
32+
33+
Input: n = 1, quantities = [100000]
34+
Output: 100000
35+
Explanation: The only optimal way is:
36+
- The 100000 products of type 0 are distributed to the only store.
37+
The maximum number of products given to any store is max(100000) = 100000.
38+
39+
40+
Constraints:
41+
42+
m == quantities.length
43+
1 <= m <= n <= 105
44+
1 <= quantities[i] <= 105
45+
'''
46+
47+
48+
49+

docs/content/Operating Systems/Concurrency/Dining Philosophers.py renamed to docs/content/Binary Search/Sweep Coverage/Painter's Partition.py

File renamed without changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# https://leetcode.com/problems/maximum-non-negative-product-in-a-matrix/description/
2+
3+
'''
4+
You are given a m x n matrix grid. Initially, you are located at the top-left corner (0, 0), and in each step, you can only move right or down in the matrix.
5+
Among all possible paths starting from the top-left corner (0, 0) and ending in the bottom-right corner (m - 1, n - 1), find the path with the maximum non-negative product. The product of a path is the product of all integers in the grid cells visited along the path.
6+
Return the maximum non-negative product modulo 109 + 7. If the maximum product is negative, return -1.
7+
Notice that the modulo is performed after getting the maximum product.
8+
9+
10+
Example 1:
11+
Input: grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
12+
Output: -1
13+
Explanation: It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.
14+
15+
Example 2:
16+
Input: grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
17+
Output: 8
18+
Explanation: Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).
19+
20+
Example 3:
21+
Input: grid = [[1,3],[0,-4]]
22+
Output: 0
23+
Explanation: Maximum non-negative product is shown (1 * 0 * -4 = 0).
24+
25+
Constraints:
26+
m == grid.length
27+
n == grid[i].length
28+
1 <= m, n <= 15
29+
-4 <= grid[i][j] <= 4
30+
'''
31+
32+
class Solution:
33+
MOD=10**9+7
34+
def maxProductPath(self, grid: List[List[int]]) -> int:
35+
m,n=len(grid),len(grid[0])
36+
maxgt=[[0]*n for _ in range(m)]
37+
minlt=[[0]*n for _ in range(m)]
38+
39+
maxgt[0][0]=minlt[0][0]=grid[0][0]
40+
41+
for i in range(1,n):
42+
maxgt[0][i]=minlt[0][i]=maxgt[0][i-1]*grid[0][i]
43+
for i in range(1,m):
44+
maxgt[i][0]=minlt[i][0]=maxgt[i-1][0]*grid[i][0]
45+
46+
for i in range(1,m):
47+
for j in range(1,n):
48+
if grid[i][j]>=0:
49+
maxgt[i][j]=max(maxgt[i][j-1],maxgt[i-1][j])*grid[i][j]
50+
minlt[i][j]=min(minlt[i][j-1],minlt[i-1][j])*grid[i][j]
51+
else:
52+
maxgt[i][j]=min(minlt[i][j-1],minlt[i-1][j])*grid[i][j]
53+
minlt[i][j]=max(maxgt[i][j-1],maxgt[i-1][j])*grid[i][j]
54+
55+
return maxgt[m-1][n-1]%self.MOD if maxgt[m-1][n-1]>=0 else -1
56+
57+
58+
'''
59+
Complexity Analysis
60+
Let m and n be the number of rows and columns of the matrix.
61+
62+
Time complexity: O(mn).
63+
We traverse each cell once, and each transition takes constant time.
64+
65+
Space complexity: O(mn).
66+
We maintain two matrices of size m×n.
67+
'''

0 commit comments

Comments
 (0)