Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2f0e665

Browse files
Chore: All Activites completed for Day-22
1 parent 4aa3204 commit 2f0e665

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed

‎Day22/Task.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Day 22: LeetCode Medium Problems 🚀
2+
3+
Welcome to Day 22! Today, we'll be tackling some medium-level challenges on LeetCode. These problems will push your problem-solving abilities and help you become more comfortable with advanced algorithms. Let's get started! 💪
4+
5+
## Tasks/Activities 📝
6+
7+
### Activity 1: Add Two Numbers ➕
8+
- [X] **Task 1:** Solve the "Add Two Numbers" problem on LeetCode.
9+
- Write a function that takes two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list.
10+
- Create a few test cases with linked lists and log the sum as a linked list.
11+
12+
### Activity 2: Longest Substring Without Repeating Characters 🔤
13+
- [X] **Task 2:** Solve the "Longest Substring Without Repeating Characters" problem on LeetCode.
14+
- Write a function that takes a string and returns the length of the longest substring without repeating characters.
15+
- Log the length for a few test cases, including edge cases.
16+
17+
### Activity 3: Container With Most Water 💧
18+
- [X] **Task 3:** Solve the "Container With Most Water" problem on LeetCode.
19+
- Write a function that takes an array of non-negative integers where each integer represents the height of a line drawn at each point. Find two lines that, together with the x-axis, form a container such that the container holds the most water.
20+
- Log the maximum amount of water for a few test cases.
21+
22+
### Activity 4: 3Sum 🌐
23+
- [X] **Task 4:** Solve the "3Sum" problem on LeetCode.
24+
- Write a function that takes an array of integers and finds all unique triplets in the array which sum to zero.
25+
- Log the triplets for a few test cases, including edge cases.
26+
27+
### Activity 5: Group Anagrams 🅰️🅱️🅾️
28+
- [X] **Task 5:** Solve the "Group Anagrams" problem on LeetCode.
29+
- Write a function that takes an array of strings and groups anagrams together.
30+
- Log the grouped anagrams for a few test cases.
31+
32+
## Feature Request 🎯
33+
34+
1. **Add Two Numbers Script:** Write a script that includes a function to solve the "Add Two Numbers" problem and logs the sum as a linked list.
35+
2. **Longest Substring Script:** Create a script that includes a function to find the longest substring without repeating characters and logs the length.
36+
3. **Container With Most Water Script:** Write a script that includes a function to find the container with the most water and logs the maximum amount of water.
37+
4. **3Sum Script:** Create a script that includes a function to find all unique triplets in an array that sum to zero and logs the triplets.
38+
5. **Group Anagrams Script:** Write a script that includes a function to group anagrams and logs the grouped anagrams.
39+
40+
## Achievement Unlocked 🏆
41+
42+
By the end of these activities, you will:
43+
44+
- ✅ Solve common medium-level LeetCode problems.
45+
- ✅ Apply advanced problem-solving skills to implement algorithms.
46+
- ✅ Understand and handle edge cases in more complex algorithmic solutions.
47+
- ✅ Gain confidence in solving medium-level coding challenges on LeetCode.
48+
49+
---
50+
51+
Happy Coding! 💻🔥

