leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2232.cpp (828B)


      1 class Solution {
      2   public:
      3     string minimizeResult(const string &expression) {
      4         const int idx = expression.find('+');
      5         const string a = expression.substr(0, idx);
      6         const string b = expression.substr(idx + 1);
      7 
      8         int mini = INT_MAX, mi, mj;
      9         for (int i = 0; i < a.size(); i++) {
     10             for (int j = 1; j <= b.size(); j++) {
     11                 int out = (i != 0 ? stoi(a.substr(0, i)) : 1) * (j != b.size() ? stoi(b.substr(j)) : 1);
     12                 int in = stoi(a.substr(i)) + stoi(b.substr(0, j));
     13                 if (in * out < mini) {
     14                     mini = in * out;
     15                     mi = i;
     16                     mj = j;
     17                 }
     18             }
     19         }
     20 
     21         return a.substr(0, mi) + "(" + a.substr(mi) + "+" + b.substr(0, mj) + ")" + b.substr(mj);
     22         ;
     23     }
     24 };