Skip to content

Commit 18581c6

Browse files
author
baochau.dinh
committed
js-concepts: leetcode binary search problems
1 parent 8ad967d commit 18581c6

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

Design Patterns/Singleton/customer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const FoodLogger = require('./singleton');
2+
const foodLogger = new FoodLogger().getFoodLogerInstance();
3+
4+
class Customer {
5+
constructor(order) {
6+
this.price = order.price,
7+
this.food = order.food,
8+
foodLogger.log(order);
9+
}
10+
}
11+
12+
module.exports = Customer;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const FoodLogger = require('./singleton');
2+
3+
const foodLogger = new FoodLogger().getFoodLogerInstance();
4+
5+
class Restaurant {
6+
constructor(inventory) {
7+
this.quantity = inventory.quantity,
8+
this.food = inventory.food,
9+
foodLogger.log(inventory)
10+
}
11+
}
12+
13+
module.exports = Restaurant;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* The singleton pattern helps us build the logs from the application
3+
* We can get them anywhere in our code base with exact the same instance
4+
* of the logger.
5+
*/
6+
7+
class FoodLogger {
8+
constructor() {
9+
this.foodLog = [];
10+
}
11+
12+
log(order) {
13+
this.foodLog.push(order.foodItem);
14+
}
15+
}
16+
17+
// this is singleton
18+
class FoodLoggerSingleton {
19+
constructor() {
20+
if (!FoodLoggerSingleton.instance) {
21+
FoodLoggerSingleton.instance = new FoodLogger();
22+
}
23+
}
24+
25+
getFoodLogerInstance() {
26+
return FoodLoggerSingleton.instance;
27+
}
28+
}
29+
30+
module.exports = FoodLoggerSingleton;

Design Patterns/Strategy/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"paymentMethod": {
3+
"strategy": "Paypal"
4+
}
5+
}

Design Patterns/Strategy/strategy.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* The strategy pattern is like an advanced version of if-else statement
3+
*/
4+
5+
interface CustomerInfoType {
6+
country: string
7+
emailAddress: string
8+
name: string
9+
accountNumber?: number
10+
address?: string
11+
cardNumber?: number
12+
city?: string
13+
routingNumber?: number
14+
state?: string
15+
}
16+
17+
export default class PaymentMethodStrategy {
18+
static BankAccount(customerInfo: CustomerInfoType) {
19+
const {name, accountNumber, routingNumber} = customerInfo;
20+
}
21+
22+
static BitCoin(customerInfo: CustomerInfoType) {
23+
const {emailAddress, accountNumber} = customerInfo;
24+
}
25+
26+
static CreditCard(customerInfo: CustomerInfoType) {
27+
const {name, cardNumber, emailAddress} = customerInfo;
28+
}
29+
30+
static MailIn(customerInfo: CustomerInfoType) {
31+
const {name, address, city, state} = customerInfo;
32+
}
33+
34+
static Paypal(customerInfo: CustomerInfoType) {
35+
const {emailAddress} = customerInfo;
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function ListNode(val, next) {
2+
this.val = (val === undefined ? 0 : val);
3+
this.next = (next === undefined ? null : next);
4+
}
5+
6+
function mergeTwoList (list1, list2) {
7+
let head = new ListNode();
8+
let curr = head;
9+
10+
let curr1 = list1, curr2 = list2;
11+
while (curr1 !== null && curr2 !== null) {
12+
if (curr1.val < curr2.val) {
13+
curr.next = curr1;
14+
curr1 = curr1.next;
15+
} else {
16+
curr.next = curr2;
17+
curr2 = curr2.next;
18+
}
19+
curr = curr.next;
20+
}
21+
22+
if (curr1 !== null) {
23+
curr.next = curr1;
24+
}
25+
26+
if (curr2 !== null) {
27+
curr.next = curr2;
28+
}
29+
30+
return head.next;
31+
}
32+
33+
let list1 = new ListNode()
34+
35+
console.log(mergeTwoList())
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const binarySearch = function (nums, target) {
2+
let left = 0, right = nums.length - 1;
3+
while (left <= right) {
4+
let mid = left + Math.floor((right - left) / 2);
5+
if (nums[mid] === target) {
6+
return mid;
7+
} else if (nums[mid] < target) {
8+
left = mid + 1;
9+
} else {
10+
right = mid - 1;
11+
}
12+
}
13+
return -1;
14+
}
15+
16+
const detectShift = function (nums) {
17+
let left = 0, right = nums.length - 1;
18+
let ans = 0;
19+
while (left < right) {
20+
let mid = left + Math.floor((right - left) / 2);
21+
if (nums[mid] > nums[left]) {
22+
ans = mid;
23+
left = mid;
24+
} else {
25+
right = mid;
26+
}
27+
}
28+
return ans;
29+
}
30+
31+
const search = function (nums, target) {
32+
let shiftIndx = detectShift(nums);
33+
let result = -1;
34+
if (nums[shiftIndx] === target) {
35+
return shiftIndx;
36+
}
37+
let first = binarySearch(nums.slice(0, shiftIndx + 1), target);
38+
let second = binarySearch(nums.slice(shiftIndx + 1, nums.length), target);
39+
40+
if (first === -1 && second === -1) {
41+
return result;
42+
} else {
43+
if (first !== -1) {
44+
return first;
45+
}
46+
if (second !== -1) {
47+
return shiftIndx + second + 1;
48+
}
49+
}
50+
}
51+
52+
console.log(search([4, 5, 6, 7, 0, 1, 2], 2));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Remove duplicates from sorted array
3+
*
4+
* return the length
5+
*/
6+
7+
function removeDuplicates(nums = []) {
8+
if (nums.length === 0) return 0;
9+
let first = 0, second = 1;
10+
while (first < nums.length && second < nums.length) {
11+
if (nums[first] !== nums[second]) {
12+
first = second;
13+
second++;
14+
} else {
15+
nums.splice(second, 1);
16+
}
17+
}
18+
19+
return nums.length;
20+
}
21+
22+
function removeDuplicatesV2(nums = []) {
23+
let frequenceMap = {};
24+
let result = [];
25+
26+
for (let i = 0; i < nums.length; i++) {
27+
frequenceMap[nums[i]] = frequenceMap[nums[i]] ? frequenceMap[nums[i]] + 1 : 1
28+
}
29+
30+
for (key in frequenceMap) {
31+
result.push(parseInt(key));
32+
}
33+
34+
return result;
35+
}
36+
37+
console.log(removeDuplicatesV2([1, 1, 2]));
38+
39+
/**
40+
*
41+
*
42+
*
43+
* 2 3 6 7 9 15 19
44+
*
45+
* 6 7 9 15 19 2 3
46+
* target = 2
47+
* nums[mid] = 15
48+
*
49+
*
50+
* 9 15 19 2 3 6 7
51+
*
52+
* 15 19 2 3 6 7 9
53+
* target = 19
54+
* nums[mid] = 3
55+
* nums[mid] < target && nums[left] < target
56+
*/

0 commit comments

Comments
 (0)