Skip to content

Commit 2627da3

Browse files
Some improvements and solution update
1 parent 6d17f4c commit 2627da3

23 files changed

+826
-789
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ leetcode.ipr
1313
leetcode.iws
1414
out/
1515
problems/src.iml
16-
.idea/codeStyles/
17-
.idea/copyright/
18-
.idea/markdown-navigator/
16+
.idea/*
17+
.gradle/

problems/src/array/MaxConsecutiveOnes.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ public static void main(String[] args) {
1515
//
1616
}
1717

18-
public int findMaxConsecutiveOnes(int[] nums) {
19-
int max = 0;
20-
boolean flag = false;
21-
int count = 0;
22-
for(int i = 0; i < nums.length; i ++){
23-
if(nums[i] == 1){
24-
if(!flag){
25-
flag = true;
26-
}
27-
count++;
28-
max = Math.max(max, count);
29-
} else{
30-
count = 0;
31-
flag = false;
32-
}
18+
public int findMaxConsecutiveOnes(int[] nums) {
19+
int max = 0;
20+
boolean flag = false;
21+
int count = 0;
22+
for (int i = 0; i < nums.length; i++) {
23+
if (nums[i] == 1) {
24+
if (!flag) {
25+
flag = true;
3326
}
34-
return max;
27+
count++;
28+
max = Math.max(max, count);
29+
} else {
30+
count = 0;
31+
flag = false;
32+
}
3533
}
36-
34+
return max;
35+
}
3736
}

problems/src/array/MaxConsecutiveOnesII.java

+44-45
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,59 @@
1313
* stream? In other words, you can't store all numbers coming from the stream as it's too large to
1414
* hold in memory. Could you solve it efficiently?
1515
*
16-
* Solution: O(N)
17-
* Maintain a left and right auxiliary array with counts of contagious 1's from both directions.
18-
* Now, iterate through the array and flip a 0 to 1 and sum up left and right contagious sum of 1's and return the
19-
* max sum as the answer
16+
* <p>Solution: O(N) Maintain a left and right auxiliary array with counts of contagious 1's from
17+
* both directions. Now, iterate through the array and flip a 0 to 1 and sum up left and right
18+
* contagious sum of 1's and return the max sum as the answer
2019
*/
2120
public class MaxConsecutiveOnesII {
2221
public static void main(String[] args) {
2322
//
2423
}
25-
public int findMaxConsecutiveOnes(int[] nums) {
26-
int[] L = new int[nums.length];
27-
int[] R = new int[nums.length];
28-
boolean flag = false;
29-
int count = 0;
30-
int max = 0;
31-
for(int j = 0; j < nums.length; j ++){
32-
if(nums[j] == 1){
33-
if(!flag){
34-
flag = true;
35-
}
36-
count++;
37-
L[j] = count;
38-
} else{
39-
count = 0;
40-
flag = false;
41-
L[j] = count;
42-
}
43-
max = Math.max(max, count);
44-
}
4524

46-
flag = false;
47-
count = 0;
48-
for(int j = nums.length - 1; j >= 0; j --){
49-
if(nums[j] == 1){
50-
if(!flag){
51-
flag = true;
52-
}
53-
count++;
54-
R[j] = count;
55-
} else{
56-
count = 0;
57-
flag = false;
58-
R[j] = count;
59-
}
25+
public int findMaxConsecutiveOnes(int[] nums) {
26+
int[] L = new int[nums.length];
27+
int[] R = new int[nums.length];
28+
boolean flag = false;
29+
int count = 0;
30+
int max = 0;
31+
for (int j = 0; j < nums.length; j++) {
32+
if (nums[j] == 1) {
33+
if (!flag) {
34+
flag = true;
6035
}
36+
count++;
37+
L[j] = count;
38+
} else {
39+
count = 0;
40+
flag = false;
41+
L[j] = count;
42+
}
43+
max = Math.max(max, count);
44+
}
6145

62-
for(int i = 0; i < nums.length; i ++){
63-
if(nums[i] == 0){
64-
int l = i == 0 ? 0 : L[i - 1];
65-
int r = i == nums.length - 1 ? 0 : R[i + 1];
66-
max = Math.max(max, l + r + 1);
67-
}
46+
flag = false;
47+
count = 0;
48+
for (int j = nums.length - 1; j >= 0; j--) {
49+
if (nums[j] == 1) {
50+
if (!flag) {
51+
flag = true;
6852
}
69-
return max;
53+
count++;
54+
R[j] = count;
55+
} else {
56+
count = 0;
57+
flag = false;
58+
R[j] = count;
59+
}
7060
}
7161

62+
for (int i = 0; i < nums.length; i++) {
63+
if (nums[i] == 0) {
64+
int l = i == 0 ? 0 : L[i - 1];
65+
int r = i == nums.length - 1 ? 0 : R[i + 1];
66+
max = Math.max(max, l + r + 1);
67+
}
68+
}
69+
return max;
70+
}
7271
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package backtracking;
2+
23
import java.util.*;
4+
35
/**
46
* Created by gouthamvidyapradhan on 25/05/2019 Remember the story of Little Match Girl? By now, you
57
* know exactly what matchsticks the little match girl has, please find out a way you can make one
@@ -19,94 +21,96 @@
1921
* sum of the given matchsticks is in the range of 0 to 10^9. The length of the given matchstick
2022
* array will not exceed 15.
2123
*
22-
* Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array which sum up to the
23-
* length of a side of square.
24-
* Now, to check if a square can be made using all the sides sticks of different length, generate a hash for for each of
25-
* the combination which was generated in the previous step. The hash function should be such that it uses unique
26-
* indexes of each match stick. If 4 different hash values are formed using unique and all indices then a square is
27-
* possible.
24+
* <p>Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array
25+
* which sum up to the length of a side of square. Now, to check if a square can be made using all
26+
* the sides sticks of different length, generate a hash for for each of the combination which was
27+
* generated in the previous step. The hash function should be such that it uses unique indexes of
28+
* each match stick. If 4 different hash values are formed using unique and all indices then a
29+
* square is possible.
2830
*/
2931
public class MatchsticksToSquare {
30-
/**
31-
* Main method
32-
* @param args
33-
*/
32+
/**
33+
* Main method
34+
*
35+
* @param args
36+
*/
3437
public static void main(String[] args) {
35-
int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10};
38+
int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10};
3639
System.out.println(new MatchsticksToSquare().makesquare(A));
3740
}
3841

39-
class Pair {
40-
int value, i;
41-
Pair(int value, int i){
42-
this.value = value;
43-
this.i = i;
44-
}
42+
class Pair {
43+
int value, i;
44+
45+
Pair(int value, int i) {
46+
this.value = value;
47+
this.i = i;
4548
}
49+
}
4650

47-
public boolean makesquare(int[] nums) {
48-
if(nums.length == 0) return false;
49-
int sum = 0;
50-
for(int n : nums){
51-
sum += n;
52-
}
53-
int side = sum / 4;
54-
if((sum % 4) != 0) return false;
55-
List<List<Pair>> list = powerSet(nums, side);
56-
Set<Integer> hashIndex = new HashSet<>();
57-
int cons = 0;
58-
for(int i = 0; i < nums.length; i ++){
59-
cons |= (1 << i);
51+
public boolean makesquare(int[] nums) {
52+
if (nums.length == 0) return false;
53+
int sum = 0;
54+
for (int n : nums) {
55+
sum += n;
56+
}
57+
int side = sum / 4;
58+
if ((sum % 4) != 0) return false;
59+
List<List<Pair>> list = powerSet(nums, side);
60+
Set<Integer> hashIndex = new HashSet<>();
61+
int cons = 0;
62+
for (int i = 0; i < nums.length; i++) {
63+
cons |= (1 << i);
64+
}
65+
for (int i = 0; i < list.size(); i++) {
66+
for (int j = i + 1; j < list.size(); j++) {
67+
Set<Integer> indexList = new HashSet<>();
68+
List<Pair> list1 = list.get(i);
69+
List<Pair> list2 = list.get(j);
70+
int hash = 0;
71+
for (Pair l1 : list1) {
72+
indexList.add(l1.i);
73+
hash |= (1 << l1.i);
6074
}
61-
for(int i = 0; i < list.size(); i ++){
62-
for(int j = i + 1; j < list.size(); j ++){
63-
Set<Integer> indexList = new HashSet<>();
64-
List<Pair> list1 = list.get(i);
65-
List<Pair> list2 = list.get(j);
66-
int hash = 0;
67-
for(Pair l1 : list1){
68-
indexList.add(l1.i);
69-
hash |= (1 << l1.i);
70-
}
71-
boolean allUnique = true;
72-
for(Pair l2 : list2){
73-
if(indexList.contains(l2.i)) {
74-
allUnique = false;
75-
break;
76-
}
77-
indexList.add(l2.i);
78-
hash |= (1 << l2.i);
79-
}
80-
if(allUnique){
81-
hashIndex.add(hash);
82-
int complement = ((~ hash) & cons);
83-
if(hashIndex.contains(complement)) return true;
84-
}
75+
boolean allUnique = true;
76+
for (Pair l2 : list2) {
77+
if (indexList.contains(l2.i)) {
78+
allUnique = false;
79+
break;
8580
}
81+
indexList.add(l2.i);
82+
hash |= (1 << l2.i);
83+
}
84+
if (allUnique) {
85+
hashIndex.add(hash);
86+
int complement = ((~hash) & cons);
87+
if (hashIndex.contains(complement)) return true;
88+
}
8689
}
87-
return false;
8890
}
91+
return false;
92+
}
8993

90-
private List<List<Pair>> powerSet(int[] nums, int expectedSum){
91-
List<List<Pair>> result = new ArrayList<>();
92-
generate(0, nums, new ArrayList<>(), result, 0, expectedSum);
93-
return result;
94-
}
94+
private List<List<Pair>> powerSet(int[] nums, int expectedSum) {
95+
List<List<Pair>> result = new ArrayList<>();
96+
generate(0, nums, new ArrayList<>(), result, 0, expectedSum);
97+
return result;
98+
}
9599

96-
private void generate(int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum,
97-
int expected){
98-
if(i >= nums.length){
99-
if(sum == expected){
100-
List<Pair> pairs = new ArrayList<>(subList);
101-
result.add(pairs);
102-
}
103-
} else{
104-
if(sum + nums[i] <= expected){
105-
subList.add(new Pair(nums[i], i));
106-
generate(i + 1, nums, subList, result, sum + nums[i], expected);
107-
subList.remove(subList.size() - 1);
108-
}
109-
generate(i + 1, nums, subList, result, sum, expected);
100+
private void generate(
101+
int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum, int expected) {
102+
if (i >= nums.length) {
103+
if (sum == expected) {
104+
List<Pair> pairs = new ArrayList<>(subList);
105+
result.add(pairs);
110106
}
107+
} else {
108+
if (sum + nums[i] <= expected) {
109+
subList.add(new Pair(nums[i], i));
110+
generate(i + 1, nums, subList, result, sum + nums[i], expected);
111+
subList.remove(subList.size() - 1);
112+
}
113+
generate(i + 1, nums, subList, result, sum, expected);
111114
}
115+
}
112116
}

0 commit comments

Comments
 (0)