From: "Cédric Finance" Date: 2007-11-08T00:44:22+09:00 Subject: Re: a problem related string(250 score) ------=_Part_1411_4927106.1194450263855 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline This should solve the problem. class DiskClusters def self.minimumFragmentation(str,int) groups = str.split(/X+/).map { |e| e.size }.sort res = 0 while(val = groups.pop) do int -= val res += 1 return res if int <= 0 end -1 end end Cedric On Nov 7, 2007 4:12 PM, Johnson Wang <99100@163.com> wrote: > How to solve this problem in Ruby???? > ============================================== > You are given a String disk representing the clusters on a disk. An 'X' > represents a used cluster, and a '.' represents an available cluster. > You are also given an int size representing the size, in clusters, of a > file waiting to be written to disk. A file can only be stored in > clusters not already being used. > Return the minimum number of groups of consecutive clusters needed to > store the file on the disk. (The disk does not wrap around at the end.) > Return -1 if the disk does not have enough space available to store the > file. > Definition > > Class: > DiskClusters > Method: > minimumFragmentation > Parameters: > String, int > Returns: > int > Method signature: > int minimumFragmentation(String disk, int size) > (be sure your method is public) > > > Constraints > - > disk will contain between 1 and 50 characters, inclusive. > - > Each character of disk will be 'X' or '.'. > - > size will be between 1 and 50, inclusive. > Examples > 0) > > "." > 2 > Returns: -1 > We can't fit the file on the disk. > 1) > > ".XXXXXXXX.XXXXXX.XX.X.X." > 6 > Returns: 6 > There is only ever one cluster together, so all six clusters are > separated. > 2) > > "XX..XX....X.XX........X...X.XX...XXXX..XX...XXXXX." > 12 > Returns: 2 > We fit eight clusters together, and four clusters together. > 3) > > ".X.XXXX.......XX....X.....X............XX.X.....X." > 20 > Returns: 3 > > 4) > "....X...X..X" > 11 > Returns: -1 > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_1411_4927106.1194450263855--