mailchiranjib
BAN USER//
public static int getSubsequence(int[] nums, int k) {
if (nums.length == 0 || k == 0)
return 0;
int sumOfSub, count = 0, end = 0, begin = end;
sumOfSub = nums[end];
while (begin < nums.length) {
if (sumOfSub % k == 0) {
count++;
}
end++;
if (end == nums.length) {
begin++;
if (begin < nums.length) {
end = begin;
sumOfSub = nums[end];
}
} else {
sumOfSub += nums[end];
}
}
return count;
}
public static void printCol(int[] arr, int c) {
int rows = 0;
for (int i = 0; i < arr.length; i++) {
if (c * i > arr.length) {
rows = i;
break;
}
}
int j = 0;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
j++;
if (j == rows) {
j = 0;
System.out.println("");
}
}
}
public static Collection<Point> findNearest(List<Point> list, Point center, int m) {
list.stream().sorted((a, b) -> Double.compare(distanceInPlane(a, center), distanceInPlane(b, center)));
return list.subList(0, m);
}
public static double distanceInPlane(Point a, Point b) {
return Math.sqrt(Math.pow(Math.abs(a.x - b.x), 0.5) + Math.pow(Math.abs(a.y - b.y), 0.5));
}
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
/**
* Idea is to put all the chars from first string in a map with values as
* true. not everytime in the rest of the strings, we can not find a
* character, we set it to false. What we will have in the end is a list of
* common chars (keys) with values true
*
* @param list
* @return
*/
public static int numberOfCommonChars(List<String> list) {
if (list == null || list.size() == 0) {
return 0;
}
if (list.size() == 1) {
return list.get(0).length();
}
Map<Character, Boolean> map = new HashMap<Character, Boolean>();
// This can be further optimized by choosing the shortest string in the
// list
String fStaring = list.get(0);
for (int i = 0; i < fStaring.length(); i++) {
map.put(fStaring.charAt(i), true);
}
for (int i = 1; i < list.size(); i++) {
String s = list.get(i);
for (Map.Entry<Character, Boolean> entry : map.entrySet()) {
if (s.indexOf(entry.getKey()) < 0) {
map.put(entry.getKey(), false);
}
}
}
return Collections.frequency(map.values(), true);
}
My idea is -
- mailchiranjib June 06, 20181. Convert the input to uni-cost graph
2. Find shortest path between two input odes and keep track of the conversation rate of the edges that are part of the shortest path.
3. Multiply the the rates with given amount