-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgoPractice.js
More file actions
187 lines (144 loc) · 5.32 KB
/
Copy pathAlgoPractice.js
File metadata and controls
187 lines (144 loc) · 5.32 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
console.log("Starting AlgoPractice.js");
// Two Sum
function twoSum(numbers, target) {
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] + numbers[j] === target && i !== j) {
return [i, j];
}
}
}
}
console.log(`${twoSum([2, 7, 11, 15], 9)}`); // [0, 1]
console.log(`${twoSum([2, 7, 11, 15], 17)}`); // [0, 3]
console.log(`${twoSum([2, 7, 11, 15], 22)} \n`); // [1, 3]
// Move Zeros
function moveZeros(nums) {
let maxIndex = nums.length - 1;
for (let i = 0; i < maxIndex; i++) {
if (nums[i] === 0) {
nums.push(nums.splice(i, 1)[0]);
i--; // Decrement to re-check the same index
maxIndex--; // If we dont decrment maxIndex we will go out of bounds
}
}
return nums;
}
function moveZeroesTwoPointer(nums) {
let left = 0; // Points to the position for the next non-zero element
for (let right = 0; right < nums.length; right++) {
if (nums[right] !== 0) {
// Swap the current non-zero element with the element at 'left'
[nums[left], nums[right]] = [nums[right], nums[left]];
left++;
}
}
return nums;
};
console.log(`${moveZeros([0, 1, 0, 3, 12])}`); // [1, 3, 12, 0, 0]
console.log(`${moveZeros([2, 1, 3, 4, 0])}`);
console.log(`${moveZeros([3, 1, 4, 3, 12])}`);
console.log(`${moveZeros([0, 1, 0, 0, 3, 12])}`);
console.log(`${moveZeroesTwoPointer([0, 1, 0, 3, 12])} \n`);
// Find the Second Largest Element in an Array
function findSecondLargest(nums) {
if (nums.length < 2) return -1;
let first = -Infinity;
let second = -Infinity;
for (const num of nums) {
if (num > first) {
second = first;
first = num;
}
else if (num < first && num > second) second = num;
}
return second === -Infinity ? -1 : second;
}
console.log(`${findSecondLargest([1, 2, 3, 4, 5])}`); // 4
console.log(`${findSecondLargest([6, 5, 3, 2, 1])}`); // 5
console.log(`${findSecondLargest([5, 5])}`); // -1
console.log(`${findSecondLargest([3, 5, 5])}`); // 3
console.log(`${findSecondLargest([3, 5, 2, 8, 6])} \n`); // 6
// Contains Duplicate
function containsDuplicate(nums) {
const set = new Set();
for (let num of nums) {
if (set.has(num)) return true;
set.add(num);
}
return false;
}
console.log(`${containsDuplicate([1, 2, 3, 1])}`);
console.log(`${containsDuplicate([1, 2, 3, 4])} \n`);
// Longest Substring Without Repeating Characters
function longestSubstring(s) {
const set = new Set();
let left = 0;
let longestCount = 0;
for (let right = 0; right < s.length; right++) {
while (set.has(s[right])) {
set.delete(s[left]);
left++;
}
set.add(s[right]);
longestCount = Math.max(longestCount, right - left + 1);
}
return longestCount;
}
console.log(`${longestSubstring("abcabcbb")}`);
console.log(`${longestSubstring("bbbbb")}`);
console.log(`${longestSubstring("abcbde")}`);
console.log(`${longestSubstring("aaaabbcccc")} \n`);
// Anagram Check
function isAnagram(s, t) {
if (s.length !== t.length) return false;
const charMap = new Map();
for (const char of s) {
charMap.set(char, (charMap.get(char) || 0) + 1); // Increment the count for each character found in s
}
for (const char of t) {
if (!charMap.has(char) || charMap.get(char) === 0) return false;
charMap.set(char, charMap.get(char) - 1); // Now decrement to check for differences in character count
}
return true;
}
console.log(isAnagram("anagram", "nagaram")); // true
console.log(isAnagram("anagram", "nagarama")); // false
console.log(`${isAnagram("rat", "car")} \n`); // false
// Remove Adjacent Duplicates
function removeAdjacentDuplicates(input) {
const stack = [];
for (const char of input) {
if (stack.length > 0 && stack[stack.length - 1] === char) stack.pop();
else stack.push(char);
}
return stack.join("");
}
console.log(removeAdjacentDuplicates("abbaca")); // "ca"
console.log(`${removeAdjacentDuplicates("azxxzy")} \n`); // "ay"
// Reverse the First K Elements of a Queue
function reverseFirstK(queue, k) {
if (k < 0 || k > queue.length) return queue;
const outputQueue = [];
for (var i = k - 1; i >= 0; i--) {
outputQueue.push(queue[i]);
}
outputQueue.push(...queue.slice(k)); // Add the remaining elements after the first k elements
return outputQueue;
}
function reverseFirstKWithQueue(items, k) { // Seems slower but I think this is what the question was asking for??
if (k < 0 || k > items.length) return items;
const queue = [...items];
const stack = [];
for (let i = 0; i < k; i++) {
stack.push(queue.shift());
}
const result = [];
while (stack.length > 0) {
result.push(stack.pop());
}
result.push(...queue);
return result;
}
console.log(reverseFirstK([1, 2, 3, 4, 5], 3)); // Output → [3, 2, 1, 4, 5]
console.log(reverseFirstKWithQueue([1, 2, 3, 4, 5], 3)); // Output → [3, 2, 1, 4, 5]