‎Day22/index.js

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
console.log("-------------------------------------------------");
2+
console.log("Activity 1: ");
3+
4+
// Task 1: Solve the "Add Two Numbers" problem on LeetCode.
5+
// Write a function that takes two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list.
6+
// Create a few test cases with linked lists and log the sum as a linked list.
7+
8+
class ListNode {
9+
constructor(val = 0, next = null) {
10+
this.val = val;
11+
this.next = next;
12+
}
13+
}
14+
15+
function addTwoNumbers(l1, l2) {
16+
const dummy = new ListNode();
17+
let current = dummy;
18+
let carry = 0;
19+
20+
while (l1 !== null || l2 !== null || carry !== 0) {
21+
const val1 = l1 ? l1.val : 0;
22+
const val2 = l2 ? l2.val : 0;
23+
const sum = val1 + val2 + carry;
24+
25+
carry = Math.floor(sum / 10);
26+
current.next = new ListNode(sum % 10);
27+
current = current.next;
28+
29+
if (l1 !== null) l1 = l1.next;
30+
if (l2 !== null) l2 = l2.next;
31+
}
32+
33+
return dummy.next;
34+
}
35+
36+
function createLinkedList(arr) {
37+
let head = new ListNode(arr[0]);
38+
let current = head;
39+
for (let i = 1; i < arr.length; i++) {
40+
current.next = new ListNode(arr[i]);
41+
current = current.next;
42+
}
43+
return head;
44+
}
45+
46+
function printLinkedList(head) {
47+
const result = [];
48+
while (head !== null) {
49+
result.push(head.val);
50+
head = head.next;
51+
}
52+
return result;
53+
}
54+
55+
const l1 = createLinkedList([2, 4, 3]);
56+
const l2 = createLinkedList([5, 6, 4]);
57+
const sum = addTwoNumbers(l1, l2);
58+
console.log(printLinkedList(sum));
59+
60+
const l3 = createLinkedList([0]);
61+
const l4 = createLinkedList([0]);
62+
const sum2 = addTwoNumbers(l3, l4);
63+
console.log(printLinkedList(sum2));
64+
65+
const l5 = createLinkedList([9, 9, 9]);
66+
const l6 = createLinkedList([1]);
67+
const sum3 = addTwoNumbers(l5, l6);
68+
console.log(printLinkedList(sum3));
69+
70+
console.log("-------------------------------------------------");
71+
console.log("Activity 2: ");
72+
73+
// Task 2: Solve the "Longest Substring Without Repeating Characters" problem on LeetCode.
74+
// Write a function that takes a string and returns the length of the longest substring without repeating characters.
75+
// Log the length for a few test cases, including edge cases.
76+
77+
function lengthOfLongestSubstring(s) {
78+
let map = new Map();
79+
let left = 0;
80+
let maxLength = 0;
81+
82+
for (let right = 0; right < s.length; right++) {
83+
if (map.has(s[right])) {
84+
left = Math.max(map.get(s[right]) + 1, left);
85+
}
86+
map.set(s[right], right);
87+
maxLength = Math.max(maxLength, right - left + 1);
88+
}
89+
90+
return maxLength;
91+
}
92+
93+
console.log(lengthOfLongestSubstring("abcabcbb"));
94+
console.log(lengthOfLongestSubstring("bbbbb"));
95+
console.log(lengthOfLongestSubstring("pwwkew"));
96+
console.log(lengthOfLongestSubstring(""));
97+
console.log(lengthOfLongestSubstring("au"));
98+
99+
100+
console.log("-------------------------------------------------");
101+
console.log("Activity 3: ");
102+
103+
// Task 3: Solve the "Container With Most Water" problem on LeetCode.
104+
// Write a function that takes an array of non-negative integers where each integer represents the height of a line drawn at each point. Find two lines that, together with the x-axis, form a container such that the container holds the most water.
105+
// Log the maximum amount of water for a few test cases.
106+
107+
function maxArea(height) {
108+
let left = 0;
109+
let right = height.length - 1;
110+
let maxArea = 0;
111+
112+
while (left < right) {
113+
const width = right - left;
114+
const minHeight = Math.min(height[left], height[right]);
115+
const area = width * minHeight;
116+
maxArea = Math.max(maxArea, area);
117+
118+
if (height[left] < height[right]) {
119+
left++;
120+
} else {
121+
right--;
122+
}
123+
}
124+
125+
return maxArea;
126+
}
127+
128+
console.log(maxArea([1,8,6,2,5,4,8,3,7]));
129+
console.log(maxArea([1,1]));
130+
console.log(maxArea([4,3,2,1,4]));
131+
console.log(maxArea([1,2,1]));
132+
console.log(maxArea([10,9,8,7,6,5,4,3,2,1]));
133+
134+
135+
136+
console.log("-------------------------------------------------");
137+
console.log("Activity 4: ");
138+
139+
// Task 4: Solve the "3Sum" problem on LeetCode.
140+
// Write a function that takes an array of integers and finds all unique triplets in the array which sum to zero.
141+
// Log the triplets for a few test cases, including edge cases.
142+
143+
function threeSum(nums) {
144+
const result = [];
145+
nums.sort((a, b) => a - b);
146+
147+
for (let i = 0; i < nums.length - 2; i++) {
148+
if (i > 0 && nums[i] === nums[i - 1]) continue;
149+
150+
let left = i + 1;
151+
let right = nums.length - 1;
152+
153+
while (left < right) {
154+
const sum = nums[i] + nums[left] + nums[right];
155+
156+
if (sum === 0) {
157+
result.push([nums[i], nums[left], nums[right]]);
158+
while (left < right && nums[left] === nums[left + 1]) left++;
159+
while (left < right && nums[right] === nums[right - 1]) right--;
160+
left++;
161+
right--;
162+
} else if (sum < 0) {
163+
left++;
164+
} else {
165+
right--;
166+
}
167+
}
168+
}
169+
return result;
170+
}
171+
172+
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
173+
console.log(threeSum([0, 0, 0, 0]));
174+
console.log(threeSum([1, 2, -2, -1]));
175+
console.log(threeSum([-4, -1, -1, 0, 1, 2]));
176+
console.log(threeSum([]));
177+
178+
179+
180+
console.log("-------------------------------------------------");
181+
console.log("Activity 5: ");
182+
183+
// Task 5: Solve the "Group Anagrams" problem on LeetCode.
184+
// Write a function that takes an array of strings and groups anagrams together.
185+
// Log the grouped anagrams for a few test cases.
186+
187+
function groupAnagrams(strs) {
188+
const map = new Map();
189+
190+
for (const str of strs) {
191+
const sortedStr = str.split('').sort().join('');
192+
if (!map.has(sortedStr)) {
193+
map.set(sortedStr, []);
194+
}
195+
map.get(sortedStr).push(str);
196+
}
197+
198+
return Array.from(map.values());
199+
}
200+
201+
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
202+
console.log(groupAnagrams([""]))
203+
console.log(groupAnagrams(["a"]))
204+
console.log(groupAnagrams(["abc", "bca", "cab", "xyz", "zyx", "yxz"]));
205+
console.log(groupAnagrams(["rat", "tar", "art", "car", "arc"]));
206+
207+
208+
console.log("-------------------------------------------------");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /