Skip to content

Commit b0e7b60

Browse files
Solved Problems and code format
1 parent cb44265 commit b0e7b60

19 files changed

+704
-606
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ My accepted leetcode solutions to some of the common interview problems.
5353
- [Find Pivot Index](problems/src/array/FindPivotIndex.java) (Easy)
5454
- [Largest Time for Given Digits](problems/src/array/LargestTimeForGivenDigits.java) (Easy)
5555
- [Minimum Time Difference](problems/src/array/MinimumTimeDifference.java) (Medium)
56+
- [Reveal Cards In Increasing Order](problems/src/array/RevealCardsInIncreasingOrder.java) (Medium)
5657

5758
#### [Backtracking](problems/src/backtracking)
5859

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package array;
2+
23
import java.util.*;
34

45
/**
@@ -27,39 +28,39 @@
2728
* <p>The length of nums will be in the range [0, 10000]. Each element nums[i] will be an integer in
2829
* the range [-1000, 1000].
2930
*
30-
* Solution: O(N) maintain a prefix and posfix sum array and then use this to arrive at the answer.
31+
* <p>Solution: O(N) maintain a prefix and posfix sum array and then use this to arrive at the
32+
* answer.
3133
*/
3234
public class FindPivotIndex {
33-
public static void main(String[] args) {
34-
}
35+
public static void main(String[] args) {}
3536

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-
}
37+
public int pivotIndex(int[] nums) {
38+
if (nums.length == 1) return 0;
39+
int[] left = new int[nums.length];
40+
int[] right = new int[nums.length];
41+
left[0] = nums[0];
42+
for (int i = 1; i < nums.length; i++) {
43+
left[i] = left[i - 1] + nums[i];
44+
}
45+
right[nums.length - 1] = nums[nums.length - 1];
46+
for (int i = nums.length - 2; i >= 0; i--) {
47+
right[i] = right[i + 1] + nums[i];
48+
}
49+
for (int i = 0; i < nums.length; i++) {
50+
int l, r;
51+
if (i == 0) {
52+
l = 0;
53+
} else {
54+
l = left[i - 1];
55+
}
5556

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;
57+
if (i == nums.length - 1) {
58+
r = 0;
59+
} else {
60+
r = right[i + 1];
61+
}
62+
if (l == r) return i;
6463
}
64+
return -1;
65+
}
6566
}

