generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from pesto-students/week8
Assignment Week8
- Loading branch information
Showing
15 changed files
with
496 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function maxSubarraySum(arr){ | ||
let maxSum = 0; | ||
let currentSum = 0; | ||
|
||
for(el of arr){ | ||
currentSum += el; | ||
|
||
if(currentSum < 0){ | ||
currentSum = 0; | ||
} | ||
|
||
if(currentSum > maxSum){ | ||
maxSum = currentSum; | ||
} | ||
} | ||
|
||
return maxSum; | ||
} | ||
|
||
let maxSubarraySumValue = maxSubarraySum([1, 2, 3, 4, -10]) | ||
console.log(maxSubarraySumValue) | ||
|
||
|
||
maxSubarraySumValue = maxSubarraySum([-2, 1, -3, 4, -1, 2, 1, -5, 4]) | ||
console.log(maxSubarraySumValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function spiralMatrix(matrix){ | ||
let rowCount = matrix.length | ||
let colCount = matrix[0].length | ||
let x = 0; | ||
let y = 0; | ||
let res = [] | ||
let temp = 0; | ||
let totalrun = rowCount*colCount | ||
|
||
while(temp<totalrun){ | ||
|
||
for(let i=y; i<colCount; i++){ | ||
res.push(matrix[x][i]); | ||
temp++; | ||
} | ||
x++; | ||
|
||
for(let i=x; i<rowCount; i++){ | ||
res.push(matrix[i][colCount-1]); | ||
temp++; | ||
} | ||
|
||
colCount--; | ||
|
||
for(let i=colCount-1; i>=y; i--){ | ||
res.push(matrix[rowCount-1][i]); | ||
temp++; | ||
} | ||
|
||
rowCount--; | ||
|
||
for(let i=rowCount-1; i>=x; i--){ | ||
res.push(matrix[i][y]); | ||
temp++; | ||
} | ||
|
||
//x++; | ||
y++; | ||
//rowCount--; | ||
//colCount--; | ||
|
||
} | ||
return res; | ||
} | ||
|
||
let matrix = [[1,2,3], | ||
[4,5,6], | ||
[7,8,9]] | ||
console.log(spiralMatrix(matrix)) | ||
|
||
|
||
matrix = [[ 1, 2, 3, 4], | ||
[ 5, 6, 7, 8], | ||
[ 9,10,11,12], | ||
[13,14,15,16]] | ||
console.log(spiralMatrix(matrix)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function sortArray(arr){ | ||
let zero = 0; | ||
let two = arr.length; | ||
|
||
for(el of arr){ | ||
if(el == 0){ | ||
zero++; | ||
} | ||
if(el == 2){ | ||
two--; | ||
} | ||
} | ||
|
||
for(let i=0;i<zero;i++){ | ||
arr[i] = 0; | ||
} | ||
for(let i=zero;i<two;i++){ | ||
arr[i] = 1; | ||
} | ||
for(let i=two;i<arr.length;i++){ | ||
arr[i] = 2; | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
let arr = [0, 1, 2, 1, 0, 1] | ||
console.log(sortArray(arr)) | ||
|
||
arr = [1, 2, 1, 2, 2] | ||
console.log(sortArray(arr)) | ||
|
||
arr = [1, 0, 0, 1, 1] | ||
console.log(sortArray(arr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function max(a,b){ | ||
return a<b? b: a; | ||
} | ||
|
||
function min(a,b){ | ||
return a < b? a: b; | ||
} | ||
|
||
function maximumProfit(arr){ | ||
let maxprofit = 0; | ||
let minprofit = 99999999999; | ||
|
||
//for(let i=0; i<arr.length; i++){ | ||
// for(let j=i+1; j<arr.length; j++){ | ||
// maxprofit = max(maxprofit, arr[j]-arr[i]) | ||
// } | ||
//} | ||
|
||
for(let i=0;i<arr.length;i++){ | ||
minprofit = min(minprofit, arr[i]) | ||
let profit = arr[i] - minprofit; | ||
maxprofit = max(maxprofit,profit); | ||
} | ||
|
||
return maxprofit; | ||
} | ||
|
||
let arr = [7,1,5,3,6,4] | ||
console.log(maximumProfit(arr)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function findDifference(A,B){ | ||
dict = new Map(); | ||
for(let i=0;i<A.length;i++){ | ||
if(dict.has(B - A[i]) || dict.has(A[i]- B)){ | ||
return 1; | ||
} | ||
dict.set(A[i],1) | ||
} | ||
return 0; | ||
} | ||
|
||
let A = [5, 10, 3, 2, 50, 80] | ||
let B = 78 | ||
console.log(findDifference(A,B)) | ||
|
||
A = [-10, 20] | ||
B = 30 | ||
console.log(findDifference(A,B)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var threeSumClosest = function(nums, target) { | ||
if(nums.length < 3) return 0; | ||
nums.sort((a,b)=>a-b); | ||
let ans = nums[0] + nums[1] + nums[2]; | ||
|
||
for(let i=0; i< nums.length-2; i++){ | ||
let first = i; | ||
let second = i+1; | ||
let third = nums.length-1; | ||
|
||
while(second<third){ | ||
let sum = nums[first] + nums[second] + nums[third]; | ||
if(sum == target) return sum; | ||
|
||
if(Math.abs(target - sum) < Math.abs(target - ans)){ | ||
ans = sum; | ||
} | ||
if(sum > target){ | ||
third --; | ||
} | ||
else{ | ||
second ++; | ||
} | ||
} | ||
} | ||
return ans; | ||
}; | ||
|
||
console.log(threeSumClosest([4,0,5,-5,3,3,0,-4,-5], -2)) | ||
console.log(threeSumClosest([-1,2,1,-4], 1)) | ||
console.log(threeSumClosest([0,0,0], 0)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class Node{ | ||
constructor(element){ | ||
this.element = element; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class LinkedList{ | ||
constructor(){ | ||
this.head = null; | ||
this.last = null; | ||
this.length = 0; | ||
} | ||
|
||
addNode(element){ | ||
let newNode = new Node(element); | ||
|
||
if(this.head == null){ | ||
this.head = newNode; | ||
this.last = this.head; | ||
}else{ | ||
this.last.next = newNode; | ||
this.last = this.last.next; | ||
} | ||
this.length += 1; | ||
} | ||
|
||
display(){ | ||
console.log("Length:" + this.length); | ||
let newhead = this.head; | ||
|
||
for(let i=0;i<this.length;i++){ | ||
console.log("->" + newhead.element); | ||
newhead = newhead.next; | ||
} | ||
} | ||
|
||
reverse(){ | ||
|
||
if(this.length < 2) | ||
return; | ||
|
||
this.first = this.head; | ||
this.second = this.head.next; | ||
|
||
if(this.length == 2){ | ||
this.second.next = this.first; | ||
this.first.next = null; | ||
this.head = this.second; | ||
return; | ||
} | ||
|
||
|
||
this.first = this.head.next; | ||
this.second = this.first.next; | ||
this.third = this.second.next; | ||
this.head.next = null; | ||
this.first.next = this.head; | ||
|
||
for(let i=0; i<this.length-3; i++){ | ||
this.second.next = this.first; | ||
|
||
this.first = this.second; | ||
this.second = this.third; | ||
this.third = this.third.next; | ||
} | ||
this.head = this.second; | ||
this.head.next = this.first; | ||
} | ||
} | ||
|
||
|
||
let newLinkedList = new LinkedList; | ||
|
||
for(let i=1;i<=10;i++){ | ||
newLinkedList.addNode(i); | ||
} | ||
|
||
newLinkedList.display(); | ||
newLinkedList.reverse(); | ||
newLinkedList.display(); | ||
|
||
//Time complexity: O(n) | ||
//Space complexity: O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class Node{ | ||
constructor(element){ | ||
this.element = element; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class LinkedList{ | ||
constructor(){ | ||
this.head = null; | ||
this.last = null; | ||
this.length = 0; | ||
} | ||
|
||
addNode(element){ | ||
let newNode = new Node(element); | ||
|
||
if(this.head == null){ | ||
this.head = newNode; | ||
this.last = this.head; | ||
}else{ | ||
this.last.next = newNode; | ||
this.last = this.last.next; | ||
} | ||
this.length += 1; | ||
} | ||
|
||
rotateLinkedlist(k){ | ||
this.last.next = this.head; | ||
let first = this.last; | ||
let second = this.head; | ||
|
||
while(k--){ | ||
first = second; | ||
second = second.next; | ||
} | ||
|
||
first.next = null; | ||
this.head = second; | ||
} | ||
|
||
display(){ | ||
console.log("Length:" + this.length); | ||
let newhead = this.head; | ||
|
||
for(let i=0;i<this.length;i++){ | ||
console.log("->" + newhead.element); | ||
newhead = newhead.next; | ||
} | ||
} | ||
} | ||
|
||
let newLinkedList = new LinkedList; | ||
|
||
for(let i=1;i<=10;i++){ | ||
newLinkedList.addNode(i); | ||
} | ||
|
||
newLinkedList.display(); | ||
newLinkedList.rotateLinkedlist(3); | ||
newLinkedList.display(); | ||
|
||
//Time complexity: O(n) | ||
//Space complexity: O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function checkLoop(x,N){ | ||
if(x<1 || N<x){ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
|
||
const array = [1,3,4] | ||
console.log(checkLoop(2,3)) | ||
|
||
console.log(checkLoop(0,5)) | ||
|
||
//Time complexity: O(1) | ||
//Space complexity: O(1) | ||
|
Oops, something went wrong.