'Finding the number of visible clothes in rope
Question: Count the Clothes Visible
A lady recently hired a maid to look after her household work so that she can concentrate on setting up her new business. As part of the routine work, the maid cleans the house and washes the clothes every day.
However, there is a problem with the drying of clothes on the rope. Since the rope is small and all the clothes are not able to be spread out properly, the maid places one cloth on top of other cloth. So some of the clothes are covered - partially or completely - by the other ones. Knowing the order and the position in which the clothes were hung, determine how many clothes are visible (partially or completely) when seen from front.
Consider the rope was of length N meters divided into N equal sections starting from 0 to N. Each cloth of width P occupies one or more than one section completely. (1<=P<=N & P is a +ve integer).
(Note: Ignore the other dimension of cloth for the purpose of this problem) Input Specifications Your program must read three arguments RopeLength, CountofClothes, ClothesPosition[] where RopeLength is the length of the rope in meters (1<=RopeLength<=10000) CountofClothes is the number of clothes which are placed on the rope (1<=CountofClothes<=10000) ClothesPosition is an array giving the position in which the clothes were hung. The cloth position is described by two integers L and W, where L represents the start position of where the cloth was hung (0<=L<=10000) and W is the width of the cloth (1<=W<=10000).
The order in which the input is received is the order in which the clothes are placed on the rope. Output Specifications Your function GetVisibleCount should set the output variable 'output1' to the count of clothes visible completely or partially.
Example Sample input:
10:5:{{0,4},{6,3},{1,5},{6,4},{7,2}}
Here 10 is the length of the rope in meters. 5 is the number of clothes hung on rope. The first cloth starts from 0 and covers 4 sections from 0. The second cloth starts at 6 and covers 3 sections from 6 and so on.. Sample output:
4
The total number of clothes visible when seen from front is 4.
Solution 1:[1]
public class DryingClothes {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//System.out.println("Enter rope length");
//int ropeLength = scan.nextInt();
System.out.println("Enter number of clothes");
int clothesCount = scan.nextInt();
int[][] dimensions = new int[clothesCount][2];
Map<Integer, Integer> visibility = new HashMap<>();
for(int i=0;i<clothesCount;i++) {
dimensions[i][0] = scan.nextInt();
dimensions[i][1] = scan.nextInt();
for(int j=dimensions[i][0];j<dimensions[i][0]+dimensions[i][1];j++) {
visibility.put(j, i);
}
}
Set<Integer> clothesRemaining = new HashSet<>();
for(int key : visibility.keySet()) {
clothesRemaining.add(visibility.get(key));
}
System.out.println(clothesRemaining.size());
scan.close();
}
}
Solution 2:[2]
The logic I have tried implementing is supposed there are 2 clothes starting at same position and width of one is greater than another; then that will cover one cloth and hence that cloth will not be visible.
And if suppose there are 2 clothes one with position 1 and another with position 2 but if the width of cloth at position 1 is more than the width of cloth at position 2 then in that case again a cloth will not be visible.
So we will run loop for n times where n=number of clothes and every time I see this condition number of visible clothes=n-1.
But the issue here is user input for the programme should happen in an incremental manner as per position. That is after 2 positions I can give input for 3 or 4th position not for 5th one and then the 3rd one.
Secondly, I want to keep one variable as class variable=numberofclothes and that gets reduced everytime I meet the above-mentioned conditions But I do not have sufficient test data to check this.
package main;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
import javax.swing.plaf.synth.SynthScrollBarUI;
public class Ropecalculation {
static int numberofvisibleclothes;
static int ropelenth;
static int numberofclother;
static int clothwidth;
static int startposition;
static int[] startpoint;
static int[] width;
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.println("Enter the rope length");
ropelenth=scan.nextInt();
System.out.println("Enter the number of clothes");
numberofclother=scan.nextInt();
for(int i=0;i<numberofclother;i++){
startpoint=new int[numberofclother];
width=new int[numberofclother];
System.out.println("Enter start position");
startposition=scan.nextInt();
startpoint[i]=startposition;
System.out.println("Enter width");
clothwidth=scan.nextInt();
width[i]=clothwidth;
//System.out.println(startpoint.length);
}
Ropecalculation rp=new Ropecalculation();
rp.checkvisibleclothes(startpoint,width);
}
public void checkvisibleclothes(int[] startpoint, int[] width) {
for(int j=0;j<startpoint.length-1;j++){
int x=startpoint[j];
int c=startpoint[j+1];
if(x==c){
int wide=width[j];
int wideagain=width[j+1];
if(wide<=wideagain){
numberofvisibleclothes=numberofclother-1;
}
}
else if(c==x+1){
int wide1=width[j];
int wideagain1=width[j+1];
if(wide1>wideagain1){
numberofvisibleclothes=numberofclother-1;
}
}
}
System.out.println(numberofvisibleclothes);
}
}
Solution 3:[3]
This is the solution of the "Counting the visible clothes" code in the Python language. The code contain the minimum lines with an optimized solution. Time complexity of the given solution is O(n*m).
Function will take three argument:
First will the length of rope,
Second will be number of clothes, and
Third will the Array that contains sizes of clothes.
def getVisibleCount(length,n,arr): visible,cr = dict(),set() for i in range(n): for j in range(arr[i][0],sum(arr[i])+1): visible[j]=i for k,v in visible.items(): cr.add(v) return len(cr)
Another Solution is :
def getVisibleCount(length,n,arr):
visible = [0 for _ in range(length)]
for i in range(n):
for j in range(arr[i][0],sum(arr[i])+1):
visible[j]=i
return len(set(visible))
For Test cases :
print(getVisibleCount(10,5,[[0,4],[6,3],[1,5],[6,4],[7,2]]))
print(getVisibleCount(11,4,[[0,2],[1,3],[3,4],[6,5]]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robert |
| Solution 2 | Ankit Bajpai |
| Solution 3 |
