There are N rectangles numbered from 0 to N-1. The K-th rectangle has size A[K] × B[K].
We want to arrange as many rectangles as possible into a strip. The rectangles can be arranged into a strip if they all share a side of the same length (which becomes the height of the strip). Note that rectangles can be rotated.
Write a function:
class Solution { public int solution(int[] A, int[] B); }
that, given two arrays A and B of N integers each, returns the maximum number of rectangles that can be arranged into a strip.
Examples:
1. Given A = [2, 3, 2, 3, 5] and B = [3, 4, 2, 4, 2], the function should return 3. Choosing the 0th, 2nd and 4th rectangles we can obtain the following strip of height 2 (note that the 0th rectangle was rotated):
We can also choose the 0th, 1st and 3rd rectangles to obtain a strip of height 3. Here we have rotated 1st and 3rd rectangles:
2. Given A = [2, 3, 1, 3] and B = [2, 3, 1, 3], the function should return 2. We can choose the 1st and 3rd rectangles:
3. Given A = [2, 10, 4, 1, 4] and B = [4, 1, 2, 2, 5], the function should return 3. We can choose the 0th, 2nd and 4th rectangles to obtain a strip of height 4:
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- arrays A and B both consist of N integers;
- each element of arrays A and B is an integer within the range [1..1,000,000,000].
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
List<Rect> list = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
Rect opt1 = new Rect(A[i], B[i]);
Rect opt2 = new Rect(B[i], A[i]);
if (opt1.equals(opt2)) {
list.add(opt1);
} else {
list.add(opt1);
list.add(opt2);
}
}
int heightCount = getCounter(list, "height");
int widthCount = getCounter(list, "width");
if (heightCount > widthCount) {
return heightCount;
} else if (widthCount > heightCount) {
return widthCount;
} else if (heightCount == widthCount && heightCount == 1) {
return heightCount + widthCount;
}
return heightCount;
}
private static int getCounter(final List<Rect> sortedList, String type) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
int counter = 0;
if ("height".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.height)) {
countMap.put(rec.height, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.height) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.height, tempCounter);
}
}
} else if ("width".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.width)) {
countMap.put(rec.width, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.width) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.width, tempCounter);
}
}
}
return counter;
}
private static class Rect {
private int height;
private int width;
Rect(int height, int width) {
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Rect geek = (Rect) obj;
return (geek.height == this.height) && (geek.width == width);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + height;
result = prime * result + width;
return result;
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
List<Rect> list = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
Rect opt1 = new Rect(A[i], B[i]);
Rect opt2 = new Rect(B[i], A[i]);
if (opt1.equals(opt2)) {
list.add(opt1);
} else {
list.add(opt1);
list.add(opt2);
}
}
int heightCount = getCounter(list, "height");
int widthCount = getCounter(list, "width");
if (heightCount > widthCount) {
return heightCount;
} else if (widthCount > heightCount) {
return widthCount;
}
return heightCount;
}
private static int getCounter(final List<Rect> sortedList, String type) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
int counter = 0;
if ("height".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.height)) {
countMap.put(rec.height, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.height) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.height, tempCounter);
}
}
} else if ("width".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.width)) {
countMap.put(rec.width, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.width) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.width, tempCounter);
}
}
}
return counter;
}
private static class Rect {
private int height;
private int width;
Rect(int height, int width) {
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Rect geek = (Rect) obj;
return (geek.height == this.height) && (geek.width == width);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + height;
result = prime * result + width;
return result;
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
List<Rect> list = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
Rect opt1 = new Rect(A[i], B[i]);
Rect opt2 = new Rect(B[i], A[i]);
if (opt1.equals(opt2)) {
list.add(opt1);
} else {
list.add(opt1);
list.add(opt2);
}
}
int heightCount = getCounter(list, "height");
int widthCount = getCounter(list, "width");
if (heightCount > widthCount) {
return heightCount;
} else if (widthCount > heightCount) {
return widthCount;
}
return heightCount;
}
private static int getCounter(final List<Rect> sortedList, String type) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
int counter = 0;
if ("height".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.height)) {
countMap.put(rec.height, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.height) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.height, tempCounter);
}
}
} else if ("width".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.width)) {
countMap.put(rec.width, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.width) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.width, tempCounter);
}
}
}
return counter;
}
private static class Rect {
private int height;
private int width;
Rect(int height, int width) {
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Rect geek = (Rect) obj;
return (geek.height == this.height) && (geek.width == width);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + height;
result = prime * result + width;
return result;
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
// write your code in Java SE 8
List<Rect> list = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
Rect opt1 = new Rect(A[i], B[i]);
Rect opt2 = new Rect(B[i], A[i]);
if (opt1.equals(opt2)) {
list.add(opt1);
} else {
list.add(opt1);
list.add(opt2);
}
}
int heightCount = getCounter(list, "height");
int widthCount = getCounter(list, "width");
if (heightCount > widthCount) {
return heightCount;
} else if (widthCount > heightCount) {
return widthCount;
}
return heightCount;
}
private static int getCounter(final List<Rect> sortedList, String type) {
Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
int counter = 0;
if ("height".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.height)) {
countMap.put(rec.height, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.height) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.height, tempCounter);
}
}
} else if ("width".equals(type)) {
for (Rect rec : sortedList) {
if (!countMap.containsKey(rec.width)) {
countMap.put(rec.width, 1);
if (counter == 0) {
counter = 1;
}
} else {
int tempCounter = countMap.get(rec.width) + 1;
if (tempCounter > counter)
counter = tempCounter;
countMap.replace(rec.width, tempCounter);
}
}
}
return counter;
}
private static class Rect {
private int height;
private int width;
Rect(int height, int width) {
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Rect geek = (Rect) obj;
return (geek.height == this.height) && (geek.width == width);
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + height;
result = prime * result + width;
return result;
}
}
}
The solution obtained perfect score.