You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 47e3fee
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: problems/src/array/BattleshipsInABoard.java
+31-30Lines changed: 31 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -3,51 +3,52 @@
3
3
/**
4
4
* Created by gouthamvidyapradhan on 12/08/2017.
5
5
* 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
Copy file name to clipboardExpand all lines: problems/src/array/CanPlaceFlowers.java
+23-24Lines changed: 23 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -3,48 +3,47 @@
3
3
/**
4
4
* Created by gouthamvidyapradhan on 10/06/2017.
5
5
* 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.
Copy file name to clipboardExpand all lines: problems/src/array/MergeSortedArray.java
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -3,24 +3,24 @@
3
3
/**
4
4
* Created by gouthamvidyapradhan on 29/07/2017.
5
5
* 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.
0 commit comments