Skip to content

Commit c35a18e

Browse files
committed
added hatchways solutions and sortedArray in O(n) time solution
1 parent d0a8cde commit c35a18e

File tree

5 files changed

+119
-30
lines changed

5 files changed

+119
-30
lines changed

hatchwaysHelp/aecio-1-10-2020.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// "getTwoNumber"
2+
// g e t_t w
3+
// 0 1 2
4+
// "get_two_number"
5+
6+
function toSnakeCase(str) {
7+
let newStr = "";
8+
for (let i = 0; i < str.length; i++) {
9+
const currentChar = str[i];
10+
if (i !== str.length - 1) {
11+
const nextChar = str[i + 1];
12+
// if the next character is capitalized => add _ and the capitalized number
13+
// add the character
14+
if (nextChar === nextChar.toUpperCase()) {
15+
newStr += `${currentChar}_`;
16+
} else {
17+
newStr += currentChar.toLowerCase();
18+
}
19+
} else {
20+
newStr += currentChar.toLowerCase();
21+
}
22+
23+
}
24+
return newStr;
25+
}
26+
27+
28+
29+
30+
// {keyA:1, keyB: {keyC:'a', keyD:[]}}
31+
//{key_a:1, key_b: {key_c:'a', key_d:{}}}
32+
33+
console.log(toSnakeCase("getTwoNumbeR"))

hatchwaysHelp/shums-2-3-2010.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const addresses = [{ uid: 1, aid: 10, address: "1 Best Street" },
2+
{ uid: 2, aid: 20, address: "2 Best Street" }];
3+
4+
const names = [{ uid: 2, name: "Kevin" }, { uid: 3, name: "Shums" }];
5+
6+
// result = [{uid: 2, aid: 20, address: "2 Best Street", name: "Kevin"},
7+
// {uid: 1, aid: 10, address: "1 Best Street"},
8+
// { uid: 3, name: "Shums"}]
9+
//
10+
11+
const join = (names, addresses) => {
12+
const joined = [];
13+
// for (let i = 0; i < addresses.length; i++) {
14+
// const join = {};
15+
// const address = addresses[i];
16+
// }
17+
18+
const addressByUid = addresses.reduce((acc, curr) => {
19+
acc[curr.uid] = curr;
20+
return acc;
21+
}, {});
22+
23+
names.forEach((el) => {
24+
const { uid, name } = el
25+
if (addressByUid[uid]) {
26+
addressByUid[uid].name = name;
27+
} else if (addressByUid[uid] === undefined) {
28+
addressByUid[uid] = {
29+
uid, name
30+
};
31+
}
32+
})
33+
34+
return Object.values(addressByUid);
35+
}
36+
37+
console.log(join(names, addresses));

hatchwaysInterviewPrep/mergingIntervals.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
// Given a set of time intervals in any order,
2-
// merge all overlapping intervals into one and output
1+
// Given a set of time intervals in any order,
2+
// merge all overlapping intervals into one and output
33
// the result which should have only mutually exclusive intervals.
44
// Let the intervals be represented as pairs of integers for simplicity.
55

6-
// For example, let the given set of intervals be
6+
// For example, let the given set of intervals be
77
// { { 1, 3 }, { 2, 4 }, { 5, 7 }, { 6, 8 } }.
8-
// The intervals { 1, 3 } and { 2, 4 } overlap with each other,
9-
// so they should be merged and become { 1, 4 }.Similarly { 5, 7 }
8+
// The intervals { 1, 3 } and { 2, 4 } overlap with each other,
9+
// so they should be merged and become { 1, 4 }.Similarly { 5, 7 }
1010
// and { 6, 8 } should be merged and become { 5, 8 }
1111
// [1,3] [2, 6], [4,5]
1212
// [1,6] [4,5]
13-
const test1 = [[1,3], [2, 4], [3, 4], [5,7], [6,8]]
13+
const test1 = [[1, 3], [2, 4], [3, 4], [5, 7], [6, 8]]
1414

