# FloodDepth

Solution:

This java solution for the problem FloodDepth scored a 100%

Logic:

So the logic here is to imagine you are walking down the rocks..as you go down you try to set the minimum depth with how low you have reached, when you go up then at that point you must calculate the maximm depth so far and put it in a depths queue.

Also if you are climbing up higher than ever before then you need to actually reset the minimum node to null because its as good as entering a new valley. At this point reset the minimum depth to null and your highest altitude is also reset to this new high.

Keep doing that until you reach zero altitude, that is end of array!

And because we are collecting the depths in a PriorityQueue, whose comparator is set to maintain the elements in reverse order then in that case we have depths stored in descending order. so when you poll for the first time..you get your answer! maximum depth. Nevertheless if the size is zero..there was no water harvesting 🙂

I hope this makes sense with the algo I have written here, its the first pass..perhaps this can be enhanced with all optimised functions…I leave that to you coder!

``````import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;

public class FloodDepth {

public int solution(int[] A) {

Integer minNode = null;
Integer highNode = A;
Queue<Integer> depths = new PriorityQueue<>(Collections.reverseOrder());

for (int i = 1; i < A.length; i++) {
//we are going up

if (A[i] > A[i - 1]) {
if (A[i] > highNode && minNode != null) {
//new high, highest altitude, so entering new valley
int depth = highNode - minNode;
depths.offer(depth);
highNode = A[i];
minNode = null;
} else if (A[i] <= highNode && minNode != null) {
//going up, though we are still in the valley
if (A[i - 1] > minNode) {
int depth = A[i] - minNode;
depths.offer(depth);
} else {
int depth = A[i] - A[i - 1];
depths.offer(depth);
}
}
if (A[i] > highNode) {
//we need this for boundary conditions
highNode = A[i];
}
} else {
//we are going down
, lets evaluate our min node.
if ((minNode == null) || A[i] < minNode) {
minNode = A[i];
}
}
}
if (depths.size() > 0) {
return depths.poll();
}
return 0;
}

public static void main(String[] args) {
FloodDepth floodDepth = new FloodDepth();
int depth = floodDepth.solution(new int[] {1, 3, 2, 1, 2, 1, 5, 3, 3, 4, 2});
System.out.println(depth);

depth = floodDepth.solution(new int[]{100000000, 1, 2, 99999999});
System.out.println(depth);
}
}``````

# Substring Size K

Problem:

Given a string `s` and an int `k`, return all unique substrings of `s` of size `k` with `k` distinct characters.

Example 1:

```Input: s = "abcabc", k = 3
Output: ["abc", "bca", "cab"]

```

Example 2:

```Input: s = "abacab", k = 3
Output: ["bac", "cab"]

```

Example 3:

```Input: s = "awaglknagawunagwkwagl", k = 4
Output: ["wagl", "aglk", "glkn", "lkna", "knag", "gawu", "awun", "wuna", "unag", "nagw", "agwk", "kwag"]
Explanation:
Substrings in order are: "wagl", "aglk", "glkn", "lkna", "knag", "gawu", "awun", "wuna", "unag", "nagw", "agwk", "kwag", "wagl"
"wagl" is repeated twice, but is included in the output once.

```

Constraints:

• The input string consists of only lowercase English letters `[a-z]`
• 0 ≤ `k` ≤ 26

Solution:

Logic:

``````import java.util.HashSet;
import java.util.Set;

public class SusbstringSizeK {

public static void printUniqueSubstrings(String s, int k) {

if (k <= s.length()) {
Set<Character> uniqueCharacters = new HashSet<>();
Set<String> uniqueSubstrings = new HashSet<>();
for (int i = 0; i <= s.length() - k; i++) {
String shorterString = s.substring(i, i + k);
for (int j = 0; j < k; j++) {
}
if (uniqueCharacters.size() == k) {
}
uniqueCharacters.clear();
}
uniqueSubstrings.stream().forEach(value -> System.out.println(value));
}
}

public static void main(String[] args) {
printUniqueSubstrings("abcabc", 3);
printUniqueSubstrings("awaglknagawunagwkwagl", 4);
}
}
``````

# Partition Labels

A string `S` of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.

Example 1:

```Input: S = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
```

Note:

• `S` will have length in range `[1, 500]`.
• `S` will consist of lowercase English letters (`'a'` to `'z'`) only.

Solution:

Logic:

``````import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PartitionLabels {

public static List<Integer> partitionLabels(String S) {
List<Character> values = new ArrayList<>();

for (int i=0; i< S.length(); i++) {
if (!values.contains(S.charAt(i))) {
}
}

List<String> partitions = new ArrayList<>();
int partitionBegin = S.indexOf(values.get(0));
int partitionEnd = S.lastIndexOf(values.get(0));

for(int i=1; i< values.size(); i++) {
int startIndex = S.indexOf(values.get(i));
int endIndex = S.lastIndexOf(values.get(i));
if ((startIndex> partitionEnd)) {
partitionBegin = startIndex;
partitionEnd = endIndex;
} else if (endIndex > partitionEnd) {
partitionEnd = endIndex;
}
}