Skip to content

Commit

Permalink
random problem solving
Browse files Browse the repository at this point in the history
  • Loading branch information
rajibkuet07 committed Sep 3, 2021
1 parent 16f6ec9 commit b5461d7
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 4 deletions.
4 changes: 4 additions & 0 deletions linked-list/singly-linked-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Functional Approach
*/

2 changes: 1 addition & 1 deletion linked-list/singly-linked-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function insert_after_specific_node( $data, $target ) {
$current_node = $this->head;

// check through the list for the target node
// if it it last node then skip
// if it is last node then skip
while (
$current_node->getData() !== $target &&
$current_node->getNext() !== null
Expand Down
41 changes: 41 additions & 0 deletions random-problems/coin-change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1.
// Your task is to write a program that will be used in a vending machine to return change.
// Assume that the vending machine will always want to return the least number of coins or notes.
// Devise a function getChange(M, P) where M is how much money was inserted into the machine
// and P the price of the item selected, that returns an array of integers representing
// the number of each denomination to return.

// Example:
// getChange(5, 0.99) // should return [1,0,0,0,0,4]

// getChange(3.14, 1.99) // should return [0,1,1,0,0,1]
// getChange(3, 0.01) // should return [4,0,2,1,1,2]
// getChange(4, 3.14) // should return [1,0,1,1,1,0]
// getChange(0.45, 0.34) // should return [1,0,1,0,0,0]

const coinChange_I = (money, price) => {
const coins = [1, 5, 10, 25, 50, 100];

let change = Math.round((money - price) * 100);
console.log(change);

for (let i = coins.length - 1; i >= 0; i--) {
let val = coins[i];
if (change >= val) {
coins[i] = Math.floor(change / val);
} else {
coins[i] = 0;
}
change = change - val * coins[i];
}

console.log(coins);
//return coins;
};

coinChange_I(5, 0.99); // should return [1,0,0,0,0,4]

coinChange_I(3.14, 1.99); // should return [0,1,1,0,0,1]
coinChange_I(3, 0.01); // should return [4,0,2,1,1,2]
coinChange_I(4, 3.14); // should return [1,0,1,1,1,0]
coinChange_I(0.45, 0.34); // should return [1,0,1,0,0,0]
7 changes: 4 additions & 3 deletions random-problems/contains-duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ function contains_duplicate_I_II( $numbers ) {

/**
* Contains duplicate second question format
* Approach 2
* O(nlogn) - time as sorting need nlogn
* O(1) - space
* Approach 1
* O(n) - space
*
* @param array $numbers array of numbers
* @return string
Expand All @@ -97,6 +96,8 @@ function contains_duplicate_II_I( $numbers ) {
/**
* Contains duplicate second question format
* Approach 2
* O(nlogn) - time as sorting need nlogn
* O(1) - space
*
* @param array $numbers array of numbers
* @return string
Expand Down
44 changes: 44 additions & 0 deletions random-problems/longest-increasing-subsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @param {number[]} nums
* @return {number}
*/
const lengthOfLIS = nums => {
if (nums.length === 0) return 0;
if (nums.length === 1) return 1;

const stack = [nums[0]];

for (let i = 1; i < nums.length; i++) {
// if item is grater than push it to the array
if (nums[i] > stack[stack.length - 1]) stack.push(nums[i]);
// else binary search and place the item in the correct position
else {
let pos = binarySearch(stack, nums[i]);
stack[pos] = nums[i];
}
console.log(nums[i], stack);
}
return stack.length;
};

const binarySearch = (stack, value, left = 0, right = stack.length - 1) => {
while (right - left > 1) {
let mid = left + Math.floor((right - left) / 2);

if (stack[mid] >= value) right = mid;
else left = mid;
}
return right;

// if ( left > right ) return -1;
// if ( left === right ) return left;

// let mid = left + Math.floor( ( right - left ) / 2 );

// if ( stack[mid] <= value && stack[mid + 1] > value ) return mid;
// else if ( value > stack[mid] ) return binarySearch( stack, value, mid + 1, right );
// else return binarySearch( stack, value, left, mid - 1 );
};

const length = lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]);
console.log(length);
41 changes: 41 additions & 0 deletions random-problems/maximum-product-subarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,44 @@ const maxProduct = nums => {

const max = maxProduct([2, -1, 3, -2, -4, 7]);
console.log(max);

const threeSum = nums => {
// [-1, 0, 1, 2, -1, -4]

let result = [];

// return if item count is less than 3
if (nums.length < 3) return result;

nums.sort((a, b) => a - b); // [ -4, -1, -1, 0, 1, 2 ]
console.log(nums);

for (let [index, number] of nums.entries()) {
let [left, right] = [index + 1, nums.length - 1];

if (number > 0) return result;

if (index > 0 && nums[index] === nums[index - 1]) continue;

while (left < right) {
let sum = number + nums[left] + nums[right];
console.log(number, left, right, sum);

if (sum > 0) {
right--;
} else if (sum < 0) {
left++;
} else {
console.log('Here');
result.push([number, nums[left], nums[right]]);
left++;
while (nums[left] === nums[left - 1] && left < right) {
left++;
}
}
}
}
return result;
};
const sum = threeSum([-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4]);
console.log(sum);

0 comments on commit b5461d7

Please sign in to comment.