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 f6feb9f

Browse files
Solved problems
1 parent 69d787c commit f6feb9f

23 files changed

+1560
-1
lines changed

‎README.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ My accepted leetcode solutions to some of the common interview problems.
4747
- [Valid Tic-Tac-Toe State](problems/src/array/ValidTicTacToeState.java) (Medium)
4848
- [Number of Subarrays with Bounded Maximum](problems/src/array/SubArraysWithBoundedMaximum.java) (Medium)
4949
- [Surface Area of 3D Shapes](problems/src/array/SurfaceAreaOfThreeDShapes.java) (Easy)
50+
- [Max Consecutive Ones](problems/src/array/MaxConsecutiveOnes.java) (Easy)
51+
- [Max Consecutive Ones II](problems/src/array/MaxConsecutiveOnesII.java) (Medium)
5052

5153
#### [Backtracking](problems/src/backtracking)
5254

@@ -67,7 +69,8 @@ My accepted leetcode solutions to some of the common interview problems.
6769
- [Expression Add Operators](problems/src/backtracking/ExpressionAddOperators.java) (Hard)
6870
- [Wildcard Matching](problems/src/backtracking/WildcardMatching.java) (Hard)
6971
- [Letter Case Permutation](problems/src/backtracking/LetterCasePermutation.java) (Easy)
70-
72+
- [Zuma Game](problems/src/backtracking/ZumaGame.java) (Hard)
73+
- [Matchsticks to Square](problems/src/backtracking/MatchsticksToSquare.java) (Medium)
7174

7275
#### [Binary Search](problems/src/binary_search)
7376

@@ -82,13 +85,15 @@ My accepted leetcode solutions to some of the common interview problems.
8285
- [Target Sum](problems/src/binary_search/TargetSum.java) (Medium)
8386
- [H-Index II](problems/src/binary_search/HIndexII.java) (Medium)
8487
- [Swim in Rising Water](problems/src/binary_search/SwimInRisingWater.java) (Hard)
88+
- [Time Based Key-Value Store](problems/src/binary_search/TimeBasedKeyValuePair.java) (Medium)
8589

8690
#### [Bit Manipulation](problems/src/bit_manipulation)
8791

8892
- [Gray Code](problems/src/bit_manipulation/GrayCode.java) (Medium)
8993
- [Hamming Distance](problems/src/bit_manipulation/HammingDistance.java) (Easy)
9094
- [Total Hamming Distance](problems/src/bit_manipulation/TotalHammingDistance.java) (Medium)
9195
- [Divide Two Integers](problems/src/bit_manipulation/DivideTwoIntegers.java) (Medium)
96+
- [Binary Number with Alternating Bits](problems/src/bit_manipulation/BinaryNumberWithAlternatingBits.java) (Easy)
9297

9398
#### [Breadth First Search](problems/src/breadth_first_search)
9499

@@ -123,6 +128,7 @@ My accepted leetcode solutions to some of the common interview problems.
123128
- [Robot Room Cleaner](problems/src/depth_first_search/RobotRoomCleaner.java) (Hard)
124129
- [Cracking the Safe](problems/src/depth_first_search/CrackingTheSafe.java) (Hard)
125130
- [All Paths From Source to Target](problems/src/depth_first_search/AllPathsFromSourceToTarget.java) (Medium)
131+
- [Max Area of Island](problems/src/depth_first_search/MaxAreaOfIsland.java) (Medium)
126132

127133
#### [Design](problems/src/design)
128134

@@ -148,6 +154,7 @@ My accepted leetcode solutions to some of the common interview problems.
148154
- [Kth Largest Element In a Array](problems/src/divide_and_conquer/KthLargestElementInAnArray.java) (Medium)
149155
- [Reverse Pairs](problems/src/divide_and_conquer/ReversePairs.java) (Hard)
150156
- [Search in a 2D Matrix](problems/src/divide_and_conquer/SearchA2DMatrix.java) (Medium)
157+
- [24 Game](problems/src/divide_and_conquer/TwentyFourGame.java) (Hard)
151158

152159
#### [Dynamic Programming](problems/src/dynamic_programming)
153160

@@ -197,6 +204,9 @@ My accepted leetcode solutions to some of the common interview problems.
197204
- [Odd Even Jump](problems/src/dynamic_programming/OddEvenJump.java) (Hard)
198205
- [Profitable Schemes](problems/src/dynamic_programming/ProfitableSchemes.java) (Hard)
199206
- [Maximum Vacation Days](problems/src/dynamic_programming/MaximumVacationDays.java) (Hard)
207+
- [Russian Doll Envelopes](problems/src/dynamic_programming/RussianDollEnvelopes.java) (Hard)
208+
- [Student Attendance Record II](problems/src/dynamic_programming/StudentAttendanceRecordII.java) (Hard)
209+
- [Out of Boundary Paths](problems/src/dynamic_programming/OutOfBoundaryPaths.java) (Medium)
200210

201211
#### [Greedy](problems/src/greedy)
202212

@@ -212,6 +222,8 @@ My accepted leetcode solutions to some of the common interview problems.
212222
- [Lemonade Change](problems/src/greedy/LemonadeChange.java) (Easy)
213223
- [Score After Flipping Matrix](problems/src/greedy/ScoreAfterFlippingMatrix.java) (Medium)
214224
- [IPO](problems/src/greedy/IPO.java) (Hard)
225+
- [String Without AAA or BBB](problems/src/greedy/StringWithout3A3B.java) (Medium)
226+
- [Boats to Save People](problems/src/greedy/BoatsToSavePeople.java) (Medium)
215227

