Skip to content

Commit

Permalink
Some improvements and solution update
Browse files Browse the repository at this point in the history
  • Loading branch information
gouthampradhan committed Jul 13, 2019
1 parent 6d17f4c commit 2627da3
Show file tree
Hide file tree
Showing 23 changed files with 826 additions and 789 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ leetcode.ipr
leetcode.iws
out/
problems/src.iml
.idea/codeStyles/
.idea/copyright/
.idea/markdown-navigator/
.idea/*
.gradle/
33 changes: 16 additions & 17 deletions problems/src/array/MaxConsecutiveOnes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ public static void main(String[] args) {
//
}

public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
boolean flag = false;
int count = 0;
for(int i = 0; i < nums.length; i ++){
if(nums[i] == 1){
if(!flag){
flag = true;
}
count++;
max = Math.max(max, count);
} else{
count = 0;
flag = false;
}
public int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
boolean flag = false;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (!flag) {
flag = true;
}
return max;
count++;
max = Math.max(max, count);
} else {
count = 0;
flag = false;
}
}

return max;
}
}
89 changes: 44 additions & 45 deletions problems/src/array/MaxConsecutiveOnesII.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,59 @@
* stream? In other words, you can't store all numbers coming from the stream as it's too large to
* hold in memory. Could you solve it efficiently?
*
* Solution: O(N)
* Maintain a left and right auxiliary array with counts of contagious 1's from both directions.
* 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
* max sum as the answer
* <p>Solution: O(N) Maintain a left and right auxiliary array with counts of contagious 1's from
* both directions. 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 max sum as the answer
*/
public class MaxConsecutiveOnesII {
public static void main(String[] args) {
//
}
public int findMaxConsecutiveOnes(int[] nums) {
int[] L = new int[nums.length];
int[] R = new int[nums.length];
boolean flag = false;
int count = 0;
int max = 0;
for(int j = 0; j < nums.length; j ++){
if(nums[j] == 1){
if(!flag){
flag = true;
}
count++;
L[j] = count;
} else{
count = 0;
flag = false;
L[j] = count;
}
max = Math.max(max, count);
}

flag = false;
count = 0;
for(int j = nums.length - 1; j >= 0; j --){
if(nums[j] == 1){
if(!flag){
flag = true;
}
count++;
R[j] = count;
} else{
count = 0;
flag = false;
R[j] = count;
}
public int findMaxConsecutiveOnes(int[] nums) {
int[] L = new int[nums.length];
int[] R = new int[nums.length];
boolean flag = false;
int count = 0;
int max = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] == 1) {
if (!flag) {
flag = true;
}
count++;
L[j] = count;
} else {
count = 0;
flag = false;
L[j] = count;
}
max = Math.max(max, count);
}

for(int i = 0; i < nums.length; i ++){
if(nums[i] == 0){
int l = i == 0 ? 0 : L[i - 1];
int r = i == nums.length - 1 ? 0 : R[i + 1];
max = Math.max(max, l + r + 1);
}
flag = false;
count = 0;
for (int j = nums.length - 1; j >= 0; j--) {
if (nums[j] == 1) {
if (!flag) {
flag = true;
}
return max;
count++;
R[j] = count;
} else {
count = 0;
flag = false;
R[j] = count;
}
}

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
int l = i == 0 ? 0 : L[i - 1];
int r = i == nums.length - 1 ? 0 : R[i + 1];
max = Math.max(max, l + r + 1);
}
}
return max;
}
}
152 changes: 78 additions & 74 deletions problems/src/backtracking/MatchsticksToSquare.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package backtracking;

import java.util.*;

/**
* Created by gouthamvidyapradhan on 25/05/2019 Remember the story of Little Match Girl? By now, you
* know exactly what matchsticks the little match girl has, please find out a way you can make one
Expand All @@ -19,94 +21,96 @@
* sum of the given matchsticks is in the range of 0 to 10^9. The length of the given matchstick
* array will not exceed 15.
*
* Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array which sum up to the
* length of a side of square.
* Now, to check if a square can be made using all the sides sticks of different length, generate a hash for for each of
* the combination which was generated in the previous step. The hash function should be such that it uses unique
* indexes of each match stick. If 4 different hash values are formed using unique and all indices then a square is
* possible.
* <p>Solution: O(2 ^ N): Generate a power set of all combination of numbers for the given array
* which sum up to the length of a side of square. Now, to check if a square can be made using all
* the sides sticks of different length, generate a hash for for each of the combination which was
* generated in the previous step. The hash function should be such that it uses unique indexes of
* each match stick. If 4 different hash values are formed using unique and all indices then a
* square is possible.
*/
public class MatchsticksToSquare {
/**
* Main method
* @param args
*/
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10};
int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10};
System.out.println(new MatchsticksToSquare().makesquare(A));
}

