Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef16476

Browse files
Merge branch 'master' of https://github.com/SumeetHaryani/problem-solving-javascript into logical-problems
2 parents 96ea1a9 + 8858d31 commit ef16476

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

‎README.md‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
1414
### Data Structures
1515

1616
- [Singly Linked List](src/_DataStructures_/LinkedList)
17+
1718
- [N Element From Last](src/_DataStructures_/LinkedList/element-from-last)
1819
- [Middle Node](src/_DataStructures_/LinkedList/middle-node)
1920
- [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list)
2021
- [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list)
22+
2123
- [Stack](src/_DataStructures_/Stack)
24+
2225
- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
2326
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
2427
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
2528
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)
2629
- [Postfix Expression Evaluation](src/_DataStructures_/Stack/postfix-expression-evaluation)
27-
30+
- [Remove Consecutive Repeated Digits](src/_DataStructures_/Stack/remove-consecutive-repeated-digits)
31+
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
32+
2833

2934
- [Queue](src/_DataStructures_/Queue)
3035
- [Weave](src/_DataStructures_/Queue/weave)
@@ -42,6 +47,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
4247
- [Get Maze Path](src/_Problems_/get_subsequence)
4348
- [Get longest consecutive 1s](src/_Problems_/max-consecutive-1s)
4449
- [Get Max Char](src/_Problems_/maxchar)
50+
- [Get Smallest Common Number](src/_Problems_/get-smallest-common-number)
4551
- [Merge 2 Sorted Arrays](src/_Problems_/merge-two-sorted-arrays)
4652
- [Palindrome](src/_Problems_/palindrome)
4753
- [Product of Elements](src/_Problems_/product-of-elements)
@@ -67,9 +73,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
6773

6874
## CONTRIBUTION Guide
6975

70-
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
76+
It's great to know that you want to contribute to this repo. Thanks for taking interest. Before you start, read the following carefully.
7177

7278
- When adding a new **problem** with solution
79+
7380
- Take care of the filename convention (Very Important)
7481
- Problem statement should be there with examples
7582
- Make sure you add the Run Time complexity of your solution
@@ -78,7 +85,8 @@ It's great to know that you want to contribute to this repo. Thanks for taking i
7885
- Strictly follow ESLINT rules
7986

8087
- When adding a Unit Test
88+
8189
- Take care of the file name convention
8290
- Make sure CI (Travis) is passing
83-
84-
Keep an eye on this guide, it's subjected to change frequently.
91+
92+
Keep an eye on this guide, it's subjected to change frequently.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Revision to PR #35 where I implemented bullshit thinking of
3+
* new breakthrough :D
4+
*/
5+
6+
class TwoStacks {
7+
constructor(capacity) {
8+
this.data = [];
9+
this.top1 = -1;
10+
this.top2 = capacity;
11+
this.overflow = new Error('Overflow: Stack is full');
12+
13+
this.capacity = capacity;
14+
}
15+
16+
push1(value) {
17+
if (this.top1 < this.top2 - 1) {
18+
this.top1 += 1;
19+
this.data[this.top1] = value;
20+
} else {
21+
throw this.overflow;
22+
}
23+
}
24+
25+
push2(value) {
26+
if (this.top1 < this.top2 - 1) {
27+
this.top2 -= 1;
28+
this.data[this.top2] = value;
29+
} else {
30+
throw this.overflow;
31+
}
32+
}
33+
34+
pop1() {
35+
if (this.top1 >= 0) {
36+
const item = this.data[this.top1];
37+
delete this.data[this.top1];
38+
this.top1 -= 1;
39+
return item;
40+
}
41+
return -1;
42+
}
43+
44+
pop2() {
45+
if (this.top2 < this.capacity) {
46+
const item = this.data[this.top2];
47+
delete this.data[this.top2];
48+
this.top2 += 1;
49+
return item;
50+
}
51+
return -1;
52+
}
53+
}
54+
55+
module.exports = TwoStacks;
56+
57+
/** Test cases */
58+
59+
/*
60+
const s = new TwoStacks(4);
61+
62+
s.push1('a');
63+
console.log(s.data);
64+
65+
s.push2('a2');
66+
console.log(s.data);
67+
68+
s.push1('b');
69+
console.log(s.data);
70+
71+
s.push2('b2');
72+
console.log(s.data);
73+
74+
s.push2('d2');
75+
console.log(s.data);
76+
77+
s.push2('c23');
78+
console.log(s.data);
79+
80+
console.log(s.pop2());
81+
console.log(s.data);
82+
83+
console.log(s.pop1());
84+
console.log(s.data);
85+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Given an integer N, remove consecutive repeated digits from it.
3+
* Input:133445
4+
* Output:1345
5+
*/
6+
7+
const Stack = require('../index');
8+
9+
10+
function removeConsecutiveDigits(no) {
11+
let s = new Stack();
12+
let newNo = "";
13+
//initally push first digit into stack
14+
newNo += no[0];
15+
s.push(no[0]);
16+
for (let i = 1; i < no.length; i++) {
17+
const digit = no[i];
18+
//if stack top and incoming digit is same ignore it else append to newNo.
19+
if (s.peek() !== digit) {
20+
newNo += digit;
21+
s.push(digit);
22+
}
23+
}
24+
return newNo
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Get the common smallest number between two integer arrays
2+
3+
const getSmallestCommonNumber = (a1, a2) => {
4+
let map = {};
5+
let i = 0;
6+
let min;
7+
8+
while (a1.length > i || a2.length > i) {
9+
if (i < a1.length) {
10+
map[`${a1[i]}a`] = true;
11+
if (map[`${a1[i]}b`] && (min > a1[i] || !min)) {
12+
min = a1[i];
13+
}
14+
}
15+
16+
if (i < a2.length) {
17+
map[`${a2[i]}b`] = true;
18+
if (map[`${a2[i]}a`] && (min > a2[i] || !min)) {
19+
min = a2[i];
20+
}
21+
}
22+
23+
i++;
24+
}
25+
26+
return min || -1;
27+
};
28+
29+
module.exports = { getSmallestCommonNumber };

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /