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 9a2e794

Browse files
Solved Problems
1 parent 0777240 commit 9a2e794

File tree

6 files changed

+190
-160
lines changed

6 files changed

+190
-160
lines changed

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ My accepted leetcode solutions to some of the common interview problems.
4949
- [Surface Area of 3D Shapes](problems/src/array/SurfaceAreaOfThreeDShapes.java) (Easy)
5050
- [Max Consecutive Ones](problems/src/array/MaxConsecutiveOnes.java) (Easy)
5151
- [Max Consecutive Ones II](problems/src/array/MaxConsecutiveOnesII.java) (Medium)
52+
- [Add to Array-Form of Integer](problems/src/array/AddToArrayFormOfInteger.java) (Easy)
5253

5354
#### [Backtracking](problems/src/backtracking)
5455

@@ -129,6 +130,7 @@ My accepted leetcode solutions to some of the common interview problems.
129130
- [Cracking the Safe](problems/src/depth_first_search/CrackingTheSafe.java) (Hard)
130131
- [All Paths From Source to Target](problems/src/depth_first_search/AllPathsFromSourceToTarget.java) (Medium)
131132
- [Max Area of Island](problems/src/depth_first_search/MaxAreaOfIsland.java) (Medium)
133+
- [Satisfiability of Equality Equations](problems/src/depth_first_search/SatisfiabilityOfEquations.java) (Medium)
132134

133135
#### [Design](problems/src/design)
134136

@@ -226,6 +228,7 @@ My accepted leetcode solutions to some of the common interview problems.
226228
- [IPO](problems/src/greedy/IPO.java) (Hard)
227229
- [String Without AAA or BBB](problems/src/greedy/StringWithout3A3B.java) (Medium)
228230
- [Boats to Save People](problems/src/greedy/BoatsToSavePeople.java) (Medium)
231+
- [Broken Calculator](problems/src/greedy/BrokenCalculator.java) (Medium)
229232

230233
#### [Hashing](problems/src/hashing)
231234

@@ -341,6 +344,7 @@ My accepted leetcode solutions to some of the common interview problems.
341344
- [Split Concatenated Strings](problems/src/string/SplitConcatenatedStrings.java) (Medium)
342345
- [Valid Word Square](problems/src/string/ValidWordSquare.java) (Easy)
343346
- [Reconstruct Original Digits from English](problems/src/string/ReconstructOriginalDigitsFromEnglish.java) (Medium)
347+
- [Push Dominoes](problems/src/string/PushDominoes.java) (Medium)
344348

345349
#### [Tree](problems/src/tree)
346350

