-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomplete_blind_75.py
More file actions
187 lines (152 loc) · 4.69 KB
/
complete_blind_75.py
File metadata and controls
187 lines (152 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""
Script to generate all remaining Blind 75 solutions
"""
import os
# Define all remaining problems with their solutions
problems = {
# String problems
"longest-substring-without-repeating-characters": {
"java": """import java.util.HashSet;
import java.util.Set;
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
int maxLength = 0;
int left = 0;
Set<Character> charSet = new HashSet<>();
for (int right = 0; right < n; right++) {
char currentChar = s.charAt(right);
while (charSet.contains(currentChar)) {
charSet.remove(s.charAt(left));
left++;
}
charSet.add(currentChar);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
}""",
"js": """/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
const n = s.length;
let maxLength = 0;
let left = 0;
const charSet = new Set();
for (let right = 0; right < n; right++) {
const currentChar = s[right];
while (charSet.has(currentChar)) {
charSet.delete(s[left]);
left++;
}
charSet.add(currentChar);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
};""",
"py": """class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
n = len(s)
max_length = 0
left = 0
char_set = set()
for right in range(n):
current_char = s[right]
while current_char in char_set:
char_set.remove(s[left])
left += 1
char_set.add(current_char)
max_length = max(max_length, right - left + 1)
return max_length"""
},
# Tree problems
"maximum-depth-of-binary-tree": {
"java": """class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}""",
"js": """/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (!root) {
return 0;
}
const leftDepth = maxDepth(root.left);
const rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
};""",
"py": """class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
return max(left_depth, right_depth) + 1"""
},
# Heap problems
"kth-largest-element-in-an-array": {
"java": """import java.util.PriorityQueue;
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int num : nums) {
minHeap.offer(num);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
}
}""",
"js": """/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function(nums, k) {
const minHeap = new MinPriorityQueue();
for (const num of nums) {
minHeap.enqueue(num);
if (minHeap.size() > k) {
minHeap.dequeue();
}
}
return minHeap.front().element;
};""",
"py": """import heapq
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
min_heap = []
for num in nums:
heapq.heappush(min_heap, num)
if len(min_heap) > k:
heapq.heappop(min_heap)
return min_heap[0]"""
}
}
def create_solution_files():
"""Create solution files for all problems"""
for problem_name, solutions in problems.items():
# Create directory if it doesn't exist
problem_dir = f"blind-75/{problem_name}"
if not os.path.exists(problem_dir):
os.makedirs(problem_dir)
# Create solution files
for lang, code in solutions.items():
filename = f"{problem_dir}/solution.{lang}"
with open(filename, 'w') as f:
f.write(code)
print(f"Created solutions for {problem_name}")
if __name__ == "__main__":
create_solution_files()