You are given N disks and two rods, each with one initial disk.
On the left rod, disks can be placed in decreasing order of size (smaller disks on top of bigger ones). On the right rod, disks can be placed in increasing order of size (bigger disks on top of smaller ones). Note that it is not permissible to place two disks of equal size on top of each other. The initial disks cannot be moved.
Write a function:
class Solution { public int solution(int[] A, int L, int R); }
that, given an array A of integers representing the sizes of the N disks and two integers L and R representing the size of the initial disks on the left and right rods respectively, returns the maximum number of disks from A that can be placed on the rods while satisfying the rules presented above.
Examples:
1. Given A = [2, 3, 3, 4], L = 3 and R = 1, your function should return 3, since only three disks can be placed on the rods. Note that the disk of size 2 can be placed on either the left rod or the right rod.
2. Given A = [1, 4, 5, 5], L = 6 and R = 4, your function should return 4.
3. Given A = [5, 2, 5, 2], L = 8 and R = 1, your function should return 4.
4. Given A = [1, 5, 5], L = 2 and R = 4, your function should return 2.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- each element of array A is an integer within the range [1..1,000,000,000];
- L and R are integers within the range [1..1,000,000,000].
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
for(int i=0;i<A.length;i++){
if(A[i]>A)
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Ha
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
HashMap mapL = new HashMap();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Integer> hashMap = new HashMap<String, Integer>(
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
for(int i=0;i<A.length;i++){
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
Solution.java:19: error: reached end of file while parsing } ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
Solution.java:10: error: cannot find symbol Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>(); ^ symbol: class Map location: class Solution Solution.java:10: error: cannot find symbol Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>(); ^ symbol: class Bool location: class Solution Solution.java:10: error: cannot find symbol Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>(); ^ symbol: class HashMap location: class Solution Solution.java:10: error: cannot find symbol Map<Integer, Bool> hashMap = new HashMap<Integer, Bool>(); ^ symbol: class Bool location: class Solution 4 errors
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
Map<Integer, boolean> hashMap = new HashMap<Integer, boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
Solution.java:10: error: cannot find symbol Map<Integer, boolean> hashMap = new HashMap<Integer, boolean>(); ^ symbol: class Map location: class Solution Solution.java:10: error: unexpected type Map<Integer, boolean> hashMap = new HashMap<Integer, boolean>(); ^ required: reference found: boolean Solution.java:10: error: cannot find symbol Map<Integer, boolean> hashMap = new HashMap<Integer, boolean>(); ^ symbol: class HashMap location: class Solution Solution.java:10: error: unexpected type Map<Integer, boolean> hashMap = new HashMap<Integer, boolean>(); ^ required: reference found: boolean 4 errors
// you can also use imports, for example:
// import java.util.*;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
mapL
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
mapL.put(a[i])
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L){
mapL.put(a[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i]==null)){
mapL.put(A[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
}
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]>R && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]>R && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]>R && mapL.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]<R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]>L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]<R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
A[i] = 0;
count++;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
continue;
count++;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
A[i] = 0;
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
continue;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
count++;
}
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
continue;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
count++;
}
}
return count;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
continue;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
count++;
}
}
return count;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
continue;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
count++;
}
}
return count;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
// 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 L, int R) {
int count =0;
HashMap<Integer, Boolean> mapL = new HashMap<Integer, Boolean>();
HashMap<Integer, Boolean> mapR = new HashMap<Integer, Boolean>();
for(int i=0;i<A.length;i++){
if(A[i]<L && mapL.get(A[i])==null){
mapL.put(A[i],true);
count++;
continue;
}
if(A[i]>R && mapR.get(A[i])==null){
mapR.put(A[i],true);
count++;
}
}
return count;
}
}
The solution obtained perfect score.
Small random tests, every value occurs at least twice. N = 100.