https://www.youtube.com/watch?v=2vtT6TBnOAM&t=581s
Saturday, August 5, 2017
Saturday, July 29, 2017
HackerRank Equals
Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. She is biased towards her friends and may have distributed the chocolates unequally. One of the program managers gets to know this and orders Christy to make sure everyone gets equal number of chocolates.
But to make things difficult for the intern, she is ordered to equalize the number of chocolates for every colleague in the following manner,
For every operation, she can choose one of her colleagues and can do one of the three things.
- She can give one chocolate to every colleague other than chosen one.
- She can give two chocolates to every colleague other than chosen one.
- She can give five chocolates to every colleague other than chosen one.
Calculate minimum number of such operations needed to ensure that every colleague has the same number of chocolates.
Input Format
First line contains an integer denoting the number of testcases. testcases follow.
Each testcase has lines. First line of each testcase contains an integer denoting the number of colleagues. Second line contains N space separated integers denoting the current number of chocolates each colleague has.
Each testcase has lines. First line of each testcase contains an integer denoting the number of colleagues. Second line contains N space separated integers denoting the current number of chocolates each colleague has.
Constraints
Number of initial chocolates each colleague has <
Output Format
lines, each containing the minimum number of operations needed to make sure all colleagues have the same number of chocolates.
Sample Input
1
4
2 2 3 7
Sample Output
2
Explanation
1st operation: Christy increases all elements by 1 except 3rd one
2 2 3 7 -> 3 3 3 8
2nd operation: Christy increases all element by 5 except last one
3 3 3 8 -> 8 8 8 8
2 2 3 7 -> 3 3 3 8
2nd operation: Christy increases all element by 5 except last one
3 3 3 8 -> 8 8 8 8
import java.util.Scanner;
/**
* Created by vdokku on 7/29/2017.
*/
public class Equal {
static boolean DBG = false;
// Min number in the Array.
static int min;
//Find the minimised action counts
public static int MinRound(int[] counts) {
int[][] results = new int[counts.length][3];
for (int i = 0; i < counts.length; i++) {
for (int j = 0; j < 3; j++) {
int delta = counts[i] - min + j; // calculating the DELTA is important.
results[i][j] = 0;
while (true) {
// Greedy approach
if (delta >= 5) {
delta -= 5;
results[i][j]++;
} else if (delta >= 2) {
delta -= 2;
results[i][j]++;
} else if (delta >= 1) {
delta -= 1;
results[i][j]++;
} else {
break;
}
}
}
}
int finalResult = -1;
// Compare results from different baseline cases (keep min, take 1, 2 ).
for (int i = 0; i < 3; i++) {
int subFinal = 0;
for (int j = 0; j < counts.length; j++) {
subFinal += results[j][i];
if (DBG) System.out.format("results[%d][%d] = %d \n", j, i, results[j][i]);
}
if (DBG) System.out.println(subFinal);
if (finalResult < 0 || finalResult > subFinal) {
finalResult = subFinal;
}
}
return finalResult;
}
public static void main(String[] args) {
int casesCount = 0;
Scanner s = new Scanner(System.in);
if (s.hasNextInt()) {
casesCount = s.nextInt();
}
s.nextLine(); // throw away the newline.
int[] outputArrary = new int[casesCount];
for (int i = 0; i < casesCount; i++) {
int count = 0;
if (s.hasNextInt()) {
count = s.nextInt();
}
s.nextLine();
int[] numbers = new int[count];
min = -1;
for (int j = 0; j < count; j++) {
if (s.hasNextInt()) {
numbers[j] = s.nextInt();
// get min value from input array
if (numbers[j] < min || min < 0) {
min = numbers[j];
}
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
//SortCounts(numbers);
outputArrary[i] = MinRound(numbers);
}
for (int i = 0; i < casesCount; i++) {
System.out.println(outputArrary[i]);
}
}
}
Thursday, July 27, 2017
LEARN complex HIVE Queries
When working with the complex hive queries which involves different analytical functions.
Here is a sample query.
Here is a sample query.
SELECT
mag.co_magasin,
dem.id_produit as id_produit_orig,
pnvente.dt_debut_commercial as dt_debut_commercial,
COALESCE(pnvente.id_produit,dem.id_produit) as id_produit,
min(
CASE WHEN dem.co_validation IS NULL THEN 0 ELSE 1 END
) as flg_demarque_valide,
sum(CASE WHEN dem.co_validation IS NULL THEN 0 ELSE cast(dem.mt_revient_ope AS INT) END)
as me_dem_con_prx_cs,
0 as me_dem_inc_prx_cs,
0 as me_dem_prov_stk_cs,
sum(CASE WHEN dem.co_validation IS NULL THEN 0 ELSE cast(dem.qt_demarque AS INT) END)
as qt_dem_con,
0 as qt_dem_inc,
0 as qt_dem_prov_stk,
RANK() OVER (PARTITION BY mag.co_magasin, dem.id_produit ORDER BY pnvente.dt_debut_commercial DESC, COALESCE(pnvente.id_produit,dem.id_produit) DESC) as rang
from default.calendrier cal
INNER JOIN default.demarque_mag_jour dem
ON CASE WHEN dem.co_societe = 1 THEN 1 ELSE 2 END = '${hiveconf:in_co_societe}'
AND dem.dt_jour = cal.dt_jour
LEFT OUTER JOIN default.produit_norm pn
ON pn.co_societe = dem.co_societe
AND pn.id_produit = dem.id_produit
LEFT OUTER JOIN default.produit_norm pnvente
ON pnvente.co_societe = pn.co_societe
AND pnvente.co_produit_rfu = pn.co_produit_lip
AND pnvente.co_type_motif='05'
INNER JOIN default.kpi_magasin mag
ON mag.co_societe = '${hiveconf:in_co_societe}'
AND mag.id_magasin = dem.id_magasin
WHERE cal.dt_jour = '${hiveconf:in_dt_jour}'
AND NOT (dem.co_validation IS NULL AND cal.dt_jour > from_unixtime(unix_timestamp()-3*60*60*24, 'ddmmyyyy'))
-- JYP 4.4
AND dem.co_operation_magasin IN ('13','14','32')
GROUP BY
mag.co_magasin,
dem.id_produit,
pnvente.dt_debut_commercial,
COALESCE(pnvente.id_produit,dem.id_produit)
Tuesday, July 18, 2017
com.google.common.collect.Lists provide different methods.
com.google.common.collect;
Lists
public static <T> List<T> reverse(List<T> list) {
if (list instanceof ImmutableList) {
return ((ImmutableList<T>) list).reverse();
} else if (list instanceof ReverseList) {
return ((ReverseList<T>) list).getForwardList();
} else if (list instanceof RandomAccess) {
return new RandomAccessReverseList<T>(list);
} else {
return new ReverseList<T>(list);
}
}
@Override public List<T> subList(int fromIndex, int toIndex) {
checkPositionIndexes(fromIndex, toIndex, size());
return reverse(forwardList.subList(
reversePosition(toIndex), reversePosition(fromIndex)));
}
public static <T> List<List<T>> partition(List<T> list, int size) {
checkNotNull(list);
checkArgument(size > 0);
return (list instanceof RandomAccess)
? new RandomAccessPartition<T>(list, size)
: new Partition<T>(list, size);
}
Lists
public static <T> List<T> reverse(List<T> list) {
if (list instanceof ImmutableList) {
return ((ImmutableList<T>) list).reverse();
} else if (list instanceof ReverseList) {
return ((ReverseList<T>) list).getForwardList();
} else if (list instanceof RandomAccess) {
return new RandomAccessReverseList<T>(list);
} else {
return new ReverseList<T>(list);
}
}
@Override public List<T> subList(int fromIndex, int toIndex) {
checkPositionIndexes(fromIndex, toIndex, size());
return reverse(forwardList.subList(
reversePosition(toIndex), reversePosition(fromIndex)));
}
public static <T> List<List<T>> partition(List<T> list, int size) {
checkNotNull(list);
checkArgument(size > 0);
return (list instanceof RandomAccess)
? new RandomAccessPartition<T>(list, size)
: new Partition<T>(list, size);
}
why variables inside foreach loop of java8 should be final?
The Java Memory Model has very important property: it guarantees that local variables and method parameters are never writable by another thread. This adds much safety to multi-threading programming. However when you create a lambda (or an anonymous class), nobody knows how it will be used. It can be passed to another thread for execution (for example, if you use parallelStream().forEach(...)). Were it possible to modify the local variable that important property would be violated. Not the thing the Java language developers would sacrifice.
Usually when you are using lambdas, you are trying to program in functional way. But in functional programming mutable variables are considered bad practice: it's better to assign every variable only once. So trying to modify the local variable actually smells. Use various stream reduction methods instead of forEach to produce a good functional code.
https://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop
https://stackoverflow.com/questions/31801313/why-variables-inside-foreach-loop-of-java8-should-be-final
Usually when you are using lambdas, you are trying to program in functional way. But in functional programming mutable variables are considered bad practice: it's better to assign every variable only once. So trying to modify the local variable actually smells. Use various stream reduction methods instead of forEach to produce a good functional code.
https://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop
https://stackoverflow.com/questions/31801313/why-variables-inside-foreach-loop-of-java8-should-be-final