Skip to content

Commit cb44265

Browse files
Solved Problems
1 parent 9a2e794 commit cb44265

18 files changed

+1245
-5
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ My accepted leetcode solutions to some of the common interview problems.
5050
- [Max Consecutive Ones](problems/src/array/MaxConsecutiveOnes.java) (Easy)
5151
- [Max Consecutive Ones II](problems/src/array/MaxConsecutiveOnesII.java) (Medium)
5252
- [Add to Array-Form of Integer](problems/src/array/AddToArrayFormOfInteger.java) (Easy)
53+
- [Find Pivot Index](problems/src/array/FindPivotIndex.java) (Easy)
54+
- [Largest Time for Given Digits](problems/src/array/LargestTimeForGivenDigits.java) (Easy)
55+
- [Minimum Time Difference](problems/src/array/MinimumTimeDifference.java) (Medium)
5356

5457
#### [Backtracking](problems/src/backtracking)
5558

@@ -87,6 +90,7 @@ My accepted leetcode solutions to some of the common interview problems.
8790
- [H-Index II](problems/src/binary_search/HIndexII.java) (Medium)
8891
- [Swim in Rising Water](problems/src/binary_search/SwimInRisingWater.java) (Hard)
8992
- [Time Based Key-Value Store](problems/src/binary_search/TimeBasedKeyValuePair.java) (Medium)
93+
- [Minimum Window Subsequence](problems/src/binary_search/MinimumWindowSubsequence.java) (Hard)
9094

9195
#### [Bit Manipulation](problems/src/bit_manipulation)
9296

@@ -131,6 +135,7 @@ My accepted leetcode solutions to some of the common interview problems.
131135
- [All Paths From Source to Target](problems/src/depth_first_search/AllPathsFromSourceToTarget.java) (Medium)
132136
- [Max Area of Island](problems/src/depth_first_search/MaxAreaOfIsland.java) (Medium)
133137
- [Satisfiability of Equality Equations](problems/src/depth_first_search/SatisfiabilityOfEquations.java) (Medium)
138+
- [Number of Enclaves](problems/src/depth_first_search/NumberOfEnclaves.java) (Medium)
134139

135140
#### [Design](problems/src/design)
136141

@@ -157,6 +162,7 @@ My accepted leetcode solutions to some of the common interview problems.
157162
- [Reverse Pairs](problems/src/divide_and_conquer/ReversePairs.java) (Hard)
158163
- [Search in a 2D Matrix](problems/src/divide_and_conquer/SearchA2DMatrix.java) (Medium)
159164
- [24 Game](problems/src/divide_and_conquer/TwentyFourGame.java) (Hard)
165+
- [Reverse Pairs II](problems/src/divide_and_conquer/ReversePairsII.java) (Hard)
160166

161167
#### [Dynamic Programming](problems/src/dynamic_programming)
162168

@@ -211,6 +217,7 @@ My accepted leetcode solutions to some of the common interview problems.
211217
- [Out of Boundary Paths](problems/src/dynamic_programming/OutOfBoundaryPaths.java) (Medium)
212218
- [Remove Boxes](problems/src/dynamic_programming/RemoveBoxes.java) (Hard)
213219
- [Stickers to Spell Word](problems/src/dynamic_programming/StickersToSpellWord.java) (Hard)
220+
- [Ones and Zeroes](problems/src/dynamic_programming/OnesAndZeroes.java) (Medium)
214221

215222
#### [Greedy](problems/src/greedy)
216223

@@ -247,6 +254,7 @@ My accepted leetcode solutions to some of the common interview problems.
247254
- [Substring with Concatenation of All Words](problems/src/hashing/SubstringConcatenationOfWords.java) (Hard)
248255
- [Distribute Candies](problems/src/hashing/DistributeCandies.java) (Easy)
249256
- [Groups of Special-Equivalent Strings](problems/src/hashing/GroupsOfSpecialEquivalentStrings.java) (Easy)
257+
- [Number of Atoms](problems/src/hashing/NumberOfAtoms.java) (Hard)
250258

251259
#### [Heap](problems/src/heap)
252260

