File tree 6 files changed +192
-0
lines changed
6 files changed +192
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Algorithm and Data Structure
2
2
My personal note learn JavaScript
3
3
4
+ ## Curriculum
5
+ This curriculum based on the one of (ACM-ICPC-Preparation)[ https://github.com/NAU-ACM/ACM-ICPC-Preparation ] , but didn't follow the order.
6
+
7
+ | | Data Structure | Algorithm | Tutorial | Questions |
8
+ | --- | -------------- | --------- | -------- | --------- |
9
+ | 1 | <ul ><li >[ Queue] ( /dataStructure/queue/queue.js ) </li ><li >[ Stack] ( /dataStructure/stack/stack.js ) </li ></ul > | <ul ><li >DFS</li ><li >BFS</li ></ul > | [ Data Structures: Stacks and Queues (Youtube)] ( https://www.youtube.com/watch?v=wjI1WNcIntg ) | <ul ><li >[ Valid Parentheses] ( https://leetcode.com/problems/valid-parentheses/ ) </li ><li >[ Largest Rectangle in Histogram] ( https://leetcode.com/problems/largest-rectangle-in-histogram/ ) [ \( my solution\) ] ( /questions/largestRectangleArea.js ) </li ></ul >|
10
+
4
11
## algorithm
5
12
1 . [ useful tricky (tricky methods depends on JavaScript features)] ( /algorithm/useful_trick.md )
Original file line number Diff line number Diff line change
1
+ class Node {
2
+ constructor ( val , next = null ) {
3
+ this . val = val ;
4
+ this . next = next ;
5
+ }
6
+ }
7
+
8
+ export default class Queue {
9
+ constructor ( ) {
10
+ this . head = null ;
11
+ this . tail = null ;
12
+ }
13
+
14
+ isEmpty ( ) {
15
+ return ! this . tail ;
16
+ }
17
+
18
+ peek ( ) {
19
+ return this . head . val ;
20
+ }
21
+
22
+ add ( val ) {
23
+ const newNode = new Node ( val ) ;
24
+ if ( this . tail ) {
25
+ this . tail . next = newNode ;
26
+ }
27
+ this . tail = newNode ;
28
+
29
+ if ( ! this . head ) {
30
+ this . head = newNode ;
31
+ }
32
+ }
33
+
34
+ remove ( ) {
35
+ if ( this . isEmpty ( ) ) {
36
+ throw new Error ( 'The queue is empty!' ) ;
37
+ }
38
+
39
+ const headVal = this . head . val ;
40
+ this . head = this . head . next ;
41
+
42
+ if ( ! this . head ) {
43
+ this . tail = null ;
44
+ }
45
+
46
+ return headVal ;
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ import Queue from "./queue" ;
2
+
3
+ describe ( 'Queue' , ( ) => {
4
+ const queue = new Queue ( ) ;
5
+
6
+ it ( 'should init a empty queue' , ( ) => {
7
+ expect ( queue . isEmpty ( ) ) . toBeTruthy ( ) ;
8
+ } ) ;
9
+
10
+ it ( 'should add 1 in queue' , ( ) => {
11
+ queue . add ( 1 ) ;
12
+ expect ( queue . isEmpty ( ) ) . toBeFalsy ( ) ;
13
+ expect ( queue . peek ( ) ) . toBe ( 1 ) ;
14
+ } ) ;
15
+
16
+ it ( 'should push 2 in queue, peek should return 1' , ( ) => {
17
+ queue . add ( 2 ) ;
18
+ expect ( queue . peek ( ) ) . toBe ( 1 ) ;
19
+ } ) ;
20
+
21
+ it ( 'should remove 1' , ( ) => {
22
+ expect ( queue . remove ( ) ) . toBe ( 1 ) ;
23
+ } ) ;
24
+
25
+ it ( 'should remove 2, and queue is empty' , ( ) => {
26
+ expect ( queue . remove ( ) ) . toBe ( 2 ) ;
27
+ expect ( queue . isEmpty ( ) ) . toBeTruthy ( ) ;
28
+ } )
29
+ } ) ;
Original file line number Diff line number Diff line change
1
+ class Node {
2
+ constructor ( val , next = null ) {
3
+ this . val = val ;
4
+ this . next = next ;
5
+ }
6
+ }
7
+
8
+ export default class Stack {
9
+ constructor ( ) {
10
+ this . top = null ;
11
+ }
12
+
13
+ isEmpty ( ) {
14
+ return ! this . top ;
15
+ }
16
+
17
+ peek ( ) {
18
+ return this . top . val ;
19
+ }
20
+
21
+ push ( val ) {
22
+ const newNode = new Node ( val ) ;
23
+
24
+ if ( this . top ) {
25
+ newNode . next = this . top ;
26
+ }
27
+ this . top = newNode ;
28
+ }
29
+
30
+ pop ( ) {
31
+ if ( this . isEmpty ( ) ) {
32
+ throw new Error ( 'The stack is empty' ) ;
33
+ }
34
+
35
+ const curVal = this . top . val ;
36
+ this . top = this . top . next ;
37
+ return curVal ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ import Stack from "./stack" ;
2
+
3
+ describe ( 'Stack' , ( ) => {
4
+ const stack = new Stack ( ) ;
5
+
6
+ it ( 'should init a empty Stack' , ( ) => {
7
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
8
+ } ) ;
9
+
10
+ it ( 'should push 1 in stack' , ( ) => {
11
+ stack . push ( 1 ) ;
12
+ expect ( stack . isEmpty ( ) ) . toBeFalsy ( ) ;
13
+ expect ( stack . peek ( ) ) . toBe ( 1 ) ;
14
+ } ) ;
15
+
16
+ it ( 'should push 2 in stack, peek should return 2' , ( ) => {
17
+ stack . push ( 2 ) ;
18
+ expect ( stack . peek ( ) ) . toBe ( 2 ) ;
19
+ } ) ;
20
+
21
+ it ( 'should pop 2' , ( ) => {
22
+ expect ( stack . pop ( ) ) . toBe ( 2 ) ;
23
+ } ) ;
24
+
25
+ it ( 'should pop 1, and stack is empty' , ( ) => {
26
+ expect ( stack . pop ( ) ) . toBe ( 1 ) ;
27
+ expect ( stack . isEmpty ( ) ) . toBeTruthy ( ) ;
28
+ } )
29
+ } ) ;
30
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Given n non-negative integers representing the histogram's bar height where the width of each bar is 1,
3
+ * find the area of largest rectangle in the histogram.
4
+ * https://leetcode.com/problems/largest-rectangle-in-histogram/
5
+ *
6
+ * @param {number[] } heights
7
+ * @return {number }
8
+ */
9
+
10
+ /**
11
+ * 1. We need to caculate areas when each bar is the smallest bar;
12
+ * 2. This means to find the last left bar smaller than current bar, and first right bar smaller than current bar.
13
+ * 3. right is simple. In order to find the left, maintain a stack, in which the smaller bar in bottom.
14
+ * 4. loop through bars (from left to right), if cur bar larger than the one in top of the stack, just push in.
15
+ * 5. If cur bar smaller than top, cur bar is the right bar of the top bar. The bar under top is the left one.
16
+ */
17
+ var largestRectangleArea = function ( heights ) {
18
+ const stack = [ ] ;
19
+ let i = 0 ;
20
+ let maxArea = 0 ;
21
+
22
+ while ( i < heights . length ) {
23
+ if ( ! stack . length || heights [ i ] >= heights [ stack [ stack . length - 1 ] ] ) {
24
+ stack . push ( i ++ ) ;
25
+ } else {
26
+ const top = stack . pop ( ) ;
27
+ const curArea = heights [ top ] * ( stack . length ? i - stack [ stack . length - 1 ] - 1 : i ) ;
28
+ maxArea = Math . max ( maxArea , curArea ) ;
29
+ }
30
+ }
31
+
32
+ while ( stack . length ) {
33
+ const top = stack . pop ( ) ;
34
+ const curArea = heights [ top ] * ( stack . length ? i - stack [ stack . length - 1 ] - 1 : i ) ;
35
+ maxArea = Math . max ( maxArea , curArea ) ;
36
+ }
37
+
38
+ return maxArea ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments