Skip to content

Commit 59d4b8e

Browse files
committed
updated
1 parent cdb1b68 commit 59d4b8e

413 files changed

Lines changed: 29203 additions & 6 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.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This repo includes a static site under `docs/` with a collapsible folder navigat
66

77
- Set your repo owner and name in `docs/config.json` (or rely on auto-detection if using `https://<owner>.github.io/<repo>/`).
88
- In GitHub Pages settings, choose `GitHub Actions` as the source.
9-
- If the tree changes, regenerate `docs/tree.json` with `python3 docs/build_tree.py`.
9+
- If the tree changes, regenerate `docs/tree.json` (and `docs/content/`) with `python3 docs/build_tree.py`.
1010

1111
## Time Complexity
1212

docs/app.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const state = {
55
branch: "main",
66
currentPath: "README.md",
77
activeItem: null,
8+
useLocalContent: false,
89
};
910

1011
const treeRoot = document.getElementById("treeRoot");
@@ -38,8 +39,11 @@ function encodePath(path) {
3839
.join("/");
3940
}
4041

41-
function rawUrlForPath(path) {
42+
function fileUrlForPath(path) {
4243
const encoded = encodePath(path);
44+
if (state.useLocalContent) {
45+
return `content/${encoded}`;
46+
}
4347
return `https://raw.githubusercontent.com/${state.repoOwner}/${state.repoName}/${state.branch}/${encoded}`;
4448
}
4549

@@ -107,7 +111,7 @@ async function loadFile(path, meta = {}) {
107111
codeBlock.className = languageClass ? `language-${languageClass}` : "";
108112

109113
try {
110-
const response = await fetch(rawUrlForPath(path));
114+
const response = await fetch(fileUrlForPath(path));
111115
if (!response.ok) {
112116
throw new Error(`Failed to fetch ${path}`);
113117
}
@@ -275,6 +279,7 @@ async function init() {
275279
state.repoOwner = config.repoOwner || detected?.owner || "your-github-username";
276280
state.repoName = config.repoName || detected?.repo || "your-repo-name";
277281
state.branch = config.branch || "main";
282+
state.useLocalContent = Boolean(config.useLocalContent);
278283

279284
repoLink.href = `https://github.com/${state.repoOwner}/${state.repoName}`;
280285

@@ -301,7 +306,7 @@ async function init() {
301306

302307
copyLink.addEventListener("click", async () => {
303308
try {
304-
await navigator.clipboard.writeText(rawUrlForPath(state.currentPath));
309+
await navigator.clipboard.writeText(fileUrlForPath(state.currentPath));
305310
copyLink.textContent = "Copied";
306311
setTimeout(() => {
307312
copyLink.textContent = "Copy raw link";

docs/build_tree.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
22
import os
3+
import shutil
34

45
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
56
OUTPUT = os.path.join(ROOT, "docs", "tree.json")
7+
CONTENT_ROOT = os.path.join(ROOT, "docs", "content")
68
EXCLUDE_DIRS = {".git", "docs", "__pycache__"}
79

810

@@ -46,10 +48,33 @@ def build_tree(current_path):
4648

4749

4850
def main():
51+
if os.path.exists(CONTENT_ROOT):
52+
shutil.rmtree(CONTENT_ROOT)
53+
os.makedirs(CONTENT_ROOT, exist_ok=True)
54+
4955
tree = build_tree(ROOT)
5056
with open(OUTPUT, "w", encoding="utf-8") as handle:
5157
json.dump(tree, handle, indent=2)
58+
59+
for current_root, dirs, files in os.walk(ROOT):
60+
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS and not d.startswith(".")]
61+
rel_root = os.path.relpath(current_root, ROOT)
62+
target_root = os.path.join(CONTENT_ROOT, rel_root)
63+
os.makedirs(target_root, exist_ok=True)
64+
65+
for filename in files:
66+
if filename.startswith("."):
67+
continue
68+
src_path = os.path.join(current_root, filename)
69+
rel_path = os.path.relpath(src_path, ROOT)
70+
if rel_path.startswith("docs/"):
71+
continue
72+
dest_path = os.path.join(CONTENT_ROOT, rel_path)
73+
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
74+
shutil.copy2(src_path, dest_path)
75+
5276
print(f"Wrote {OUTPUT}")
77+
print(f"Mirrored content into {CONTENT_ROOT}")
5378

5479

5580
if __name__ == "__main__":

docs/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"repoOwner": "prakash-sa",
33
"repoName": "Competitive-Programming",
4-
"branch": "master"
4+
"branch": "master",
5+
"useLocalContent": true
56
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# https://leetcode.com/problems/two-sum/description/
2+
3+
'''
4+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
You can return the answer in any order.
7+
8+
Example 1:
9+
10+
Input: nums = [2,7,11,15], target = 9
11+
Output: [0,1]
12+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
13+
Example 2:
14+
15+
Input: nums = [3,2,4], target = 6
16+
Output: [1,2]
17+
Example 3:
18+
19+
Input: nums = [3,3], target = 6
20+
Output: [0,1]
21+
22+
23+
Constraints:
24+
25+
2 <= nums.length <= 104
26+
-109 <= nums[i] <= 109
27+
-109 <= target <= 109
28+
Only one valid answer exists.
29+
'''
30+
31+
class Solution:
32+
def twoSum(self, nums: List[int], target: int) -> List[int]:
33+
freq={}
34+
for i,num in enumerate(nums):
35+
k=target-num
36+
if k in freq:
37+
return [i,freq[k]]
38+
freq[num]=i
39+
return []
40+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# https://leetcode.com/problems/container-with-most-water/description/
2+
3+
'''
4+
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
5+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
6+
Return the maximum amount of water a container can store.
7+
Notice that you may not slant the container.
8+
9+
Example 1:
10+
Input: height = [1,8,6,2,5,4,8,3,7]
11+
Output: 49
12+
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
13+
14+
Example 2:
15+
Input: height = [1,1]
16+
Output: 1
17+
18+
Constraints:
19+
20+
n == height.length
21+
2 <= n <= 105
22+
0 <= height[i] <= 104
23+
'''
24+
25+
from collections import List
26+
27+
class Solution:
28+
def maxArea(self, height: List[int]) -> int:
29+
left=0
30+
right=len(height)-1
31+
ans=0
32+
while left<right:
33+
h=min(height[left],height[right])
34+
ans=max(ans,(right-left)*h)
35+
if height[left]>height[right]:
36+
right-=1
37+
else:
38+
left+=1
39+
return ans
40+
41+
# Time Complexity: O(n)
42+
# Space Complexity: O(1)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# https://www.hellointerview.com/learn/code/sliding-window/maximum-points-you-can-obtain-from-cards
2+
3+
'''
4+
There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.
5+
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
6+
Your score is the sum of the points of the cards you have taken.
7+
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
8+
9+
10+
Example 1:
11+
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
12+
Output: 12
13+
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
14+
Example 2:
15+
Input: cardPoints = [2,2,2], k = 2
16+
Output: 4
17+
Explanation: Regardless of which two cards you take, your score will always be 4.
18+
Example 3:
19+
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
20+
Output: 55
21+
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
22+
23+
24+
Constraints:
25+
1 <= cardPoints.length <= 105
26+
1 <= cardPoints[i] <= 104
27+
1 <= k <= cardPoints.length
28+
'''
29+
from collections import List
30+
31+
class Solution:
32+
def maxScore(self, cardPoints: List[int], k: int) -> int:
33+
total=sum(cardPoints[:k])
34+
ans=total
35+
n=len(cardPoints)
36+
for i in range(1,k+1):
37+
total-=cardPoints[k-i]
38+
total+=cardPoints[n-i]
39+
ans=max(ans,total)
40+
return ans
41+
42+
43+
# Complexity Analysis
44+
# Let k be the number of cards we need to select.
45+
# Time complexity: O(k). We are using two for loops of length k for calculation purposes. This gives us O(2⋅k), which in Big O notation is equal to O(k).
46+
# Space complexity: O(1). No extra space is used since all the calculations are done impromptu.
47+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# https://leetcode.com/problems/3sum/description/
2+
3+
'''
4+
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
5+
Notice that the solution set must not contain duplicate triplets.
6+
7+
Example 1:
8+
9+
Input: nums = [-1,0,1,2,-1,-4]
10+
Output: [[-1,-1,2],[-1,0,1]]
11+
Explanation:
12+
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
13+
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
14+
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
15+
The distinct triplets are [-1,0,1] and [-1,-1,2].
16+
Notice that the order of the output and the order of the triplets does not matter.
17+
18+
Example 2:
19+
Input: nums = [0,1,1]
20+
Output: []
21+
Explanation: The only possible triplet does not sum up to 0.
22+
23+
Example 3:
24+
Input: nums = [0,0,0]
25+
Output: [[0,0,0]]
26+
Explanation: The only possible triplet sums up to 0.
27+
28+
Constraints:
29+
3 <= nums.length <= 3000
30+
-105 <= nums[i] <= 105
31+
'''
32+
33+
34+
from typing import List
35+
36+
class Solution:
37+
def threeSum(self, nums: List[int]) -> List[List[int]]:
38+
nums.sort()
39+
res = []
40+
n = len(nums)
41+
42+
for i in range(n):
43+
# skip duplicate first numbers
44+
if i > 0 and nums[i] == nums[i-1]:
45+
continue
46+
# optional micro-prune since array is sorted
47+
if nums[i] > 0:
48+
break
49+
50+
l, r = i + 1, n - 1
51+
while l < r:
52+
s = nums[i] + nums[l] + nums[r]
53+
if s == 0:
54+
res.append([nums[i], nums[l], nums[r]])
55+
l += 1
56+
r -= 1
57+
# skip duplicates on both sides
58+
while l < r and nums[l] == nums[l-1]:
59+
l += 1
60+
while l < r and nums[r] == nums[r+1]:
61+
r -= 1
62+
elif s < 0:
63+
l += 1
64+
else:
65+
r -= 1
66+
return res
67+
68+
69+
# Complexity: O(n^2) time, O(1) extra space (excluding output).
70+
71+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
2+
3+
'''
4+
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
5+
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
6+
The tests are generated such that there is exactly one solution. You may not use the same element twice.
7+
Your solution must use only constant extra space.
8+
9+
Example 1:
10+
Input: numbers = [2,7,11,15], target = 9
11+
Output: [1,2]
12+
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
13+
14+
Example 2:
15+
Input: numbers = [2,3,4], target = 6
16+
Output: [1,3]
17+
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
18+
19+
Example 3:
20+
Input: numbers = [-1,0], target = -1
21+
Output: [1,2]
22+
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
23+
24+
25+
Constraints:
26+
27+
2 <= numbers.length <= 3 * 104
28+
-1000 <= numbers[i] <= 1000
29+
numbers is sorted in non-decreasing order.
30+
-1000 <= target <= 1000
31+
The tests are generated such that there is exactly one solution.
32+
'''
33+
34+
35+
# The Python Case (No Overflow)
36+
# Python's standard int type supports arbitrary-precision integers. This means they can grow to be as large as your machine's memory allows, unlike fixed-size integers in languages like C++ (int is typically 32-bit) or Java (int is 32-bit, long is 64-bit).
37+
38+
class Solution:
39+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
40+
if not numbers:
41+
return [-1,-1]
42+
left=0
43+
right=len(numbers)-1
44+
while left<right:
45+
k=numbers[left]+numbers[right]
46+
if k==target:
47+
return [left+1,right+1]
48+
elif k<target:
49+
left+=1
50+
else:
51+
right-=1
52+
return [-1,-1]
53+
54+
55+
# Complexity Analysis
56+
# Time complexity: O(n).
57+
# The input array is traversed at most once. Thus the time complexity is O(n).
58+
59+
# Space complexity: O(1).
60+
# We only use additional space to store two indices and the sum, so the space complexity is O(1).

0 commit comments

Comments
 (0)