@@ -271,6 +279,8 @@ My accepted leetcode solutions to some of the common interview problems.
271279
- [Reverse Nodes in k-Group](problems/src/linked_list/ReverseNodesKGroup.java) (Hard)
272280
- [Swap Nodes in Pairs](problems/src/linked_list/SwapNodesInPairs.java) (Medium)
273281
- [Middle of Linked List](problems/src/linked_list/MiddleOfLinkedList.java) (Easy)
282+
- [Split Linked List in Parts](problems/src/linked_list/SplitLinkedListInParts.java) (Medium)
283+
- [Next Greater Node In Linked List](problems/src/linked_list/NextGreaterNodeInLinkedList.java) (Medium)
274284

275285
#### [Math](problems/src/math)
276286

@@ -290,6 +300,7 @@ My accepted leetcode solutions to some of the common interview problems.
290300
- [Squirrel Simulation](problems/src/math/SquirrelSimulation.java) (Medium)
291301
- [Projection Area of 3D Shapes](problems/src/math/ProjectionAreaOf3DShapes.java) (Easy)
292302
- [Decoded String at Index](problems/src/math/DecodedStringAtIndex.java) (Medium)
303+
- [Base 7](problems/src/math/Base7.java) (Easy)
293304

294305
#### [Reservoir Sampling](problems/src/reservoir_sampling)
295306

@@ -345,6 +356,8 @@ My accepted leetcode solutions to some of the common interview problems.
345356
- [Valid Word Square](problems/src/string/ValidWordSquare.java) (Easy)
346357
- [Reconstruct Original Digits from English](problems/src/string/ReconstructOriginalDigitsFromEnglish.java) (Medium)
347358
- [Push Dominoes](problems/src/string/PushDominoes.java) (Medium)
359+
- [Validate IP Address](problems/src/string/ValidateIPAddress.java) (Medium)
360+
- [Reverse String II](problems/src/string/ReverseStringII.java) (Easy)
348361

349362
#### [Tree](problems/src/tree)
350363

