radobenc
BAN USERLet's try this:
public class IsPowerOf2 {
public static void main(String[] args) {
System.out.println(isPowerOf2(16384));
System.out.println(isPowerOf2(16382));
}
private static boolean isPowerOf2(int i) {
if (i < 2) {
return false;
}
int a = 2;
while (a < i) {
a = 2 * a;
if (i == a) {
return true;
}
}
return false;
}
}
With bits of Java library classes (Math, Arrays):
import java.util.Arrays;
public class NumberAsArray {
public static void main(String[] args) {
System.out.println(Arrays.toString(inc(new int[] {1,0,0,1})));
System.out.println(Arrays.toString(inc(new int[] {3,7,9,9})));
System.out.println(Arrays.toString(inc(new int[] {9,9,9,9,9})));
}
private static int[] inc (int[] is) {
// Convert array to number
int a = 0;
for (int i = 0; i < is.length; i++) {
a += is[i] * Math.pow(10, is.length - i - 1);
}
// Increment number
a++;
// Convert number to array
int[] result = new int[(int) Math.log10(a) + 1];
for (int i = 0; i < result.length; i++) {
result[result.length - i - 1] = a % 10;
a = a / 10;
}
return result;
}
}
The idea is to sort the list of candidates based on the result of diff() method.
The top 50 go to NY, bottom 50 go to SF.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class FlightScheduler {
static class CostData {
private final int id; // person id
private final int toNy;
private final int toSf;
public CostData(int id, final int toNy,
final int toSf) {
this.id = id;
this.toNy = toNy;
this.toSf = toSf;
}
int diff() {
return this.toNy - this.toSf;
}
}
private static final int NUMBER_OF_PARTICIPANTS = 100;
static CostData create(int id) {
return new CostData(id, random(), random());
}
private static void evaluateAndPrint(
List<CostData> data) {
int sumToNy = 0;
int sumToSf = 0;
for (int i = 0; i < data.size(); i++) {
CostData c = data.get(i);
System.out.println(String.format(
"%6d [%6d, %6d] diff=%6d", c.id,
c.toNy, c.toSf, c.diff()));
if (i < data.size() / 2) {
sumToNy += c.toNy;
} else {
sumToSf += c.toSf;
}
}
System.out.println("----------------------");
System.out.println(String.format(
" [%6d, %6d] total=%6d", sumToNy,
sumToSf, sumToNy + sumToSf));
System.out.println("----------------------");
}
public static void main(String[] args) {
List<CostData> input = new ArrayList<CostData>();
for (int i = 0; i < NUMBER_OF_PARTICIPANTS; i++) {
input.add(create(i));
}
// Evaluate and print the input
evaluateAndPrint(input);
// Evaluate and print cost saving
Collections.sort(input, new Comparator<CostData>() {
@Override
public int compare(CostData o1, CostData o2) {
return (int) (o1.diff() - o2.diff());
}
});
evaluateAndPrint(input);
}
static int random() {
return (int) (Math.random() * 1000);
}
}
Assuming I can user Java's Arrays.sort():
import java.util.Arrays;
public class CanMatchAfterSwap {
static boolean canMatch(String s1, String s2) {
if (null == s1 || null == s1 || s1.length() != s2.length()) {
return false;
}
// Extract and sort even or odd character strings from s1 and s2
String s1e = evenOrOdd(true, s1);
String s2e = evenOrOdd(true, s2);
String s1o = evenOrOdd(false, s1);
String s2o = evenOrOdd(false, s2);
// If sorted extract match, it can be done
return s1e.equals(s2e) && s1o.equals(s2o);
}
static String evenOrOdd(boolean even, String s) {
int start = even ? 0 : 1;
char[] result = new char[s.length() / 2 + 1];
int j = 0;
for (int i = start; i < s.length(); i = i + 2) {
result[j++] = s.charAt(i);
}
Arrays.sort(result);
return new String(result);
}
public static void main(String[] args) {
System.out.println(canMatch("cdab", "abcd"));
System.out.println(canMatch("dcba", "abcd"));
System.out.println(canMatch("abcde", "cdeba"));
}
}
package sk.radobenc.exercises;
import java.util.HashMap;
import java.util.Map;
/*
1 2 3
4 5 6
7 8 9
0
To adjust 0 position, fix the respective element in map m below.
*/
public class NumberPad {
static class Pos {
private final int row;
private final int column;
Pos(int row, int column) {
this.row = row;
this.column = column;
}
@Override
public boolean equals(Object obj) {
Pos p = (Pos) obj;
return column == p.column && row == p.row;
}
@Override
public int hashCode() {
return 31 * (31 + column) + row;
}
@Override
public String toString() {
return new StringBuilder().append('[').append(row).append(',').append(column).append(']').toString();
}
}
public static void main(String[] args) {
Map<Pos, Integer> m = new HashMap<Pos, Integer>();
m.put(new Pos(0, 0), 1);
m.put(new Pos(0, 1), 2);
m.put(new Pos(0, 2), 3);
m.put(new Pos(1, 0), 4);
m.put(new Pos(1, 1), 5);
m.put(new Pos(1, 2), 6);
m.put(new Pos(2, 0), 7);
m.put(new Pos(2, 1), 8);
m.put(new Pos(2, 2), 9);
m.put(new Pos(3, 0), 0);
Map<Integer, Pos> revM = new HashMap<Integer, Pos>();
for (Map.Entry<Pos, Integer> e : m.entrySet()) {
revM.put(e.getValue(), e.getKey());
}
int[][] moves = { { 2, 1 }, { 2, -1 }, { 1, -2 }, { 1, 2 }, { -1, -2 }, { -1, 2 }, { -2, -1 }, { -2, 1 } };
for (int i = 0; i < 10; i++) {
for (int j = 0; j < moves.length; j++) {
Pos source = revM.get(i);
Pos target = new Pos(source.row + moves[j][0], source.column + moves[j][1]);
if (null == m.get(target)) {
continue;
}
System.out.println("input=" + i + " target=" + target + " targetSign=" + m.get(target));
}
}
}
}
output:
input=0 target=[2,2] targetSign=9
input=0 target=[1,1] targetSign=5
input=1 target=[2,1] targetSign=8
input=1 target=[1,2] targetSign=6
input=2 target=[2,2] targetSign=9
input=2 target=[2,0] targetSign=7
input=3 target=[2,1] targetSign=8
input=3 target=[1,0] targetSign=4
input=4 target=[2,2] targetSign=9
input=4 target=[0,2] targetSign=3
input=5 target=[3,0] targetSign=0
input=6 target=[2,0] targetSign=7
input=6 target=[0,0] targetSign=1
input=7 target=[1,2] targetSign=6
input=7 target=[0,1] targetSign=2
input=8 target=[0,0] targetSign=1
input=8 target=[0,2] targetSign=3
input=9 target=[3,0] targetSign=0
input=9 target=[1,0] targetSign=4
input=9 target=[0,1] targetSign=2
Using PriorityQueue in Java:
- radobenc October 02, 2018