-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some aws challenges found in the web
- Loading branch information
1 parent
87bf07f
commit a4d8b3a
Showing
5 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package aws; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class FindMinimumPossibleVariance { | ||
|
||
public static void main(String...args) { | ||
|
||
System.out.println(findMinimumVariance(List.of(4, 2, 5, 4))); | ||
System.out.println(findMinimumVariance(List.of(4, 1, 2, 5, 3, 1, 4))); | ||
System.out.println(findMinimumVariance(List.of(4, 2, 5, 4, 4, 1, 2))); | ||
} | ||
|
||
|
||
/* | ||
public static int findMinimumVariance(List<Integer> height) { | ||
// buildings different heights | ||
// goal: group of building with 2 conditions | ||
//1. contiguous group with size >= 2 | ||
//2. first and last size of the group got to have the same size | ||
// variance should be as minimal as possible | ||
// variance = group size - occurrences of the first building size | ||
// 4i, 2, 5, 4j | ||
//4, 2, 5, 4, 3, 1, 4 | ||
//4, 2, 5, 4, 4, 1, 2 | ||
// i0 i3 -> index second - index first | ||
// size = (3 - 0) + 1 = 4 | ||
// variance = min(variance, size - 2) | ||
Integer variance = null; | ||
for (int i = 0; i < height.size(); i++) { | ||
int groupSize = 0; | ||
int occurrencesOfTheFirstBuildingSize = 1; // 2 | ||
int firstBuildingSize = height.get(i); // 4 | ||
for (int j = (i + 1); j < height.size(); j++) { | ||
groupSize = (j - i) + 1; // (2 - 0) + 1 = 4 | ||
int lastBuildingSize = height.get(j); | ||
if (lastBuildingSize == firstBuildingSize) { | ||
occurrencesOfTheFirstBuildingSize++; | ||
int possibleVariance = groupSize - occurrencesOfTheFirstBuildingSize; // 4 - 2 | ||
variance = Math.min((variance == null ? possibleVariance : variance), possibleVariance); | ||
} | ||
} | ||
} | ||
return variance == null ? -1 : variance; | ||
} | ||
*/ | ||
|
||
public static int findMinimumVariance(List<Integer> height) { | ||
Map<Integer, Integer> appear = new HashMap<>(); | ||
int result = Integer.MAX_VALUE; | ||
|
||
for (int i = 0; i < height.size(); i++) { | ||
int num = height.get(i); | ||
|
||
if (appear.containsKey(num)) { | ||
result = Math.min(result, i - appear.get(num) - 1); | ||
} | ||
|
||
appear.put(num, i); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package aws; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class InventoryManagementSystem { | ||
|
||
public static void main(String... args) { | ||
|
||
System.out.println(getMinimumChange(List.of(1,3,1,3), 2)); | ||
|
||
} | ||
|
||
public static int getMinimumChange(List<Integer> prod_price, int k) { | ||
|
||
|
||
// [5,7,7,8,3] | ||
// 5,7 // 7,7// 7,8// 8,3 | ||
// Map | ||
// 5, 1 | ||
// 7, 4 | ||
// 8, 2 | ||
// 3, 1 | ||
|
||
// [5,7,7,8] | ||
// 5,7 // 7,7// 7,8 | ||
// Map | ||
// 5, 1 | ||
// 7, 4 | ||
// 8, 1 | ||
|
||
|
||
// [1,3,1,3] | ||
// 1,3 // 3,1 // 1,3 | ||
// Map | ||
// 1, 3 | ||
// 3, 3 | ||
|
||
|
||
// [1,3,2,1,3] k=3 | ||
// 1,3,2 | ||
// 3,2,1 | ||
// 2,1,3 | ||
// Map | ||
// 1, 3 | ||
// 2, 3 | ||
// 3, 3 | ||
|
||
int changes = 0; | ||
Map<Integer, Integer> mapCount = new HashMap<>(); | ||
int maxRepeated = 0; | ||
for (int i = 0; i < prod_price.size(); i++) { | ||
var sub = prod_price.subList(i, i + (k - 1)); | ||
|
||
for (int v : sub) { | ||
int count = mapCount.getOrDefault(v, 0); | ||
mapCount.put(v, ++count); | ||
maxRepeated = Math.max(maxRepeated, mapCount.get(v)); | ||
} | ||
|
||
} | ||
|
||
for (Integer value : mapCount.values()) { | ||
if (value != maxRepeated) | ||
changes++; | ||
} | ||
|
||
return changes; | ||
|
||
} | ||
|
||
/* public static int getMinimumChange(List<Integer> prod_price, int k) { | ||
// [1,3,2,1,3], k=3 | ||
// [5,7,7,8], k=2 | ||
// [1,3,1,3], k=2 | ||
int changes = 0; | ||
for (int i = 0; i < k; i++) { | ||
Map<Integer, Integer> countMap = new HashMap<>(); | ||
int maxRepeat = 0; | ||
int groupSize = 0; | ||
for (int j = i; j < prod_price.size(); j += k) { | ||
int value = prod_price.get(j); | ||
int count = countMap.getOrDefault(value, 0); | ||
countMap.put(value, ++count); | ||
maxRepeat = Math.max(maxRepeat, countMap.get(value)); | ||
groupSize++; | ||
} | ||
changes = changes + (groupSize - maxRepeat); | ||
} | ||
return changes; | ||
} | ||
*/ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package aws; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class IsSpecialSequence { | ||
|
||
public boolean isSpecialSequence(String dna_sequence) { | ||
// palindromic patterns -> dna sequence string | ||
// special if: | ||
// can be divided into two non-empty | ||
// AND the two resulting string can be rearranged to form a palindrome | ||
// you can remove 1 char from each to make it work | ||
|
||
for (int i = 0; i < dna_sequence.length() - 1; i++) { | ||
String first = dna_sequence.substring(0, i + 1); | ||
String second = dna_sequence.substring(i + 1); | ||
|
||
if (isPalindromeSpecial(first) && isPalindromeSpecial(second)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isPalindromeSpecial(String s) { | ||
Map<Character, Integer> map = new HashMap<>(); | ||
for (int i = 0; i < s.length(); i++) { | ||
int count = map.getOrDefault(s.charAt(i), 0); | ||
map.put(s.charAt(i), ++count); | ||
} | ||
|
||
int oddCount = 0; | ||
for (Integer value : map.values()) { | ||
if (value % 2 != 0) | ||
oddCount++; | ||
} | ||
|
||
if (s.length() % 2 == 0) {// pair | ||
if (oddCount > 2) | ||
return false; | ||
} else { // odd | ||
if (oddCount > 1) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package aws; | ||
|
||
import java.util.*; | ||
|
||
public class PnL { | ||
|
||
public static void main(String... args) { | ||
|
||
|
||
|
||
List<Integer> pnl2 = new ArrayList<>(); | ||
pnl2.add(5); | ||
pnl2.add(3); | ||
pnl2.add(1); | ||
pnl2.add(2); | ||
System.out.println(maximizeNegativePnLMonths(pnl2)); | ||
|
||
List<Integer> pnl3 = new ArrayList<>(); | ||
pnl3.add(1); | ||
pnl3.add(1); | ||
pnl3.add(1); | ||
pnl3.add(1); | ||
pnl3.add(1); | ||
System.out.println(maximizeNegativePnLMonths(pnl3)); | ||
|
||
List<Integer> pnl4 = new ArrayList<>(); | ||
pnl4.add(5); | ||
pnl4.add(2); | ||
pnl4.add(3); | ||
pnl4.add(5); | ||
pnl4.add(2); | ||
pnl4.add(3); | ||
System.out.println(maximizeNegativePnLMonths(pnl4)); | ||
|
||
|
||
} | ||
|
||
public static int maximizeNegativePnLMonths(List<Integer> pnl) { | ||
|
||
// index (i) = month | ||
// pnl[i] = a representation of money (profit or loss) | ||
|
||
// pnl[i] * -1 (TURN IT LOSS) | ||
|
||
// maxNumberOfMonths afford to have a negative PnL | ||
// such that the cumulative for each of the n months is > 0 | ||
|
||
|
||
// [5,3,1,2i] | ||
// int sum = 0; // 5 -> 2 -> 1 -> -1 (1) | ||
// int i = 0; // 1 -> 2 -> 3 -> 4 | ||
// int nMonths = 0; // 2 | ||
|
||
|
||
// // [1, 1i, 1, 1, 1] | ||
// int sum = 0; // 1 -> 2 (0) -> | ||
// int i = 0; // 1 | ||
// int nMonths = 0; // 0 | ||
|
||
|
||
// [5, x2, 3, x5, 2, 3i] | ||
// | ||
// int sum = 0; // 5 -> 3 -> (0) 6 -> 1 -> (-1) 3 -> 0 | ||
// int i = 0; // 6 // finish | ||
// int nMonths = 0; // 2 | ||
|
||
|
||
// [5, x2, 3, 5, x2, x3] | ||
|
||
int sum = 0; | ||
for (Integer i : pnl) { | ||
sum += i; | ||
} | ||
|
||
pnl.sort(Comparator.naturalOrder()); | ||
|
||
int n = 0; | ||
for (Integer v : pnl) { | ||
|
||
sum = sum - (2 * v); | ||
|
||
if (sum > 0) | ||
n++; | ||
else | ||
break; | ||
|
||
|
||
} | ||
|
||
return n; | ||
|
||
} | ||
|
||
|
||
/* int sum = 0; | ||
int i = 0; | ||
int nMonths = 0; | ||
for (Integer v : pnl) { | ||
if (i == 0) { | ||
sum += v; | ||
i++; | ||
continue; | ||
} | ||
sum = sum + (v * -1); | ||
if (sum > 0) { | ||
nMonths++; | ||
} else { | ||
sum = (sum + v) + v; //rollback, plus v | ||
} | ||
i++; | ||
} | ||
return nMonths;*/ | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package aws; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class PalindromeAmazonTest { | ||
|
||
@Test | ||
public void test() { | ||
IsSpecialSequence palindromeAmazon = new IsSpecialSequence(); | ||
|
||
assertTrue(palindromeAmazon.isSpecialSequence("abcad")); | ||
assertTrue(palindromeAmazon.isSpecialSequence("abccba")); | ||
assertFalse(palindromeAmazon.isSpecialSequence("abcdef")); | ||
|
||
} | ||
} |