@@ -380,7 +393,7 @@ My accepted leetcode solutions to some of the common interview problems.
380393
- [Average of Levels in Binary Tree](problems/src/tree/AverageOfLevelsInBinaryTree.java) (Easy)
381394
- [Convert Binary Search Tree to Sorted Doubly Linked List](problems/src/tree/BSTtoDoublyLinkedList.java) (Easy)
382395
- [Same Tree](problems/src/tree/SameTree.java) (Easy)
383-
- [Binary Tree Longest Consecutive Sequence II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
396+
- [Binary Tree Longest Consecutive SequencefindMinDifference II](problems/src/tree/BinaryTreeLongestConsecutiveSequenceII.java) (Medium)
384397
- [Minimum Absolute Difference in BST](problems/src/tree/MinimumAbsoluteDifferenceInBST.java) (Medium)
385398
- [Equal Tree Partition](problems/src/tree/EqualTreePartition.java) (Medium)
386399
- [Split BST](problems/src/tree/SplitBST.java) (Medium)
@@ -392,6 +405,10 @@ My accepted leetcode solutions to some of the common interview problems.
392405
- [Convert BST to Greater Tree](problems/src/tree/ConvertBSTToGreaterTree.java) (Easy)
393406
- [All Nodes Distance K in Binary Tree](problems/src/tree/AllNodesDistanceKInBinaryTree.java) (Medium)
394407
- [All Possible Full Binary Trees](problems/src/tree/AllPossibleFullBinaryTrees.java) (Medium)
408+
- [Flip Equivalent Binary Trees](problems/src/tree/FlipEquivalentBinaryTrees.java) (Medium)
409+
- [Construct Binary Tree from String](problems/src/tree/ConstructBinaryTreefromString.java) (Medium)
410+
- [Find Largest Value in Each Tree Row](problems/src/tree/FindLargestValueInEachTreeRow.java) (Medium)
411+
- [Find Bottom Left Tree Value](problems/src/tree/FindBottomLeftTreeValue.java) (Medium)
395412

396413
#### [Two Pointers](problems/src/two_pointers)
397414

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package array;
2+
import java.util.*;
3+
4+
/**
5+
* Created by gouthamvidyapradhan on 06/08/2019 Given an array of integers nums, write a method that
6+
* returns the "pivot" index of this array.
7+
*
8+
* <p>We define the pivot index as the index where the sum of the numbers to the left of the index
9+
* is equal to the sum of the numbers to the right of the index.
10+
*
11+
* <p>If no such index exists, we should return -1. If there are multiple pivot indexes, you should
12+
* return the left-most pivot index.
13+
*
14+
* <p>Example 1:
15+
*
16+
* <p>Input: nums = [1, 7, 3, 6, 5, 6] Output: 3 Explanation: The sum of the numbers to the left of
17+
* index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3. Also, 3 is the
18+
* first index where this occurs.
19+
*
20+
* <p>Example 2:
21+
*
22+
* <p>Input: nums = [1, 2, 3] Output: -1 Explanation: There is no index that satisfies the
23+
* conditions in the problem statement.
24+
*
25+
* <p>Note:
26+
*
27+
* <p>The length of nums will be in the range [0, 10000]. Each element nums[i] will be an integer in
28+
* the range [-1000, 1000].
29+
*
30+
* Solution: O(N) maintain a prefix and posfix sum array and then use this to arrive at the answer.
31+
*/
32+
public class FindPivotIndex {
33+
public static void main(String[] args) {
34+
}
35+
36+
public int pivotIndex(int[] nums) {
37+
if(nums.length == 1) return 0;
38+
int[] left = new int[nums.length];
39+
int[] right = new int[nums.length];
40+
left[0] = nums[0];
41+
for(int i = 1; i < nums.length; i ++){
42+
left[i] = left[i - 1] + nums[i];
43+
}
44+
right[nums.length - 1] = nums[nums.length - 1];
45+
for(int i = nums.length - 2; i >= 0; i --){
46+
right[i] = right[i + 1] + nums[i];
47+
}
48+
for(int i = 0; i < nums.length; i ++){
49+
int l, r;
50+
if(i == 0){
51+
l = 0;
52+
} else {
53+
l = left[i - 1];
54+
}
55+
56+
if(i == nums.length - 1){
57+
r = 0;
58+
} else {
59+
r = right[i + 1];
60+
}
61+
if(l == r) return i;
62+
}
63+
return -1;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package array;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 06/08/2019 Given an array of 4 digits, return the largest 24
5+
* hour time that can be made.
6+
*
7+
* <p>The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is
8+
* larger if more time has elapsed since midnight.
9+
*
10+
* <p>Return the answer as a string of length 5. If no valid time can be made, return an empty
11+
* string.
12+
*
13+
* <p>Example 1:
14+
*
15+
* <p>Input: [1,2,3,4] Output: "23:41" Example 2:
16+
*
17+
* <p>Input: [5,5,5,5] Output: ""
18+
*
19+
* <p>Note:
20+
*
21+
* <p>A.length == 4 0 <= A[i] <= 9
22+
* Solution O(N ^ 4) Check all combinations of time possible and return the maximum possible as the answer.
23+
*/
24+
public class LargestTimeForGivenDigits {
25+
public static void main(String[] args) {
26+
int[] A = {2, 0, 6, 6};
27+
System.out.println(new LargestTimeForGivenDigits().largestTimeFromDigits(A));
28+
}
29+
30+
public String largestTimeFromDigits(int[] A) {
31+
int max = -1;
32+
String result = "";
33+
for(int i = 0; i < A.length; i ++){
34+
if(A[i] > 2) continue;
35+
for(int j = 0; j < A.length; j ++){
36+
if(j == i) continue;
37+
if(A[i] == 2 && A[j] > 3) continue;
38+
for(int k = 0; k < A.length; k ++){
39+
if(k == i || k == j) continue;
40+
if(A[k] > 5) continue;
41+
for(int l = 0; l < A.length; l ++){
42+
if(l == i || l == j || l == k) continue;
43+
int value = ((A[i] * 10 + A[j]) * 60) + A[k] * 10 + A[l];
44+
if(value > max){
45+
max = value;
46+
result = A[i] + "" + A[j] + ":" + A[k] + "" + A[l];
47+
}
48+
}
49+
}
50+
}
51+
}
52+
return result;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package array;
2+
import java.util.*;
3+
import java.util.stream.Collectors;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 30/07/2019 Given a list of 24-hour clock time points in
7+
* "Hour:Minutes" format, find the minimum minutes difference between any two time points in the
8+
* list. Example 1: Input: ["23:59","00:00"] Output: 1 Note: The number of time points in the given
9+
* list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to
10+
* 23:59.
11+
*
12+
* Solution: O(N log N) convert each time value of the form hh:mm to minutes and sort the array. For every pair (i,
13+
* j) where j = i + 1 (also for the case where i = 0 and j = N - 1) check the minute difference and return the
14+
* minimum time difference as the answer.
15+
*/
16+
public class MinimumTimeDifference {
17+
public static void main(String[] args) {
18+
List<String> list = Arrays.asList("23:59","00:00");
19+
System.out.println(new MinimumTimeDifference().findMinDifference(list));
20+
}
21+
22+
public int findMinDifference(List<String> timePoints) {
23+
List<Integer> timeInMinutes = timePoints.stream().map(t -> {
24+
String[] strings = t.split(":");
25+
return Integer.parseInt(strings[0]) * 60 + Integer.parseInt(strings[1]);
26+
}).sorted(Integer::compareTo).collect(Collectors.toList());
27+
int min = Integer.MAX_VALUE;
28+
for(int i = 1, l = timeInMinutes.size(); i < l; i ++){
29+
int prev = timeInMinutes.get(i - 1);
30+
int curr = timeInMinutes.get(i);
31+
min = Math.min(min, curr - prev);
32+
min = Math.min(min, ((24 * 60) - curr) + prev);
33+
}
34+
int prev = timeInMinutes.get(0);
35+
int curr = timeInMinutes.get(timeInMinutes.size() - 1);
36+
min = Math.min(min, curr - prev);
37+
min = Math.min(min, ((24 * 60) - curr) + prev);
38+
return min;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package binary_search;
2+
import java.util.*;
3+
/**
4+
* Created by gouthamvidyapradhan on 06/08/2019 Given strings S and T, find the minimum (contiguous)
5+
* substring W of S, so that T is a subsequence of W.
6+
*
7+
* <p>If there is no such window in S that covers all characters in T, return the empty string "".
8+
* If there are multiple such minimum-length windows, return the one with the left-most starting
9+
* index.
10+
*
11+
* <p>Example 1:
12+
*
13+
* <p>Input: S = "abcdebdde", T = "bde" Output: "bcde" Explanation: "bcde" is the answer because it
14+
* occurs before "bdde" which has the same length. "deb" is not a smaller window because the
15+
* elements of T in the window must occur in order.
16+
*
17+
* <p>Note:
18+
*
19+
* <p>All the strings in the input will only contain lowercase letters. The length of S will be in
20+
* the range [1, 20000]. The length of T will be in the range [1, 100].
21+
*
22+
* Solution O(S x T x log S) General idea is to first find the left-most left (l) and right (r) index where r - l is
23+
* minimum and the minimum window contains the sub-sequence and iteratively check the next left-most indices and
24+
* continue for the entire string S. A naive implementation would result in O(S ^ 2)
25+
* therefore to speed up we have to maintain a hashtable of character as key and all its index of occurrence in a
26+
* sorted list. Now, since this list is sorted we can easily find the next left-most by binarySearch or even better
27+
* by using a TreeSet higher or ceil function.
28+
*/
29+
public class MinimumWindowSubsequence {
30+
public static void main(String[] args) {
31+
System.out.println(new MinimumWindowSubsequence().minWindow("abcdebdde", "x"));
32+
}
33+
34+
public String minWindow(String S, String T) {
35+
if(T.isEmpty() || S.isEmpty()) return "";
36+
Map<Character, TreeSet<Integer>> charMap = new HashMap<>();
37+
for(int i = 0, l = S.length(); i < l; i ++){
38+
char c = S.charAt(i);
39+
charMap.putIfAbsent(c, new TreeSet<>());
40+
charMap.get(c).add(i);
41+
}
42+
int min = Integer.MAX_VALUE;
43+
int start = -1, end;
44+
int ansStart = -1, ansEnd = -1;
45+
boolean finished = false;
46+
while(true){
47+
int index = start;
48+
end = -1;
49+
for(int i = 0, l = T.length(); i < l; i ++){
50+
char c = T.charAt(i);
51+
if(!charMap.containsKey(c)){
52+
return "";
53+
} else{
54+
TreeSet<Integer> indicies = charMap.get(c);
55+
Integer found = indicies.higher(index);
56+
if(found == null){
57+
finished = true;
58+
break;
59+
} else{
60+
index = found;
61+
if(i == 0){
62+
start = index;
63+
} if(i == l - 1){
64+
end = index;
65+
}
66+
}
67+
}
68+
}
69+
if(start != -1 && end != -1){
70+
if((end - start) < min){
71+
min = end - start;
72+
ansStart = start;
73+
ansEnd = end;
74+
}
75+
}
76+
if(finished) return ansStart == -1 ? "" : S.substring(ansStart, ansEnd + 1);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)