216228
#### [Hashing](problems/src/hashing)
217229

@@ -240,6 +252,7 @@ My accepted leetcode solutions to some of the common interview problems.
240252
- [Candy](problems/src/heap/Candy.java) (Hard)
241253
- [Smallest Rotation with Highest Score](problems/src/heap/SmallestRotationWithHighestScore.java) (Hard)
242254
- [Maximum Frequency Stack](problems/src/heap/FreqStack.java) (Hard)
255+
- [Reachable Nodes In Subdivided Graph](problems/src/heap/ReachableNodesInSubdividedGraph.java) (Hard)
243256

244257
#### [Linked List](problems/src/linked_list)
245258

@@ -270,6 +283,7 @@ My accepted leetcode solutions to some of the common interview problems.
270283
- [Reaching Points](problems/src/math/ReachingPoints.java) (Hard)
271284
- [Nth Magical Number](problems/src/math/NthMagicalNumber.java) (Hard)
272285
- [Squirrel Simulation](problems/src/math/SquirrelSimulation.java) (Medium)
286+
- [Projection Area of 3D Shapes](problems/src/math/ProjectionAreaOf3DShapes.java) (Easy)
273287

274288
#### [Reservoir Sampling](problems/src/reservoir_sampling)
275289

@@ -320,6 +334,10 @@ My accepted leetcode solutions to some of the common interview problems.
320334
- [Replace Words](problems/src/string/ReplaceWords.java) (Medium)
321335
- [Rotate String](problems/src/string/RotateString.java) (Easy)
322336
- [Keyboard Row](problems/src/string/KeyboardRow.java) (Easy)
337+
- [Student Attendance Record I](problems/src/string/StudentAttendanceRecordI.java) (Easy)
338+
- [Split Concatenated Strings](problems/src/string/SplitConcatenatedStrings.java) (Medium)
339+
- [Valid Word Square](problems/src/string/ValidWordSquare.java) (Easy)
340+
- [Reconstruct Original Digits from English](problems/src/string/ReconstructOriginalDigitsFromEnglish.java) (Medium)
323341

324342
#### [Tree](problems/src/tree)
325343

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 08/05/2019 Given a binary array, find the maximum number of
5+
* consecutive 1s in this array.
6+
*
7+
* <p>Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three
8+
* digits are consecutive 1s. The maximum number of consecutive 1s is 3. Note:
9+
*
10+
* <p>The input array will only contain 0 and 1. The length of input array is a positive integer and
11+
* will not exceed 10,000
12+
*/
13+
public class MaxConsecutiveOnes {
14+
public static void main(String[] args) {
15+
//
16+
}
17+
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+
}
33+
}
34+
return max;
35+
}
36+
37+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 08/05/2019 Given a binary array, find the maximum number of
5+
* consecutive 1s in this array if you can flip at most one 0.
6+
*
7+
* <p>Example 1: Input: [1,0,1,1,0] Output: 4 Explanation: Flip the first zero will get the the
8+
* maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4.
9+
* Note:
10+
*
11+
* <p>The input array will only contain 0 and 1. The length of input array is a positive integer and
12+
* will not exceed 10,000 Follow up: What if the input numbers come in one by one as an infinite
13+
* stream? In other words, you can't store all numbers coming from the stream as it's too large to
14+
* hold in memory. Could you solve it efficiently?
15+
*
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
20+
*/
21+
public class MaxConsecutiveOnesII {
22+
public static void main(String[] args) {
23+
//
24+
}
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+
}
45+
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+
}
60+
}
61+
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+
}
71+
72+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package backtracking;
2+
import java.util.*;
3+
/**
4+
* Created by gouthamvidyapradhan on 25/05/2019 Remember the story of Little Match Girl? By now, you
5+
* know exactly what matchsticks the little match girl has, please find out a way you can make one
6+
* square by using up all those matchsticks. You should not break any stick, but you can link them
7+
* up, and each matchstick must be used exactly one time.
8+
*
9+
* <p>Your input will be several matchsticks the girl has, represented with their stick length. Your
10+
* output will either be true or false, to represent whether you could make one square using all the
11+
* matchsticks the little match girl has.
12+
*
13+
* <p>Example 1: Input: [1,1,2,2,2] Output: true
14+
*
15+
* <p>Explanation: You can form a square with length 2, one side of the square came two sticks with
16+
* length 1. Example 2: Input: [3,3,3,3,4] Output: false
17+
*
18+
* <p>Explanation: You cannot find a way to form a square with all the matchsticks. Note: The length
19+
* sum of the given matchsticks is in the range of 0 to 10^9. The length of the given matchstick
20+
* array will not exceed 15.
21+
*
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.
28+
*/
29+
public class MatchsticksToSquare {
30+
/**
31+
* Main method
32+
* @param args
33+
*/
34+
public static void main(String[] args) {
35+
int[] A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 6, 10, 10};
36+
System.out.println(new MatchsticksToSquare().makesquare(A));
37+
}
38+
39+
class Pair {
40+
int value, i;
41+
Pair(int value, int i){
42+
this.value = value;
43+
this.i = i;
44+
}
45+
}
46+
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);
60+
}
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+
}
85+
}
86+
}
87+
return false;
88+
}
89+
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+
}
95+
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);
110+
}
111+
}
112+
}

0 commit comments

Comments
(0)

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