problems/src/array/LargestTimeForGivenDigits.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*
1919
* <p>Note:
2020
*
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.
21+
* <p>A.length == 4 0 <= A[i] <= 9 Solution O(N ^ 4) Check all combinations of time possible and
22+
* return the maximum possible as the answer.
2323
*/
2424
public class LargestTimeForGivenDigits {
2525
public static void main(String[] args) {
@@ -30,18 +30,18 @@ public static void main(String[] args) {
3030
public String largestTimeFromDigits(int[] A) {
3131
int max = -1;
3232
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;
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;
4343
int value = ((A[i] * 10 + A[j]) * 60) + A[k] * 10 + A[l];
44-
if(value > max){
44+
if (value > max) {
4545
max = value;
4646
result = A[i] + "" + A[j] + ":" + A[k] + "" + A[l];
4747
}
Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package array;
2+
23
import java.util.*;
34
import java.util.stream.Collectors;
45

@@ -9,32 +10,38 @@
910
* list is at least 2 and won't exceed 20000. The input time is legal and ranges from 00:00 to
1011
* 23:59.
1112
*
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.
13+
* <p>Solution: O(N log N) convert each time value of the form hh:mm to minutes and sort the array.
14+
* For every pair (i, j) where j = i + 1 (also for the case where i = 0 and j = N - 1) check the
15+
* minute difference and return the minimum time difference as the answer.
1516
*/
1617
public class MinimumTimeDifference {
1718
public static void main(String[] args) {
18-
List<String> list = Arrays.asList("23:59","00:00");
19+
List<String> list = Arrays.asList("23:59", "00:00");
1920
System.out.println(new MinimumTimeDifference().findMinDifference(list));
2021
}
2122

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;
23+
public int findMinDifference(List<String> timePoints) {
24+
List<Integer> timeInMinutes =
25+
timePoints
26+
.stream()
27+
.map(
28+
t -> {
29+
String[] strings = t.split(":");
30+
return Integer.parseInt(strings[0]) * 60 + Integer.parseInt(strings[1]);
31+
})
32+
.sorted(Integer::compareTo)
33+
.collect(Collectors.toList());
34+
int min = Integer.MAX_VALUE;
35+
for (int i = 1, l = timeInMinutes.size(); i < l; i++) {
36+
int prev = timeInMinutes.get(i - 1);
37+
int curr = timeInMinutes.get(i);
38+
min = Math.min(min, curr - prev);
39+
min = Math.min(min, ((24 * 60) - curr) + prev);
3940
}
41+
int prev = timeInMinutes.get(0);
42+
int curr = timeInMinutes.get(timeInMinutes.size() - 1);
43+
min = Math.min(min, curr - prev);
44+
min = Math.min(min, ((24 * 60) - curr) + prev);
45+
return min;
46+
}
4047
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package array;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Arrays;
5+
6+
/**
7+
* Created by gouthamvidyapradhan on 12/08/2019 In a deck of cards, every card has a unique integer.
8+
* You can order the deck in any order you want.
9+
*
10+
* <p>Initially, all the cards start face down (unrevealed) in one deck.
11+
*
12+
* <p>Now, you do the following steps repeatedly, until all cards are revealed:
13+
*
14+
* <p>Take the top card of the deck, reveal it, and take it out of the deck. If there are still
15+
* cards in the deck, put the next top card of the deck at the bottom of the deck. If there are
16+
* still unrevealed cards, go back to step 1. Otherwise, stop. Return an ordering of the deck that
17+
* would reveal the cards in increasing order.
18+
*
19+
* <p>The first entry in the answer is considered to be the top of the deck.
20+
*
21+
* <p>Example 1:
22+
*
23+
* <p>Input: [17,13,11,2,3,5,7] Output: [2,13,3,11,5,17,7] Explanation: We get the deck in the order
24+
* [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. After reordering, the deck starts
25+
* as [2,13,3,11,5,17,7], where 2 is the top of the deck. We reveal 2, and move 13 to the bottom.
26+
* The deck is now [3,11,5,17,7,13]. We reveal 3, and move 11 to the bottom. The deck is now
27+
* [5,17,7,13,11]. We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. We reveal
28+
* 7, and move 13 to the bottom. The deck is now [11,17,13]. We reveal 11, and move 17 to the
29+
* bottom. The deck is now [13,17]. We reveal 13, and move 17 to the bottom. The deck is now [17].
30+
* We reveal 17. Since all the cards revealed are in increasing order, the answer is correct.
31+
*
32+
* <p>Note:
33+
*
34+
* <p>1 <= A.length <= 1000 1 <= A[i] <= 10^6 A[i] != A[j] for all i != j
35+
*
36+
* <p>Solution: O(N) General idea is to start from the last element and build the array of element
37+
* in the backwards order. Use a doubly-ended queue which allows you to poll from either end of a
38+
* queue.
39+
*/
40+
public class RevealCardsInIncreasingOrder {
41+
public static void main(String[] args) {
42+
int[] A = {17, 13, 11, 2, 3, 5, 7};
43+
int[] R = new RevealCardsInIncreasingOrder().deckRevealedIncreasing(A);
44+
}
45+
46+
public int[] deckRevealedIncreasing(int[] deck) {
47+
Arrays.sort(deck);
48+
ArrayDeque<Integer> queue = new ArrayDeque<>();
49+
for (int i = deck.length - 1; i >= 0; i--) {
50+
queue.offer(deck[i]);
51+
if (i == 0) break;
52+
int temp = queue.pollFirst();
53+
queue.offer(temp);
54+
}
55+
int[] answer = new int[deck.length];
56+
int i = 0;
57+
while (!queue.isEmpty()) {
58+
answer[i++] = queue.pollLast();
59+
}
60+
return answer;
61+
}
62+
}
Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package binary_search;
2+
23
import java.util.*;
4+
35
/**
46
* Created by gouthamvidyapradhan on 06/08/2019 Given strings S and T, find the minimum (contiguous)
57
* substring W of S, so that T is a subsequence of W.
@@ -19,61 +21,63 @@
1921
* <p>All the strings in the input will only contain lowercase letters. The length of S will be in
2022
* the range [1, 20000]. The length of T will be in the range [1, 100].
2123
*
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.
24+
* <p>Solution O(S x T x log S) General idea is to first find the left-most left (l) and right (r)
25+
* index where r - l is minimum and the minimum window contains the sub-sequence and iteratively
26+
* check the next left-most indices and continue for the entire string S. A naive implementation
27+
* would result in O(S ^ 2) therefore to speed up we have to maintain a hashtable of character as
28+
* key and all its index of occurrence in a sorted list. Now, since this list is sorted we can
29+
* easily find the next left-most by binarySearch or even better by using a TreeSet higher or ceil
30+
* function.
2831
*/
2932
public class MinimumWindowSubsequence {
3033
public static void main(String[] args) {
3134
System.out.println(new MinimumWindowSubsequence().minWindow("abcdebdde", "x"));
3235
}
3336

3437
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-
}
38+
if (T.isEmpty() || S.isEmpty()) return "";
39+
Map<Character, TreeSet<Integer>> charMap = new HashMap<>();
40+
for (int i = 0, l = S.length(); i < l; i++) {
41+
char c = S.charAt(i);
42+
charMap.putIfAbsent(c, new TreeSet<>());
43+
charMap.get(c).add(i);
44+
}
45+
int min = Integer.MAX_VALUE;
46+
int start = -1, end;
47+
int ansStart = -1, ansEnd = -1;
48+
boolean finished = false;
49+
while (true) {
50+
int index = start;
51+
end = -1;
52+
for (int i = 0, l = T.length(); i < l; i++) {
53+
char c = T.charAt(i);
54+
if (!charMap.containsKey(c)) {
55+
return "";
56+
} else {
57+
TreeSet<Integer> indicies = charMap.get(c);
58+
Integer found = indicies.higher(index);
59+
if (found == null) {
60+
finished = true;
61+
break;
62+
} else {
63+
index = found;
64+
if (i == 0) {
65+
start = index;
66+
}
67+
if (i == l - 1) {
68+
end = index;
69+
}
70+
}
71+
}
72+
}
73+
if (start != -1 && end != -1) {
74+
if ((end - start) < min) {
75+
min = end - start;
76+
ansStart = start;
77+
ansEnd = end;
78+
}
79+
}
80+
if (finished) return ansStart == -1 ? "" : S.substring(ansStart, ansEnd + 1);
81+
}
7882
}
7983
}

0 commit comments

Comments
 (0)