Skip to content

Commit

Permalink
Code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
gouthampradhan committed Sep 14, 2017
1 parent a273cc9 commit 47e3fee
Show file tree
Hide file tree
Showing 157 changed files with 4,046 additions and 4,473 deletions.
61 changes: 31 additions & 30 deletions problems/src/array/BattleshipsInABoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,52 @@
/**
* Created by gouthamvidyapradhan on 12/08/2017.
* 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:
You receive a valid board, made of only battleships or empty slots.
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.
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
Example:
X..X
...X
...X
In the above board there are 2 battleships.
Invalid Example:
...X
XXXX
...X
This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
Follow up:
Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
Solution:
The below solution works in one pass using only O(1) memory.
Iterate through each cell and add one to count if and only if the current cell equals 'X' and its adjacent upper and
left cell does not contain 'X'
* <p>
* You receive a valid board, made of only battleships or empty slots.
* 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.
* At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships.
* Example:
* X..X
* ...X
* ...X
* In the above board there are 2 battleships.
* Invalid Example:
* ...X
* XXXX
* ...X
* This is an invalid board that you will not receive - as battleships will always have a cell separating between them.
* <p>
* Follow up:
* Could you do it in one-pass, using only O(1) extra memory and without modifying the value of the board?
* <p>
* Solution:
* The below solution works in one pass using only O(1) memory.
* Iterate through each cell and add one to count if and only if the current cell equals 'X' and its adjacent upper and
* left cell does not contain 'X'
*/
public class BattleshipsInABoard {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception {
char[][] board = {{'X', '.', '.', 'X'}, {'.', '.', '.', 'X'}, {'.', '.', '.', 'X'}};
System.out.println(new BattleshipsInABoard().countBattleships(board));
}

public int countBattleships(char[][] board) {
int count = 0;
for(int i = 0; i < board.length; i ++){
for(int j = 0; j < board[0].length; j ++){
if(board[i][j] == 'X'){
if(i - 1 >= 0){ //check for the boundary condition
if(board[i - 1][j] == 'X')
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'X') {
if (i - 1 >= 0) { //check for the boundary condition
if (board[i - 1][j] == 'X')
continue;
}
if(j - 1 >= 0){
if(board[i][j - 1] == 'X'){
if (j - 1 >= 0) {
if (board[i][j - 1] == 'X') {
continue;
}
}
Expand Down
47 changes: 23 additions & 24 deletions problems/src/array/CanPlaceFlowers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@
/**
* Created by gouthamvidyapradhan on 10/06/2017.
* Accepted
*
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.
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.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: False
Note:
The input array won't violate no-adjacent-flowers rule.
The input array size is in the range of [1, 20000].
n is a non-negative integer which won't exceed the input array size.
* <p>
* 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.
* <p>
* 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.
* <p>
* Example 1:
* Input: flowerbed = [1,0,0,0,1], n = 1
* Output: True
* Example 2:
* Input: flowerbed = [1,0,0,0,1], n = 2
* Output: False
* Note:
* The input array won't violate no-adjacent-flowers rule.
* The input array size is in the range of [1, 20000].
* n is a non-negative integer which won't exceed the input array size.
*/
public class CanPlaceFlowers
{
public class CanPlaceFlowers {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
int[] n = {1,0,0,0,1};
public static void main(String[] args) throws Exception {
int[] n = {1, 0, 0, 0, 1};
System.out.println(new CanPlaceFlowers().canPlaceFlowers(n, 1));
}

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

int[] T = new int[flowerbed.length + 4];
for(int i = 0, j = 2; i < flowerbed.length; i ++)
for (int i = 0, j = 2; i < flowerbed.length; i++)
T[j++] = flowerbed[i];
T[0] = 1;
T[T.length - 1] = 1;
int total = 0, count = 0;
for(int i = 1; i < T.length; i ++) {
if(T[i] == 0)
for (int i = 1; i < T.length; i++) {
if (T[i] == 0)
count++;
else {
if((count % 2) == 0)
if ((count % 2) == 0)
total += ((count / 2) - 1);
else
total += (count / 2);
Expand Down
27 changes: 14 additions & 13 deletions problems/src/array/FirstMissingPositive.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,41 @@
/**
* Created by gouthamvidyapradhan on 24/06/2017.
* Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
* <p>
* For example,
* Given [1,2,0] return 3,
* and [3,4,-1,1] return 2.
* <p>
* Your algorithm should run in O(n) time and uses constant space.
*/
public class FirstMissingPositive {
private int L;
public static void main(String[] args) throws Exception{

public static void main(String[] args) throws Exception {
int[] nums = {1, 3, 5, 9};
System.out.println(new FirstMissingPositive().firstMissingPositive(nums));
}

public int firstMissingPositive(int[] nums) {
L = nums.length;
for(int i = 0; i < L; i ++){
if(nums[i] > 0 && nums[i] <= L && nums[i] != i + 1){
for (int i = 0; i < L; i++) {
if (nums[i] > 0 && nums[i] <= L && nums[i] != i + 1) {
int v = nums[i];
nums[i] = -1;
replace(v, nums);
}
}

for(int i = 0; i < L; i ++){
if(nums[i] != i + 1)
for (int i = 0; i < L; i++) {
if (nums[i] != i + 1)
return i + 1;
}

return L + 1;
}

private void replace(int i, int[] nums){
if(i > 0 && i <= L && i != nums[i - 1]){
private void replace(int i, int[] nums) {
if (i > 0 && i <= L && i != nums[i - 1]) {
int v = nums[i - 1];
nums[i - 1] = i;
replace(v, nums);
Expand Down
22 changes: 11 additions & 11 deletions problems/src/array/MaxProductOfThreeNumbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
/**
* Created by gouthamvidyapradhan on 27/06/2017.
* Given an integer array, find three numbers whose product is maximum and output the maximum product.
Example 1:
Input: [1,2,3]
Output: 6
Example 2:
Input: [1,2,3,4]
Output: 24
Note:
The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
* <p>
* Example 1:
* Input: [1,2,3]
* Output: 6
* Example 2:
* Input: [1,2,3,4]
* Output: 24
* Note:
* The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
* Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.
*/
public class MaxProductOfThreeNumbers {
public static void main(String[] args) {
int[] A = {1,2,3};
int[] A = {1, 2, 3};
System.out.println(new MaxProductOfThreeNumbers().maximumProduct(A));
}

Expand Down
53 changes: 30 additions & 23 deletions problems/src/array/MergeIntervals.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package array;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Created by gouthamvidyapradhan on 13/06/2017.
* Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
Solution: O(N log N) where N is the number of intervals
1. Sort the intervals based on start index
2. Mark the first interval as the current interval
3. For every ith interval starting 1 -> N, if the ith interval overlaps with the current interval then create a new
current interval. Else, add the current interval to result set and begin a new current interval.
* <p>
* For example,
* Given [1,3],[2,6],[8,10],[15,18],
* return [1,6],[8,10],[15,18].
* <p>
* Solution: O(N log N) where N is the number of intervals
* 1. Sort the intervals based on start index
* 2. Mark the first interval as the current interval
* 3. For every ith interval starting 1 -> N, if the ith interval overlaps with the current interval then create a new
* current interval. Else, add the current interval to result set and begin a new current interval.
*/
public class MergeIntervals
{
public class MergeIntervals {
public static class Interval {
int start;
int end;
Interval() { start = 0; end = 0; }
Interval(int s, int e) { start = s; end = e; }

Interval() {
start = 0;
end = 0;
}

Interval(int s, int e) {
start = s;
end = e;
}
}

public static void main(String[] args) throws Exception
{
public static void main(String[] args) throws Exception {
Interval i1 = new Interval(1, 2);
Interval i2 = new Interval(3, 4);
Interval i3 = new Interval(5, 6);
Expand All @@ -37,16 +45,15 @@ public static void main(String[] args) throws Exception
}

public List<Interval> merge(List<Interval> intervals) {
if(intervals.isEmpty()) return new ArrayList<>();
if (intervals.isEmpty()) return new ArrayList<>();
Collections.sort(intervals, (o1, o2) -> Integer.compare(o1.start, o2.start));
List<Interval> result = new ArrayList<>();
Interval curr = intervals.get(0);
for(int i = 1, l = intervals.size(); i < l; i ++) {
for (int i = 1, l = intervals.size(); i < l; i++) {
Interval I = intervals.get(i);
if(I.start >= curr.start && I.start <= curr.end) { //check if the new interval overlaps with the current
if (I.start >= curr.start && I.start <= curr.end) { //check if the new interval overlaps with the current
curr.end = curr.end > I.end ? curr.end : I.end;
}
else {
} else {
result.add(curr);
curr = I;
}
Expand Down
14 changes: 7 additions & 7 deletions problems/src/array/MergeSortedArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
/**
* Created by gouthamvidyapradhan on 29/07/2017.
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
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.
* <p>
* Note:
* 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.
*/
public class MergeSortedArray {
public static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception {
int[] A = {0};
int[] B = {1};
new MergeSortedArray().merge(A, 0, B, 1);
for(int i : A)
for (int i : A)
System.out.println(i);
}

public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m + n - 1, j = m - 1, k = n - 1;
while(j >= 0 && k >= 0)
while (j >= 0 && k >= 0)
nums1[i--] = (nums1[j] > nums2[k]) ? nums1[j--] : nums2[k--];
while(k >= 0)
while (k >= 0)
nums1[i--] = nums2[k--];
}

Expand Down
19 changes: 10 additions & 9 deletions problems/src/array/MissingNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@
/**
* Created by gouthamvidyapradhan on 04/07/2017.
* Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
* <p>
* For example,
* Given nums = [0, 1, 3] return 2.
* <p>
* Note:
* Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
*/
public class MissingNumber {

public static void main(String[] args) throws Exception{
public static void main(String[] args) throws Exception {
int[] nums = {0};
System.out.println(new MissingNumber().missingNumber(nums));
}
public int missingNumber(int[] nums){

public int missingNumber(int[] nums) {
int sum = 0;
int n = nums.length;
for (int num : nums) {
sum += num;
}
int arrSum = (((n + 1)) * n) / 2;
if(arrSum == sum) return 0;
if (arrSum == sum) return 0;
else return arrSum - sum;
}
}
Loading

0 comments on commit 47e3fee

Please sign in to comment.