java - Find largest sequence within an arraylist -
some background
last week did problem in textbook told me generate 20 random numbers , put brackets around successive numbers equal consider following program outputs
697342(33)(666)(44)69(66)1(88)
what need do
the next problem longest sequence of these words , put brackets around them. if have
1122345(6666)
basically need put brackets around 4 6's , since occur often. i've finished other problems in chapter studying ( arrays , arraylists), can't seem figure 1 out.
here solution have made putting brackets around successive numbers:
class seq { private arraylist<integer> nums; private random randnum; public seq() { nums = new arraylist<integer>(); randnum = new random(); } public void fillarrlist() { (int = 0 ; < 20 ; i++) { int thisrandnum = randnum.nextint(9)+1; nums.add(thisrandnum); } } public string tostring() { stringbuilder result = new stringbuilder(); boolean inrun = false; (int = 0; < nums.size(); i++) { if (i < nums.size() - 1 && nums.get(i).equals(nums.get(i + 1))) { if (!inrun) { result.append("("); } result.append(nums.get(i)); inrun = true; } else { result.append(nums.get(i)); if (inrun) { result.append(")"); } inrun = false; } } return result.tostring(); } }
my thoughts
iterate through whole list. make count variable, keeps track of how many numbers successive of each other. i.e 22
have count of 2
. 444
count of 3
next make oldcount
, compares current count
oldcount
. want keep going if our new count
greater oldcount
after need way starting index of largest count
variable, end.
is way of thinking correct? because i'm having trouble updating oldcount , count variable while comparing them, since there values change. i'm not looking code, rather valuable hints.
my count resetting this
int startindex, endindex = 0; int count = 0; int oldcount = 0; for(int = 0 ; < nums.size(); i++) { if(nums.get(i) == nums.get(i+1) && count >= oldcount) { count++; } oldcount = count; }
only after walking elements know longest subsequence.
11222333333444555 11222(333333)444555
hence after loop can insert both brackets.
so have maintain local optimum: start index plus length or last index of optimum. , every sequence start index of current sequence.
as asked:
the optimal state (sequence) , current state 2 things. 1 cannot in advance current state final optimal state.
public string tostring() { // begin "best" solution empty sequence. int startbest = 0; // starting index int lengthbest = 0; // length of sequence // determine sequences: int startcurrent = 0; // starting index of current/last sequence (int = 0; < nums.size(); i++) { // can add current num current sequence? if (i == startcurrent || nums.get(i).equals(nums.get(i - 1)))) { // can extend current sequence i: int lengthcurrent = - startcurrent + 1; if (lengthcurrent > lengthbest) { // current length better? // new optimum: startbest = startcurrent; lengthbest = lengthcurrent; } } else { // different num, start here. // had real sequence (i != 0), no need // checking new optimum length 1. startcurrent = i; } } // found best solution. // create result: stringbuilder result = new stringbuilder(); (int = 0; < nums.size(); i++) { result.append(nums.get(i)); } // insert right ')' first index changes 1 after inserting '('. result.insert(startbest + lengthbest, ")"); result.insert(startbest, "("); return result.tostring(); }
the first problem how find end of sequence, , set correct start of sequence.
the problem original algorithm there handled 1 sequence (one subsequence start).
Comments
Post a Comment