File tree Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Expand file tree Collapse file tree 3 files changed +57
-4
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Each problem directory includes:
7
7
8
8
There might still be some personal comments added for my learning purposes.
9
9
10
- ## Algorithms by Topic :
10
+ ## Algorithms:
11
11
12
12
- ** Math**
13
13
* Fibonacci Sequence
@@ -37,6 +37,14 @@ There might still be some personal comments added for my learning purposes.
37
37
[ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/sorting/sorting.js )
38
38
[ ` Test file ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/sorting/sorting.test.js )
39
39
40
+ - ** LeetCode**
41
+ * Valid Parentheses
42
+ [ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.js )
43
+ [ ` Test file ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.test.js )
44
+ * Integer to Roman
45
+ [ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/integer-to-roman/integer-to-roman.js )
46
+ [ ` Test file ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/integer-to-roman/integer-to-roman.test.js )
47
+
40
48
- ** All**
41
49
* Snail
42
50
[ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.js )
@@ -47,9 +55,7 @@ There might still be some personal comments added for my learning purposes.
47
55
* Tree relation
48
56
[ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/recursion-tree/recursion-tree.js )
49
57
[ ` Test file ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/recursion-tree/recursion-tree.test.js )
50
- * Valid Parentheses
51
- [ ` Solution ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.js )
52
- [ ` Test file ` ] ( https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.test.js )
58
+
53
59
54
60
## Usage
55
61
- Clone this repo
Original file line number Diff line number Diff line change
1
+ // Problem: Given an integer, convert it to a roman numeral
2
+ // https://leetcode.com/problems/integer-to-roman/
3
+ // Time complexity: O(n)
4
+
5
+ const integerToRoman = ( num ) => {
6
+ const romans = {
7
+ M : 1000 ,
8
+ CM : 900 ,
9
+ D : 500 ,
10
+ CD : 400 ,
11
+ C : 100 ,
12
+ XC : 90 ,
13
+ L : 50 ,
14
+ XL : 40 ,
15
+ X : 10 ,
16
+ IX : 9 ,
17
+ V : 5 ,
18
+ IV : 4 ,
19
+ I : 1 ,
20
+ } ;
21
+ const romanNum = [ ] ;
22
+ for ( const roman in romans ) {
23
+ if ( num >= romans [ roman ] ) {
24
+ const amount = Math . floor ( num / romans [ roman ] ) ;
25
+ for ( let i = 0 ; i < amount ; i ++ ) {
26
+ romanNum . push ( roman ) ;
27
+ }
28
+ num -= amount * romans [ roman ] ;
29
+ }
30
+ }
31
+ return romanNum . join ( "" ) ;
32
+ } ;
33
+
34
+ export default integerToRoman ;
Original file line number Diff line number Diff line change
1
+ import integerToRoman from "./integer-to-roman" ;
2
+
3
+ describe ( "Integer to Roman number" , ( ) => {
4
+ test ( "Integer to Roman number" , ( ) => {
5
+ expect ( integerToRoman ( 126 ) ) . toBe ( "CXXVI" ) ;
6
+ } ) ;
7
+ test ( "Integer to Roman number" , ( ) => {
8
+ expect ( integerToRoman ( 234 ) ) . toBe ( "CCXXXIV" ) ;
9
+ } ) ;
10
+ test ( "Integer to Roman number" , ( ) => {
11
+ expect ( integerToRoman ( 0 ) ) . toBe ( "" ) ;
12
+ } ) ;
13
+ } ) ;
You can’t perform that action at this time.
0 commit comments