@@ -404,4 +408,5 @@ My accepted leetcode solutions to some of the common interview problems.
404408
- [Smallest Range](problems/src/two_pointers/SmallestRange.java) (Hard)
405409
- [Subarray Product Less Than K](problems/src/two_pointers/SubarrayProductLessThanK.java) (Medium)
406410
- [Number of Matching Subsequences](problems/src/two_pointers/NumberOfMatchingSubsequences.java) (Medium)
411+
- [Subarrays with K Different Integers](problems/src/two_pointers/SubarraysWithKDifferentIntegers.java) (Hard)
407412

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
1-
package heap;
1+
package array;
2+
23
import java.math.BigInteger;
34
import java.util.*;
5+
46
/**
57
* Created by gouthamvidyapradhan on 25/07/2019
8+
*
9+
* <p>For a non-negative integer X, the array-form of X is an array of its digits in left to right
10+
* order. For example, if X = 1231, then the array form is [1,2,3,1].
11+
*
12+
* <p>Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
13+
*
14+
* <p>Example 1:
15+
*
16+
* <p>Input: A = [1,2,0,0], K = 34 Output: [1,2,3,4] Explanation: 1200 + 34 = 1234 Example 2:
17+
*
18+
* <p>Input: A = [2,7,4], K = 181 Output: [4,5,5] Explanation: 274 + 181 = 455 Example 3:
19+
*
20+
* <p>Input: A = [2,1,5], K = 806 Output: [1,0,2,1] Explanation: 215 + 806 = 1021 Example 4:
21+
*
22+
* <p>Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1 Output: [1,0,0,0,0,0,0,0,0,0,0] Explanation:
23+
* 9999999999 +たす 1 = 10000000000
24+
*
25+
* <p>Note:
26+
*
27+
* <p>1 <= A.length <= 10000 0 <= A[i] <= 9 0 <= K <= 10000 If A.length > 1, then A[0] != 0
28+
*
29+
* <p>Solution: O(N) use BigInteger to add long numbers
630
*/
7-
public class Task1 {
31+
public class AddToArrayFormOfInteger {
832
public static void main(String[] args) {
933
//
1034
}
1135

12-
public List<Integer> addToArrayForm(int[] A, int K) {
13-
StringBuilder sb = new StringBuilder();
14-
for(int a : A){
15-
sb.append(a);
16-
}
17-
BigInteger big = new BigInteger(sb.toString());
18-
BigInteger result = big.add(BigInteger.valueOf(K));
19-
String resultStr = result.toString();
20-
List<Integer> list = new ArrayList<>();
21-
for(char a : resultStr.toCharArray()){
22-
list.add(Integer.parseInt(String.valueOf(a)));
23-
}
24-
return list;
36+
public List<Integer> addToArrayForm(int[] A, int K) {
37+
StringBuilder sb = new StringBuilder();
38+
for (int a : A) {
39+
sb.append(a);
2540
}
41+
BigInteger big = new BigInteger(sb.toString());
42+
BigInteger result = big.add(BigInteger.valueOf(K));
43+
String resultStr = result.toString();
44+
List<Integer> list = new ArrayList<>();
45+
for (char a : resultStr.toCharArray()) {
46+
list.add(Integer.parseInt(String.valueOf(a)));
47+
}
48+
return list;
49+
}
2650
}
Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package heap;
1+
package depth_first_search;
2+
23
import java.util.*;
4+
35
/**
46
* Created by gouthamvidyapradhan on 25/07/2019 Given an array equations of strings that represent
57
* relationships between variables, each string equations[i] has length 4 and takes one of two
@@ -29,74 +31,72 @@
2931
* <p>1 <= equations.length <= 500 equations[i].length == 4 equations[i][0] and equations[i][3] are
3032
* lowercase letters equations[i][1] is either '=' or '!' equations[i][2] is '='
3133
*
32-
* Solution: O(N) For all the equations which are of the form 'a==b' form a graph of connected components. Start
33-
* assigning values to each of the connected components. All the nodes in the connected components should have the
34-
* same value assigned - If any of the connected components fails this criteria
35-
* then return false.
34+
* <p>Solution: O(N) For all the equations which are of the form 'a==b' form a graph of connected
35+
* components. Start assigning values to each of the connected components. All the nodes in the
36+
* connected components should have the same value assigned - If any of the connected components
37+
* fails this criteria then return false.
3638
*/
37-
public class Task2 {
39+
public class SatisfiabilityOfEquations {
3840
public static void main(String[] args) {
39-
String[] input = {"c==c","f!=a","f==b","b==c"};
40-
System.out.println(new Task2().equationsPossible(input));
41+
String[] input = {"c==c","f!=a","f==b","b==c"};
42+
System.out.println(new SatisfiabilityOfEquations().equationsPossible(input));
4143
}
4244

43-
private Set<Character> done;
44-
private Map<Character, Integer> valueMap;
45-
private int count = 0;
46-
public boolean equationsPossible(String[] equations) {
47-
Map<Character, List<Character>> graph = new HashMap<>();
48-
done = new HashSet<>();
49-
valueMap = new HashMap<>();
50-
for(String eq : equations){
51-
if(eq.charAt(1) == '='){
52-
graph.putIfAbsent(eq.charAt(0), new ArrayList<>());
53-
graph.get(eq.charAt(0)).add(eq.charAt(3));
54-
graph.putIfAbsent(eq.charAt(3), new ArrayList<>());
55-
graph.get(eq.charAt(3)).add(eq.charAt(0));
45+
private Set<Character> done;
46+
private Map<Character, Integer> valueMap;
47+
private int count = 0;
5648

57-
}
58-
}
59-
for(char c : graph.keySet()){
60-
if(!done.contains(c)){
61-
dfs(c, graph, ++count);
62-
}
63-
}
49+
public boolean equationsPossible(String[] equations) {
50+
Map<Character, List<Character>> graph = new HashMap<>();
51+
done = new HashSet<>();
52+
valueMap = new HashMap<>();
53+
for (String eq : equations) {
54+
if (eq.charAt(1) == '=') {
55+
graph.putIfAbsent(eq.charAt(0), new ArrayList<>());
56+
graph.get(eq.charAt(0)).add(eq.charAt(3));
57+
graph.putIfAbsent(eq.charAt(3), new ArrayList<>());
58+
graph.get(eq.charAt(3)).add(eq.charAt(0));
59+
}
60+
}
61+
for (char c : graph.keySet()) {
62+
if (!done.contains(c)) {
63+
dfs(c, graph, ++count);
64+
}
65+
}
6466

65-
for(String eq : equations){
66-
if(eq.charAt(1) == '!'){
67-
char a = eq.charAt(0);
68-
char b = eq.charAt(3);
69-
if(a == b) return false;
70-
if(valueMap.containsKey(a) && valueMap.containsKey(b)){
71-
if(valueMap.get(a).intValue() == valueMap.get(b).intValue()){
72-
return false;
73-
}
74-
}
75-
}
67+
for (String eq : equations) {
68+
if (eq.charAt(1) == '!') {
69+
char a = eq.charAt(0);
70+
char b = eq.charAt(3);
71+
if (a == b) return false;
72+
if (valueMap.containsKey(a) && valueMap.containsKey(b)) {
73+
if (valueMap.get(a).intValue() == valueMap.get(b).intValue()) {
74+
return false;
75+
}
7676
}
77-
returntrue;
77+
}
7878
}
79+
return true;
80+
}
7981

80-
81-
private boolean dfs(char node, Map<Character, List<Character>> graph, int value){
82-
done.add(node);
83-
valueMap.put(node, value);
84-
List<Character> children = graph.get(node);
85-
if(!children.isEmpty()){
86-
for(char c : children){
87-
if(!done.contains(c)){
88-
boolean status = dfs(c, graph, value);
89-
if(!status) {
90-
return status;
91-
}
92-
} else{
93-
if(valueMap.get(c) != value){
94-
return false;
95-
}
96-
}
97-
}
82+
private boolean dfs(char node, Map<Character, List<Character>> graph, int value) {
83+
done.add(node);
84+
valueMap.put(node, value);
85+
List<Character> children = graph.get(node);
86+
if (!children.isEmpty()) {
87+
for (char c : children) {
88+
if (!done.contains(c)) {
89+
boolean status = dfs(c, graph, value);
90+
if (!status) {
91+
return status;
92+
}
93+
} else {
94+
if (valueMap.get(c) != value) {
95+
return false;
96+
}
9897
}
99-
returntrue;
98+
}
10099
}
101-
100+
return true;
101+
}
102102
}
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package heap;
1+
package greedy;
2+
23
import java.util.*;
4+
35
/**
46
* Created by gouthamvidyapradhan on 25/07/2019 On a broken calculator that has a number showing on
57
* its display, we can perform two operations:
@@ -26,33 +28,33 @@
2628
*
2729
* <p>1 <= X <= 10^9 1 <= Y <= 10^9
2830
*
29-
* Solution: O(log Y) Arrive at the solution by working backwards starting from Y.
30-
* General idea is as follows.
31-
* If Y is even then find the minimum steps required to arrive at Y by finding the quotient after dividing by 2. If Y
32-
* is odd then find the minimum steps required to arrive at Y + 1 (even number) + 1 (to move backwards)
31+
* <p>Solution: O(log Y) Arrive at the solution by working backwards starting from Y. General idea
32+
* is as follows. If Y is even then find the minimum steps required to arrive at Y by finding the
33+
* quotient after dividing by 2. If Y is odd then find the minimum steps required to arrive at Y + 1
34+
* (even number) + 1 (to move backwards)
3335
*/
3436
public class BrokenCalculator {
3537
public static void main(String[] args) {
3638
//
3739
}
3840

39-
public int brokenCalc(int X, int Y) {
40-
if(X == Y) return 0;
41-
else if(Y < X) return X - Y;
42-
else {
43-
int count = 0;
44-
while(Y > X){
45-
if(Y % 2 == 0){
46-
Y /= 2;
47-
count++;
48-
} else{
49-
Y += 1;
50-
Y /= 2;
51-
count+=2;
52-
}
53-
}
54-
if(X == Y) return count;
55-
else return count + (X - Y);
41+
public int brokenCalc(int X, int Y) {
42+
if (X == Y) return 0;
43+
else if (Y < X) return X - Y;
44+
else {
45+
int count = 0;
46+
while (Y > X) {
47+
if (Y % 2 == 0) {
48+
Y /= 2;
49+
count++;
50+
} else {
51+
Y += 1;
52+
Y /= 2;
53+
count += 2;
5654
}
55+
}
56+
if (X == Y) return count;
57+
else return count + (X - Y);
5758
}
59+
}
5860
}
Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package math;
2-
importjava.util.*;
1+
package string;
2+
33
/**
44
* Created by gouthamvidyapradhan on 24/07/2019 There are N dominoes in a line, and we place each
55
* domino vertically upright.
@@ -32,44 +32,44 @@
3232
* <p>Input: "RR.L" Output: "RR.L" Explanation: The first domino expends no additional force on the
3333
* second domino. Note:
3434
*
35-
* <p>0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.'
35+
* <p>0 <= N <= 10^5 String dominoes contains only 'L', 'R' and '.' Solution: O(N)
3636
*/
37-
public class Task2 {
37+
public class PushDominoes {
3838
public static void main(String[] args) {
39-
System.out.println(new Task2().pushDominoes("RR.L"));
39+
System.out.println(new PushDominoes().pushDominoes("RR.L"));
4040
}
4141

42-
public String pushDominoes(String dominoes) {
43-
int R = -1, L = -1;
44-
char[] A = dominoes.toCharArray();
45-
for(int i = 0; i < A.length; i ++){
46-
if(A[i] == 'L'){
47-
if(R > L){
48-
int d = (i - R);
49-
int st;
50-
st = R + d/2;
51-
if((d % 2) == 0){
52-
A[st] = '.';
53-
}
54-
for(int j = st + 1; j < i; j ++){
55-
A[j] = 'L';
56-
}
57-
} else{
58-
for(int j = (L == -1 ? 0 : L); j < i; j ++){
59-
A[j] = 'L';
60-
}
61-
}
62-
L = i;
63-
} else {
64-
if(A[i] == 'R'){
65-
R = i;
66-
} else{
67-
if(R > L){
68-
A[i] = 'R';
69-
}
70-
}
71-
}
42+
public String pushDominoes(String dominoes) {
43+
int R = -1, L = -1;
44+
char[] A = dominoes.toCharArray();
45+
for (int i = 0; i < A.length; i++) {
46+
if (A[i] == 'L') {
47+
if (R > L) {
48+
int d = (i - R);
49+
int st;
50+
st = R + d / 2;
51+
if ((d % 2) == 0) {
52+
A[st] = '.';
53+
}
54+
for (int j = st + 1; j < i; j++) {
55+
A[j] = 'L';
56+
}
57+
} else {
58+
for (int j = (L == -1 ? 0 : L); j < i; j++) {
59+
A[j] = 'L';
60+
}
61+
}
62+
L = i;
63+
} else {
64+
if (A[i] == 'R') {
65+
R = i;
66+
} else {
67+
if (R > L) {
68+
A[i] = 'R';
69+
}
7270
}
73-
returnString.valueOf(A);
71+
}
7472
}
73+
return String.valueOf(A);
74+
}
7575
}

0 commit comments

Comments
(0)

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