1515
function mergeIntervals(arr) {
1616
arr = arr.sort((a, b) => a[0] - b[0]);
17-
17+
1818
const stack = [arr[0]];
1919
for (let i = 1; i < arr.length; i++) {
2020
const current = arr[i];

hatchwaysInterviewPrep/thirdMax.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,44 @@
77
// Input: [3, 2, 1] 1
88
// Input: [1, 4, 2, 6, 9] 4
99

10-
function thirdMax (arr, n) {
10+
function thirdMax(arr, n) {
1111
// let maxNum = -Infinity;
1212
// let secondMaxNum = -Infinity;
1313
// let thirdMaxNum = -Infinity;
1414
// // let arr = new Array(6).fill(-Infinity);
15-
15+
1616
// for (const num of arr) {
1717
// if (num > maxNum) {
1818
// maxNum = num;
1919
// } else if (num < maxNum && num > secondMaxNum) {
2020
// secondMaxNum = num;
2121
// } else if (num < secondMaxNum && num > thirdMaxNum) {
22-
// thirdMaxNum =
22+
// thirdMaxNum =
2323
// }
2424
// }
2525
arr = arr.sort((a, b) => a - b);
2626
const arrSet = new Set(arr);
27-
28-
if (arr.length > 3) arr[arr.length - 1]
27+
28+
if (arr.length < 3) arr[arr.length - 1]
2929
return arr[arr.length - n];
30-
31-
30+
}
31+
3232
public int findKthLargest(int[] nums, int k) {
3333

34-
final PriorityQueue<Integer> pq = new PriorityQueue<>();
35-
for(int val : nums) {
36-
pq.offer(val);
34+
final PriorityQueue < Integer > pq = new PriorityQueue<>();
35+
for (int val : nums) {
36+
pq.offer(val);
3737

38-
if(pq.size() > k) {
39-
pq.poll();
40-
}
38+
if (pq.size() > k) {
39+
pq.poll();
4140
}
42-
return pq.peek();
41+
}
42+
return pq.peek();
4343
}
44-
[3, 1, 5, 9 , 6, 7] k = 3
45-
46-
47-
[9, 6, 7]
44+
[3, 1, 5, 9, 6, 7] k = 3
45+
46+
47+
[9, 6, 7]
4848
}
4949

5050
public int findKthLargest(int[] nums, int k) {

squaresOfSortedArray.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Example 2:
88
// Input: [-7,-3,2,3,11]
99
// Output: [4,9,9,49,121]
10-
10+
1111
// Note:
1212
// 1 <= A.length <= 10000
1313
// -10000 <= A[i] <= 10000
@@ -29,10 +29,10 @@ const squaresOfSortedArray = (arr) => {
2929
let negativeIdx = positiveIdx - 1;
3030
let tempIdx = 0;
3131
let res = [];
32-
while ( negativeIdx >= 0 && positiveIdx < arr.length) {
32+
while (negativeIdx >= 0 && positiveIdx < arr.length) {
3333
let negSquard = Math.pow(arr[negativeIdx], 2);
3434
let posSquared = Math.pow(arr[positiveIdx], 2);
35-
35+
3636
if (negSquard < posSquared) {
3737
res[tempIdx] = negSquard;
3838
negativeIdx--;
@@ -53,5 +53,24 @@ const squaresOfSortedArray = (arr) => {
5353
return res
5454
}
5555

56-
console.log(squaresOfSortedArray([-4,-1,0,3,10])) // [0,1,9,16,100]
57-
console.log(squaresOfSortedArray([-7,-3,2,3,11])) // [4,9,9,49,121]
56+
const squaresOfSortedArray2 = (arr) => {
57+
if (!arr.length) return [];
58+
const sortedSquares = Array(arr.length).fill(0);
59+
let left = 0;
60+
let right = arr.length - 1;
61+
for (let i = sortedSquares.length - 1; i >= 0; i--) {
62+
if (Math.abs(arr[left] > arr[right])) {
63+
sortedSquares[i] = Math.pow(arr[left], 2);
64+
left++;
65+
} else {
66+
sortedSquares[i] = Math.pow(arr[right], 2);
67+
right--;
68+
}
69+
}
70+
return sortedSquares;
71+
}
72+
73+
// console.log(squaresOfSortedArray([-4,-1,0,3,10])) // [0,1,9,16,100]
74+
// console.log(squaresOfSortedArray([-7,-3,2,3,11])) // [4,9,9,49,121]
75+
console.log(squaresOfSortedArray2([-4, -1, 0, 3, 10])) // [0,1,9,16,100]
76+
console.log(squaresOfSortedArray2([-7, -3, 2, 3, 11])) // [4,9,9,49,121]

0 commit comments

Comments
 (0)