|
| 1 | +packageleetcode2018; |
| 2 | + |
| 3 | +importjava.util.LinkedList; |
| 4 | +importjava.util.Queue; |
| 5 | + |
| 6 | +publicclassItemAssociation { |
| 7 | + |
| 8 | +publicstaticvoidmain(String[]args) { |
| 9 | +// TODO Auto-generated method stub |
| 10 | +ItemAssociationtest =newItemAssociation(); |
| 11 | +String[][]mat=newString[][]{{"Item1","Item2"}, |
| 12 | +{"Item3","Item4"}, |
| 13 | +{"Item4","Item5"}}; |
| 14 | + |
| 15 | +test.largestItemAssociation(mat); |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +publicString[]largestItemAssociation(String[][]itemAssociation) |
| 20 | +{StringBuffertemp=newStringBuffer(); |
| 21 | +intn=Integer.MIN_VALUE; |
| 22 | +Queue<String>q =newLinkedList<>(); |
| 23 | +for(inti =0;i<itemAssociation.length;i++){ |
| 24 | +if(!q.isEmpty() &&q.peek()!=itemAssociation[i][0]){ |
| 25 | +if(n<q.size()){ |
| 26 | +n=q.size(); |
| 27 | +temp.setLength(0); |
| 28 | +while(!q.isEmpty()){ |
| 29 | +temp.append(q.poll()+","); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +for(intj=0;j<itemAssociation[0].length;j++){ |
| 34 | +if(!q.contains(itemAssociation[i][j])) |
| 35 | +q.offer(itemAssociation[i][j]); |
| 36 | + } |
| 37 | +} |
| 38 | +if(n<q.size()){ |
| 39 | +n=q.size(); |
| 40 | +temp.setLength(0); |
| 41 | +while(!q.isEmpty()){ |
| 42 | +temp.append(q.poll()+","); |
| 43 | + } |
| 44 | + } |
| 45 | +String[]re=temp.toString().split(","); |
| 46 | +returnre; |
| 47 | +} |
| 48 | + |
| 49 | +} |