class Pair {
int value, i;
Pair(int value, int i){
this.value = value;
this.i = i;
}
class Pair {
int value, i;

Pair(int value, int i) {
this.value = value;
this.i = i;
}
}

public boolean makesquare(int[] nums) {
if(nums.length == 0) return false;
int sum = 0;
for(int n : nums){
sum += n;
}
int side = sum / 4;
if((sum % 4) != 0) return false;
List<List<Pair>> list = powerSet(nums, side);
Set<Integer> hashIndex = new HashSet<>();
int cons = 0;
for(int i = 0; i < nums.length; i ++){
cons |= (1 << i);
public boolean makesquare(int[] nums) {
if (nums.length == 0) return false;
int sum = 0;
for (int n : nums) {
sum += n;
}
int side = sum / 4;
if ((sum % 4) != 0) return false;
List<List<Pair>> list = powerSet(nums, side);
Set<Integer> hashIndex = new HashSet<>();
int cons = 0;
for (int i = 0; i < nums.length; i++) {
cons |= (1 << i);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
Set<Integer> indexList = new HashSet<>();
List<Pair> list1 = list.get(i);
List<Pair> list2 = list.get(j);
int hash = 0;
for (Pair l1 : list1) {
indexList.add(l1.i);
hash |= (1 << l1.i);
}
for(int i = 0; i < list.size(); i ++){
for(int j = i + 1; j < list.size(); j ++){
Set<Integer> indexList = new HashSet<>();
List<Pair> list1 = list.get(i);
List<Pair> list2 = list.get(j);
int hash = 0;
for(Pair l1 : list1){
indexList.add(l1.i);
hash |= (1 << l1.i);
}
boolean allUnique = true;
for(Pair l2 : list2){
if(indexList.contains(l2.i)) {
allUnique = false;
break;
}
indexList.add(l2.i);
hash |= (1 << l2.i);
}
if(allUnique){
hashIndex.add(hash);
int complement = ((~ hash) & cons);
if(hashIndex.contains(complement)) return true;
}
boolean allUnique = true;
for (Pair l2 : list2) {
if (indexList.contains(l2.i)) {
allUnique = false;
break;
}
indexList.add(l2.i);
hash |= (1 << l2.i);
}
if (allUnique) {
hashIndex.add(hash);
int complement = ((~hash) & cons);
if (hashIndex.contains(complement)) return true;
}
}
return false;
}
return false;
}

private List<List<Pair>> powerSet(int[] nums, int expectedSum){
List<List<Pair>> result = new ArrayList<>();
generate(0, nums, new ArrayList<>(), result, 0, expectedSum);
return result;
}
private List<List<Pair>> powerSet(int[] nums, int expectedSum) {
List<List<Pair>> result = new ArrayList<>();
generate(0, nums, new ArrayList<>(), result, 0, expectedSum);
return result;
}

private void generate(int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum,
int expected){
if(i >= nums.length){
if(sum == expected){
List<Pair> pairs = new ArrayList<>(subList);
result.add(pairs);
}
} else{
if(sum + nums[i] <= expected){
subList.add(new Pair(nums[i], i));
generate(i + 1, nums, subList, result, sum + nums[i], expected);
subList.remove(subList.size() - 1);
}
generate(i + 1, nums, subList, result, sum, expected);
private void generate(
int i, int[] nums, List<Pair> subList, List<List<Pair>> result, int sum, int expected) {
if (i >= nums.length) {
if (sum == expected) {
List<Pair> pairs = new ArrayList<>(subList);
result.add(pairs);
}
} else {
if (sum + nums[i] <= expected) {
subList.add(new Pair(nums[i], i));
generate(i + 1, nums, subList, result, sum + nums[i], expected);
subList.remove(subList.size() - 1);
}
generate(i + 1, nums, subList, result, sum, expected);
}
}
}
Loading

0 comments on commit 2627da3

Please sign in to comment.