Skip to content

Commit 47e3fee

Browse files
Code reformat
1 parent a273cc9 commit 47e3fee

File tree

157 files changed

+4046
-4473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+4046
-4473
lines changed

problems/src/array/BattleshipsInABoard.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,52 @@
33
/**
44
* Created by gouthamvidyapradhan on 12/08/2017.
55
* Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:
6-
7-
You receive a valid board, made of only battleships or empty slots.
8-
Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
9-
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
10-
Example:
11-
X..X
12-
...X
13-
...X
14-
In the above board there are 2 battleships.
15-
Invalid Example:
16-
...X
17-
XXXX
18-
...X
19-
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
20-
21-
Follow up:
22-
Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
23-
24-
Solution:
25-
The below solution works in one pass using only O(1) memory.
26-
Iterate through each cell and add one to count if and only if the current cell equals 'X' and its adjacent upper and
27-
left cell does not contain 'X'
6+
* <p>
7+
* You receive a valid board, made of only battleships or empty slots.
8+
* Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
9+
* At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
10+
* Example:
11+
* X..X
12+
* ...X
13+
* ...X
14+
* In the above board there are 2 battleships.
15+
* Invalid Example:
16+
* ...X
17+
* XXXX
18+
* ...X
19+
* This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
20+
* <p>
21+
* Follow up:
22+
* Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
23+
* <p>
24+
* Solution:
25+
* The below solution works in one pass using only O(1) memory.
26+
* Iterate through each cell and add one to count if and only if the current cell equals 'X' and its adjacent upper and
27+
* left cell does not contain 'X'
2828
*/
2929
public class BattleshipsInABoard {
3030
/**
3131
* Main method
32+
*
3233
* @param args
3334
* @throws Exception
3435
*/
35-
public static void main(String[] args) throws Exception{
36+
public static void main(String[] args) throws Exception {
3637
char[][] board = {{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}};
3738
System.out.println(new BattleshipsInABoard().countBattleships(board));
3839
}
3940

4041
public int countBattleships(char[][] board) {
4142
int count = 0;
42-
for(int i = 0; i < board.length; i ++){
43-
for(int j = 0; j < board[0].length; j ++){
44-
if(board[i][j] == 'X'){
45-
if(i - 1 >= 0){ //check for the boundary condition
46-
if(board[i - 1][j] == 'X')
43+
for (int i = 0; i < board.length; i++) {
44+
for (int j = 0; j < board[0].length; j++) {
45+
if (board[i][j] == 'X') {
46+
if (i - 1 >= 0) { //check for the boundary condition
47+
if (board[i - 1][j] == 'X')
4748
continue;
4849
}
49-
if(j - 1 >= 0){
50-
if(board[i][j - 1] == 'X'){
50+
if (j - 1 >= 0) {
51+
if (board[i][j - 1] == 'X') {
5152
continue;
5253
}
5354
}

problems/src/array/CanPlaceFlowers.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,47 @@
33
/**
44
* Created by gouthamvidyapradhan on 10/06/2017.
55
* Accepted
6-
*
7-
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
8-
9-
Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
10-
11-
Example 1:
12-
Input: flowerbed = [1,0,0,0,1], n = 1
13-
Output: True
14-
Example 2:
15-
Input: flowerbed = [1,0,0,0,1], n = 2
16-
Output: False
17-
Note:
18-
The input array won't violate no-adjacent-flowers rule.
19-
The input array size is in the range of [1, 20000].
20-
n is a non-negative integer which won't exceed the input array size.
6+
* <p>
7+
* Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.
8+
* <p>
9+
* Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.
10+
* <p>
11+
* Example 1:
12+
* Input: flowerbed = [1,0,0,0,1], n = 1
13+
* Output: True
14+
* Example 2:
15+
* Input: flowerbed = [1,0,0,0,1], n = 2
16+
* Output: False
17+
* Note:
18+
* The input array won't violate no-adjacent-flowers rule.
19+
* The input array size is in the range of [1, 20000].
20+
* n is a non-negative integer which won't exceed the input array size.
2121
*/
22-
public class CanPlaceFlowers
23-
{
22+
public class CanPlaceFlowers {
2423
/**
2524
* Main method
25+
*
2626
* @param args
2727
* @throws Exception
2828
*/
29-
public static void main(String[] args) throws Exception
30-
{
31-
int[] n = {1,0,0,0,1};
29+
public static void main(String[] args) throws Exception {
30+
int[] n = {1, 0, 0, 0, 1};
3231
System.out.println(new CanPlaceFlowers().canPlaceFlowers(n, 1));
3332
}
3433

3534
public boolean canPlaceFlowers(int[] flowerbed, int n) {
3635

3736
int[] T = new int[flowerbed.length + 4];
38-
for(int i = 0, j = 2; i < flowerbed.length; i ++)
37+
for (int i = 0, j = 2; i < flowerbed.length; i++)
3938
T[j++] = flowerbed[i];
4039
T[0] = 1;
4140
T[T.length - 1] = 1;
4241
int total = 0, count = 0;
43-
for(int i = 1; i < T.length; i ++) {
44-
if(T[i] == 0)
42+
for (int i = 1; i < T.length; i++) {
43+
if (T[i] == 0)
4544
count++;
4645
else {
47-
if((count % 2) == 0)
46+
if ((count % 2) == 0)
4847
total += ((count / 2) - 1);
4948
else
5049
total += (count / 2);

problems/src/array/FirstMissingPositive.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@
33
/**
44
* Created by gouthamvidyapradhan on 24/06/2017.
55
* Given an unsorted integer array, find the first missing positive integer.
6-
7-
For example,
8-
Given [1,2,0] return 3,
9-
and [3,4,-1,1] return 2.
10-
11-
Your algorithm should run in O(n) time and uses constant space.
6+
* <p>
7+
* For example,
8+
* Given [1,2,0] return 3,
9+
* and [3,4,-1,1] return 2.
10+
* <p>
11+
* Your algorithm should run in O(n) time and uses constant space.
1212
*/
1313
public class FirstMissingPositive {
1414
private int L;
15-
public static void main(String[] args) throws Exception{
15+
16+
public static void main(String[] args) throws Exception {
1617
int[] nums = {1, 3, 5, 9};
1718
System.out.println(new FirstMissingPositive().firstMissingPositive(nums));
1819
}
1920

2021
public int firstMissingPositive(int[] nums) {
2122
L = nums.length;
22-
for(int i = 0; i < L; i ++){
23-
if(nums[i] > 0 && nums[i] <= L && nums[i] != i + 1){
23+
for (int i = 0; i < L; i++) {
24+
if (nums[i] > 0 && nums[i] <= L && nums[i] != i + 1) {
2425
int v = nums[i];
2526
nums[i] = -1;
2627
replace(v, nums);
2728
}
2829
}
2930

30-
for(int i = 0; i < L; i ++){
31-
if(nums[i] != i + 1)
31+
for (int i = 0; i < L; i++) {
32+
if (nums[i] != i + 1)
3233
return i + 1;
3334
}
3435

3536
return L + 1;
3637
}
3738

38-
private void replace(int i, int[] nums){
39-
if(i > 0 && i <= L && i != nums[i - 1]){
39+
private void replace(int i, int[] nums) {
40+
if (i > 0 && i <= L && i != nums[i - 1]) {
4041
int v = nums[i - 1];
4142
nums[i - 1] = i;
4243
replace(v, nums);

problems/src/array/MaxProductOfThreeNumbers.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
/**
66
* Created by gouthamvidyapradhan on 27/06/2017.
77
* Given an integer array, find three numbers whose product is maximum and output the maximum product.
8-
9-
Example 1:
10-
Input: [1,2,3]
11-
Output: 6
12-
Example 2:
13-
Input: [1,2,3,4]
14-
Output: 24
15-
Note:
16-
The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
17-
Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
8+
* <p>
9+
* Example 1:
10+
* Input: [1,2,3]
11+
* Output: 6
12+
* Example 2:
13+
* Input: [1,2,3,4]
14+
* Output: 24
15+
* Note:
16+
* The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
17+
* Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
1818
*/
1919
public class MaxProductOfThreeNumbers {
2020
public static void main(String[] args) {
21-
int[] A = {1,2,3};
21+
int[] A = {1, 2, 3};
2222
System.out.println(new MaxProductOfThreeNumbers().maximumProduct(A));
2323
}
2424

problems/src/array/MergeIntervals.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
package array;
22

3-
import java.util.*;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
47

58
/**
69
* Created by gouthamvidyapradhan on 13/06/2017.
710
* Given a collection of intervals, merge all overlapping intervals.
8-
9-
For example,
10-
Given [1,3],[2,6],[8,10],[15,18],
11-
return [1,6],[8,10],[15,18].
12-
13-
Solution: O(N log N) where N is the number of intervals
14-
1. Sort the intervals based on start index
15-
2. Mark the first interval as the current interval
16-
3. For every ith interval starting 1 -> N, if the ith interval overlaps with the current interval then create a new
17-
current interval. Else, add the current interval to result set and begin a new current interval.
18-
11+
* <p>
12+
* For example,
13+
* Given [1,3],[2,6],[8,10],[15,18],
14+
* return [1,6],[8,10],[15,18].
15+
* <p>
16+
* Solution: O(N log N) where N is the number of intervals
17+
* 1. Sort the intervals based on start index
18+
* 2. Mark the first interval as the current interval
19+
* 3. For every ith interval starting 1 -> N, if the ith interval overlaps with the current interval then create a new
20+
* current interval. Else, add the current interval to result set and begin a new current interval.
1921
*/
20-
public class MergeIntervals
21-
{
22+
public class MergeIntervals {
2223
public static class Interval {
2324
int start;
2425
int end;
25-
Interval() { start = 0; end = 0; }
26-
Interval(int s, int e) { start = s; end = e; }
26+
27+
Interval() {
28+
start = 0;
29+
end = 0;
30+
}
31+
32+
Interval(int s, int e) {
33+
start = s;
34+
end = e;
35+
}
2736
}
2837

29-
public static void main(String[] args) throws Exception
30-
{
38+
public static void main(String[] args) throws Exception {
3139
Interval i1 = new Interval(1, 2);
3240
Interval i2 = new Interval(3, 4);
3341
Interval i3 = new Interval(5, 6);
@@ -37,16 +45,15 @@ public static void main(String[] args) throws Exception
3745
}
3846

3947
public List<Interval> merge(List<Interval> intervals) {
40-
if(intervals.isEmpty()) return new ArrayList<>();
48+
if (intervals.isEmpty()) return new ArrayList<>();
4149
Collections.sort(intervals, (o1, o2) -> Integer.compare(o1.start, o2.start));
4250
List<Interval> result = new ArrayList<>();
4351
Interval curr = intervals.get(0);
44-
for(int i = 1, l = intervals.size(); i < l; i ++) {
52+
for (int i = 1, l = intervals.size(); i < l; i++) {
4553
Interval I = intervals.get(i);
46-
if(I.start >= curr.start && I.start <= curr.end) { //check if the new interval overlaps with the current
54+
if (I.start >= curr.start && I.start <= curr.end) { //check if the new interval overlaps with the current
4755
curr.end = curr.end > I.end ? curr.end : I.end;
48-
}
49-
else {
56+
} else {
5057
result.add(curr);
5158
curr = I;
5259
}

problems/src/array/MergeSortedArray.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
/**
44
* Created by gouthamvidyapradhan on 29/07/2017.
55
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
6-
7-
Note:
8-
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
6+
* <p>
7+
* Note:
8+
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
99
*/
1010
public class MergeSortedArray {
11-
public static void main(String[] args) throws Exception{
11+
public static void main(String[] args) throws Exception {
1212
int[] A = {0};
1313
int[] B = {1};
1414
new MergeSortedArray().merge(A, 0, B, 1);
15-
for(int i : A)
15+
for (int i : A)
1616
System.out.println(i);
1717
}
1818

1919
public void merge(int[] nums1, int m, int[] nums2, int n) {
2020
int i = m + n - 1, j = m - 1, k = n - 1;
21-
while(j >= 0 && k >= 0)
21+
while (j >= 0 && k >= 0)
2222
nums1[i--] = (nums1[j] > nums2[k]) ? nums1[j--] : nums2[k--];
23-
while(k >= 0)
23+
while (k >= 0)
2424
nums1[i--] = nums2[k--];
2525
}
2626

problems/src/array/MissingNumber.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@
33
/**
44
* Created by gouthamvidyapradhan on 04/07/2017.
55
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
6-
7-
For example,
8-
Given nums = [0, 1, 3] return 2.
9-
10-
Note:
11-
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
6+
* <p>
7+
* For example,
8+
* Given nums = [0, 1, 3] return 2.
9+
* <p>
10+
* Note:
11+
* Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
1212
*/
1313
public class MissingNumber {
1414

15-
public static void main(String[] args) throws Exception{
15+
public static void main(String[] args) throws Exception {
1616
int[] nums = {0};
1717
System.out.println(new MissingNumber().missingNumber(nums));
1818
}
19-
public int missingNumber(int[] nums){
19+
20+
public int missingNumber(int[] nums) {
2021
int sum = 0;
2122
int n = nums.length;
2223
for (int num : nums) {
2324
sum += num;
2425
}
2526
int arrSum = (((n + 1)) * n) / 2;
26-
if(arrSum == sum) return 0;
27+
if (arrSum == sum) return 0;
2728
else return arrSum - sum;
2829
}
2930
}

0 commit comments

Comments
 (0)