int maxGap(vector<int> A, int n) { int _min = INT_MAX, _max = INT_MIN; for (int i = 0; i < n; ++i) { _min = min(_min, A[i]); _max = max(_max, A[i]); } vector<bool> hasNum(n + 1, false); vector<int> minBucket(n + 1), maxBucket(n + 1); for (int i = 0; i < n; ++i) { int idx = getBucketIdx(A[i], _min, _max, n); minBucket[idx] = hasNum[idx] ? min(minBucket[idx], A[i]) : A[i]; maxBucket[idx] = hasNum[idx] ? max(maxBucket[idx], A[i]) : A[i]; hasNum[idx] = true; } int i = 0; int last; int ret = INT_MIN; while (i <= n) { if (hasNum[i]) { last = i++; break; } } while (i <= n) { if (hasNum[i]) { ret = max(minBucket[i] - maxBucket[last], ret); last = i; } ++i; } return ret; }
double Parameter::getNum(string key) { if (!hasNum(key)) { cout<<key + "is not a valid num parameter"<<endl; throw key + "is not a valid num parameter"; } return this->num_map[key]; }
int findCentral (char *inputStr, size_t size) { char recurBuf[size+1];//add a place for '\0' char c;// tem; char case1c; int i = 0; int matchC, start, end; strncpy(recurBuf, inputStr, size); recurBuf[size] = '\0'; // TODO : tested -- 1+2 , (1+2) // tested -- 1.2 + 2.2 // tested -- (1+2) * 3 // failed -- ((3+5)/(2+2)-2) //cases not to recur: 1; (-1); -1.1 --> just one operand //cases needing recursion: 1+2; 1.1*2.2; (-1)*2; -1*2; -(1+2) ; ((1+2)*3)-4; 1*(2-3); (1+2)*(3-4) switch (judgeR (recurBuf, &i)) { case 1: //mean one operand and no recursion needed { i = size-1; while (i >= 0) { case1c = recurBuf[i--]; if (case1c == '-') { ungetch(' '); } if (case1c != '(' && case1c != ')' && !isspace(case1c)) {// space should be adjusted ungetch(case1c); } } ungetch(' '); return 1; } case 2: //mean more than one //two situations: useful parenthesis ;; no () or useless () { const int dui = i; matchC = matchParen(recurBuf, size, &start, &end); if(matchC == -1){ fprintf(stderr, "parenthesis are not in pair: %s, %s \n", recurBuf, inputStr); puts("You seems just entering part of expression, entering more?"); } //means match! -- () exists else if (matchC == 0) { //useful () if ( hasNum(recurBuf, 0, start-1) != -1 || hasNum(recurBuf, end, size) != -1 ) { while (i < start){ c = recurBuf[i++]; if ((c == '+' || c == '-') && hasNum(recurBuf, 0, i-1)!=-1 ){ ungetch(c); ungetch(' '); goto ufRecursion; } } i = end; while (i < size){ c = recurBuf[i++]; if ((c == '+' || c == '-') && hasNum(recurBuf, i, size)!=-1 ){ ungetch(c); ungetch(' '); goto ufRecursion; } } i = dui; while (i < start){ c = recurBuf[i++]; if ((c == '*' || c == '/') && hasNum(recurBuf, 0, i-1)!=-1){ ungetch(c); ungetch(' '); goto ufRecursion; } } i = end; while (i < size){ c = recurBuf[i++]; if ((c == '*' || c == '/') && hasNum(recurBuf, i, size)!=-1){ ungetch(c); ungetch(' '); goto ufRecursion; } } } // useless () else { i = start; while (--i >=0){ if (recurBuf[i] == '-'){ ungetch('-'); break; } } ungetch(' '); state &= findCentral(&recurBuf[start+1], end-start-2); return 1; } return 0;//means the input is not right ufRecursion: state &= findCentral (&recurBuf[i], size-i); state &= findCentral (recurBuf, i-1); }// no () else if (matchC == 1){ //i = 0; while (i < size){ c = recurBuf[i++]; if ((c == '+' || c == '-') && hasNum(recurBuf, 0, i-1)!=-1 ){ ungetch(c); ungetch(' '); goto nRecursion; } } i = dui; while (i < size){ c = recurBuf[i++]; if ((c == '*' || c == '/') && hasNum(recurBuf, 0, i-1)!=-1){ ungetch(c); ungetch(' '); goto nRecursion; } } return 0;//means input is not right nRecursion: state &= findCentral (&recurBuf[i], size-i); state &= findCentral (recurBuf, i-1);//the total size become less than size-1 } else { printf("bad in matchParen: %d \n", matchC); return 0; } return 1; } default: return 0; } }