Created
December 8, 2023 12:37
-
-
Save singhkunal2050/d9d862cf1727cb00668b69bddae8648e to your computer and use it in GitHub Desktop.
Teachmint - int
This file contains hidden or 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
// Given an integer k and a list of integers, count the number of distinct valid pairs of integers (a, b) in the list for which a + b = k | |
// Input: numbers = [0, -1, -2, 1, 2, 2, 2, 2, 3, 4, 5] , k = 2 | |
// Output: [(0, 2), (-1, 3), (-2, 4)] | |
// Note: Two pairs of integers (a, b) and (c, d) are considered distinct if at least one element of (a, b) does not also belong to (c, d). | |
// [0, -1, -2, 1, 2, 2, 2, 2, 3, 4, 5] , k = 2 | |
// nested loop | |
// for current index value, find another value within array that is = k - curr, get index of that value, push that pair | |
// [0, 2] , k =2 | |
{ | |
0 : 2, | |
2 : 0, | |
-1 : 3, | |
5 : -3 | |
} | |
"ABCDEFGAXYZQWBEFXY" | |
string | |
max_val = | |
temp -> current string | |
max_len -> override everytime, max_len > temp.len, override max_len | |
simple n2 | |
i = 0, temp -> A max_len = -1 | |
i = 6 temp -> ABXDEFG, max_len = 7 reset temp | |
i = 7 temp -> A | |
i = 12 temp -> ABEFXY , max_len will be still 7 | |
start from | |
----- | |
// Design a stack that supports push, pop, top, and retrieving the minimum element in (worst-case) constant time. | |
// Implement the MinStack class: | |
// MinStack() initializes the stack object. | |
// void push(val) pushes the element val onto the stack. | |
// int pop() removes the element on the top of the stack. You need not return it. EDIT: return it | |
// int top() gets the top element of the stack (without popping it) | |
// int getMin() retrieves the minimum element in the stack (without popping it) | |
// Example | |
// Push -2, 0, -3 | |
// GetMin should give -3 now | |
// Pop | |
// GetMin should give -2 | |
class Stack { | |
private min; | |
private stack; | |
constructor(){ | |
this.min = []; | |
this.stack = []; | |
} | |
push(value) { | |
this.stack.push(value); | |
updateMin(value, 'push'); | |
} | |
private updateMin(value, source){ | |
if(source === 'push'){ | |
if(this.min.length === 0){ | |
this.min.push(value); | |
} else { | |
if(this.min[this.min.length - 1] > value){ | |
this.min.push(value) | |
} | |
} | |
} else if(source === 'pop') { | |
if(value === this.min[this.min.length - 1]){ | |
this.min.pop(); | |
} | |
} | |
} | |
pop() { | |
let value = this.stack.pop(); | |
updateMin(value, 'pop') | |
return value; | |
} | |
getMin() { | |
return this.min[this.min.length -1]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment