Skip to content

Commit 830a21e

Browse files
author
Prashant Jain
committed
Added problems to Math and Backtracking
1 parent 357b29c commit 830a21e

4 files changed

Lines changed: 322 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package in.knowledgegate.dsa.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Given n pairs of parentheses, write a function
8+
* to generate all combinations of well-formed
9+
* parentheses.
10+
*
11+
* Example 1:
12+
* Input: n = 3
13+
* Output: ["((()))","(()())","(())()","()(())","()()()"]
14+
*
15+
* Example 2:
16+
* Input: n = 1
17+
* Output: ["()"]
18+
*
19+
* Constraints:
20+
* 1 <= n <= 8
21+
*/
22+
public class GenerateParentheses {
23+
public static void main(String[] args) {
24+
GenerateParentheses generator =
25+
new GenerateParentheses();
26+
List<String> parentheses =
27+
generator.generateParenthesis(3);
28+
System.out.println("Generate output:");
29+
for (String parenthesis : parentheses) {
30+
System.out.print(" " + parenthesis);
31+
}
32+
}
33+
34+
public List<String> generateParenthesis(int n) {
35+
List<String> result = new ArrayList<>();
36+
backtrack(result, new StringBuilder(), 0, 0
37+
,n);
38+
return result;
39+
}
40+
41+
private void backtrack(List<String> result,
42+
StringBuilder cur, int open, int close,
43+
int max) {
44+
if (cur.length() == max*2) {
45+
result.add(cur.toString());
46+
return;
47+
}
48+
49+
if (open < max) {
50+
cur.append('(');
51+
backtrack(result, cur, open + 1, close,
52+
max);
53+
cur.deleteCharAt(cur.length() - 1);
54+
}
55+
if (close < open) {
56+
cur.append(')');
57+
backtrack(result, cur, open, close + 1,
58+
max);
59+
cur.deleteCharAt(cur.length() - 1);
60+
}
61+
}
62+
}
63+
64+
65+
66+
67+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* You are given a large integer represented as an
5+
* integer array digits, where each digits[i] is
6+
* the ith digit of the integer. The digits are
7+
* ordered from most significant to least significant
8+
* in left-to-right order. The large integer does
9+
* not contain any leading 0's.
10+
*
11+
* Increment the large integer by one and return
12+
* the resulting array of digits.
13+
*
14+
*
15+
* Example 1:
16+
* Input: digits = [1,2,3]
17+
* Output: [1,2,4]
18+
* Explanation: The array represents the integer 123.
19+
* Incrementing by one gives 123 + 1 = 124.
20+
* Thus, the result should be [1,2,4].
21+
*
22+
* Example 2:
23+
* Input: digits = [4,3,2,1]
24+
* Output: [4,3,2,2]
25+
* Explanation: The array represents the integer 4321.
26+
* Incrementing by one gives 4321 + 1 = 4322.
27+
* Thus, the result should be [4,3,2,2].
28+
*
29+
* Example 3:
30+
* Input: digits = [9]
31+
* Output: [1,0]
32+
* Explanation: The array represents the integer 9.
33+
* Incrementing by one gives 9 + 1 = 10.
34+
* Thus, the result should be [1,0].
35+
*
36+
* Constraints:
37+
* 1 <= digits.length <= 100
38+
* 0 <= digits[i] <= 9
39+
* digits does not contain any leading 0's.
40+
*/
41+
public class PlusOne {
42+
public static void main(String[] args) {
43+
PlusOne plusOne = new PlusOne();
44+
int[] res = plusOne.plusOne(new int[]{9,9,9
45+
,9});
46+
for (int item : res) {
47+
System.out.print(" " + item);
48+
}
49+
}
50+
51+
public int[] plusOne(int[] digits) {
52+
int carry = 1;
53+
for (int i = digits.length - 1; carry > 0 && i >= 0; i--) {
54+
int temp = carry;
55+
carry = (digits[i] + carry) / 10;
56+
digits[i] = (digits[i] + temp) % 10;
57+
}
58+
if (carry != 1) {
59+
return digits;
60+
} else {
61+
int[] res = new int[digits.length + 1];
62+
res[0] = 1;
63+
for (int i = 0; i < digits.length; i++) {
64+
res[i+1] = digits[i];
65+
}
66+
return res;
67+
}
68+
}
69+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
/**
4+
* Roman numerals are represented by seven different
5+
* symbols: I, V, X, L, C, D and M.
6+
*
7+
* Symbol Value
8+
* I 1
9+
* V 5
10+
* X 10
11+
* L 50
12+
* C 100
13+
* D 500
14+
* M 1000
15+
* For example, 2 is written as II in Roman numeral,
16+
* just two one's added together. 12 is written as
17+
* XII, which is simply X + II. The number 27 is
18+
* written as XXVII, which is XX + V + II.
19+
*
20+
* Roman numerals are usually written largest to
21+
* smallest from left to right. However, the numeral
22+
* for four is not IIII. Instead, the number four
23+
* is written as IV. Because the one is before the
24+
* five we subtract it making four. The same
25+
* principle applies to the number nine, which is
26+
* written as IX. There are six instances where
27+
* subtraction is used:
28+
*
29+
* I can be placed before V (5) and X (10) to make 4 and 9.
30+
* X can be placed before L (50) and C (100) to make 40 and 90.
31+
* C can be placed before D (500) and M (1000) to make 400 and 900.
32+
* Given a roman numeral, convert it to an integer.
33+
*
34+
*
35+
* Example 1:
36+
* Input: s = "III"
37+
* Output: 3
38+
* Explanation: III = 3.
39+
*
40+
* Example 2:
41+
* Input: s = "LVIII"
42+
* Output: 58
43+
* Explanation: L = 50, V= 5, III = 3.
44+
*
45+
* Example 3:
46+
* Input: s = "MCMXCIV"
47+
* Output: 1994
48+
* Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
49+
*
50+
*
51+
* Constraints:
52+
* 1 <= s.length <= 15
53+
* s contains only the characters
54+
* ('I', 'V', 'X', 'L', 'C', 'D', 'M').
55+
* It is guaranteed that s is a valid roman
56+
* numeral in the range [1, 3999].
57+
*/
58+
public class RomanToInteger {
59+
public static void main(String[] args) {
60+
RomanToInteger converter =
61+
new RomanToInteger();
62+
System.out.println("III:" + converter.romanToInt("III"));
63+
System.out.println("LVIII:" + converter.romanToInt("LVIII"));
64+
System.out.println("MCMXCIV:" + converter.romanToInt("MCMXCIV"));
65+
}
66+
67+
public int romanToInt(String s) {
68+
int result = 0;
69+
int i = 0;
70+
s = s + "00";
71+
72+
while (true) {
73+
char c = s.charAt(i);
74+
char next = s.charAt(i + 1);
75+
switch (c) {
76+
case 'I':
77+
if (next == 'V') {
78+
result += 4;
79+
i++;
80+
} else if (next == 'X') {
81+
result += 9;
82+
i++;
83+
} else {
84+
result += 1;
85+
}
86+
break;
87+
88+
case 'X':
89+
if (next == 'L') {
90+
result += 40;
91+
i++;
92+
} else if (next == 'C') {
93+
result += 90;
94+
i++;
95+
} else {
96+
result += 10;
97+
}
98+
break;
99+
100+
case 'C':
101+
if (next == 'D') {
102+
result += 400;
103+
i++;
104+
} else if (next == 'M') {
105+
result += 900;
106+
i++;
107+
} else {
108+
result += 100;
109+
}
110+
break;
111+
case 'V':
112+
result += 5;
113+
break;
114+
case 'L':
115+
result += 50;
116+
break;
117+
case 'D':
118+
result += 500;
119+
break;
120+
case 'M':
121+
result += 1000;
122+
break;
123+
case '0':
124+
default:
125+
return result;
126+
}
127+
i++;
128+
}
129+
}
130+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package in.knowledgegate.dsa.math;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Given an array, rotate the array to the right
7+
* by k steps, where k is non-negative.
8+
*
9+
* Example 1:
10+
* Input: nums = [1,2,3,4,5,6,7], k = 3
11+
* Output: [5,6,7,1,2,3,4]
12+
* Explanation:
13+
* rotate 1 steps to the right: [7,1,2,3,4,5,6]
14+
* rotate 2 steps to the right: [6,7,1,2,3,4,5]
15+
* rotate 3 steps to the right: [5,6,7,1,2,3,4]
16+
*
17+
* Example 2:
18+
* Input: nums = [-1,-100,3,99], k = 2
19+
* Output: [3,99,-1,-100]
20+
* Explanation:
21+
* rotate 1 steps to the right: [99,-1,-100,3]
22+
* rotate 2 steps to the right: [3,99,-1,-100]
23+
*
24+
*
25+
* Constraints:
26+
* 1 <= nums.length <= 105
27+
* -231 <= nums[i] <= 231 - 1
28+
* 0 <= k <= 105
29+
*/
30+
public class RotateArray {
31+
public static void main(String[] args) {
32+
RotateArray rotateArray = new RotateArray();
33+
int[] nums = new int[]{1,2,3,4,5,6,7};
34+
System.out.print("Rotating " + Arrays.toString(nums));
35+
rotateArray.rotate(nums, 10);
36+
System.out.println(" : " + Arrays.toString(nums));
37+
}
38+
public void rotate(int[] nums, int k) {
39+
// Another way: int[] copyArr = nums.clone();
40+
int[] copyArr = new int[nums.length];
41+
for (int i = 0; i < nums.length; i++) {
42+
copyArr[i] = nums[i];
43+
}
44+
45+
for (int i = 0; i < nums.length; i++) {
46+
nums[(i+k)%nums.length] = copyArr[i];
47+
}
48+
}
49+
}
50+
51+
52+
53+
54+
55+
56+

0 commit comments

Comments
 (0)