コード例 #1
0
ファイル: a.cpp プロジェクト: iamslash/learn_to_code
 int findNumberOfLIS(std::vector<int>& V) {
   int mod = 1e9+7;
   int n = V.size();
   if (n <= 1)
     return n;
   std::vector<int> lens(n, 0);
   std::vector<int> cnts(n, 1);
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < i; ++j) {
       if (V[j] >= V[i])
         continue;
       if (lens[j] >= lens[i]) {
         lens[i] = lens[j] + 1;
         cnts[i] = cnts[j];
       } else if (lens[j]+1 == lens[i]) {
         cnts[i] = (cnts[i] + cnts[j]) % mod;
       }
     }
   }
   int maxlen = *std::max_element(lens.begin(), lens.end());
   int rslt = 0;
   for (int i = 0; i < n; ++i) {
     if (lens[i] == maxlen)
       rslt = (rslt + cnts[i]) % mod;
   }
   return rslt;
 }
コード例 #2
0
 int numTrees(int n) {
     if (n < 1)
         return 0;
     vector<int> cnts(n+1, -1);
     cnts[0] = 1;
     cnts[1] = 1;
     vector<int> nums(n);
     for (int i = 0; i < n; ++i)
         nums[i] = i;
     int result = count(nums, nums.begin(), nums.end()-1, cnts);
     return result;
 }
コード例 #3
0
ファイル: a.cpp プロジェクト: iamslash/learn_to_code
 int characterReplacement(std::string s, int k) {
   int n = s.size();
   std::vector<int> cnts(26, 0);
   int i = 0, max_cnt = 0, max_len = 0;
   for (int j = 0; j < n; ++j) {
     max_cnt = std::max(max_cnt, ++cnts[s[j]-'A']);
     while (j - i + 1 - max_cnt > k) {
       cnts[s[i++]-'A']--;
     }
     max_len = std::max(max_len, j - i + 1);
   }
   return max_len;
 }
コード例 #4
0
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    int longestIncreasingSubsequence(vector<int> nums) {
        if (nums.size() == 0) return 0;

        vector<int> cnts(nums.size());
        cnts[0] = 1;
        for (auto i = 1u; i < nums.size(); ++i) {
            int max_cnt = 0;
            for (auto j = 0u; j < i; ++j) {
                if (nums[i] >= nums[j]) {
                    max_cnt = max(max_cnt, cnts[j]);
                };
            }
            cnts[i] = max_cnt + 1;
        }

        return *max_element(cnts.begin(), cnts.end());
    }
コード例 #5
0
ファイル: Solution1.cpp プロジェクト: raoqiyu/LeetCode
 string removeDuplicateLetters(string s) {
     // Greddy
     // Each time we select a character which has the smllest lexicographical order from left to right  util  we meet a non
     // -duplicate character which we cannot skip
     
     // Notice : 
     // when we meet a non-duplicate character, this means that we must select a character from the substring (s[0] .... s[pos])
     // which consists of character from beginning of the string s to the position of the non-duplicate character
     
     // The non-duplicate character has two kinds:
     //      1. it only appear once among the whole string s
     //      2. it appears many times, but we have met all of the duplicate position, this position is its last appearance
     //          so we should maintain characters' count of appearance.
     // Once we have selected a characer we must delete all same character after its position
     // find next character from the substring
     if(s.empty())
         return "";
         
     vector<int> cnts(26, 0);
     for(auto c : s)
         cnts[c-'a']++;
     // the position of the character we will select
     int pos = 0;
     for(int i = 0; i < s.length(); i++){
         // Select the smallest lexicographical order character among the duplicate characters
         if(s[i] < s[pos])
             pos = i;
         // update the cnts infomation of the chatacter we meet, 
         // if s[i] is non-duplicate, we select a character(s[pos]) immediately
         // pos is in [0...i]
         if(--cnts[s[i]-'a'] == 0)
             break;
     }
     cout <<pos << endl;
     char selected = s[pos];
     string sub_s;
     // delete all selected character after position of pos
     for(int i = pos+1; i < s.length(); i++)
         if(s[i] != selected)
             sub_s += s[i];
     
     // find next character form the substring
     string ret = selected + removeDuplicateLetters(sub_s);
     
     return ret;
 }
コード例 #6
0
ファイル: solution.cpp プロジェクト: Time1ess/MyCodes
 string originalDigits(string s) {
     vector<int> cnts(26, 0);
     for(auto c: s)
         cnts[c-'a']++;
     string ans = "";
     static vector<pair<char, string>> uniques{{'z', "zero"},{'x', "six"},{'g', "eight"},{'s', "seven"},
                                               {'v', "five"},{'f', "four"},{'w', "two"},{'t', "three"},
                                               {'o', "one"},{'i', "nine"}};
     static vector<int> nums{0,6,8,7,5,4,2,3,1,9};
     for(int i = 0; i < 10; i++)
     {
         int k = cnts[uniques[i].first-'a'];
         for(auto c: uniques[i].second)
             cnts[c-'a'] -= k;
         ans.append(k, nums[i]+'0');
     }
     sort(ans.begin(), ans.end());
     return ans;
 }
コード例 #7
0
ファイル: Solution.cpp プロジェクト: hawkphantomnet/leetcode
 int coinChange(vector<int>& coins, int amount) {
     int max = amount + 1;
     vector<int> cnts(max, max);
     cnts[0] = 0;
     int n = coins.size();
     for (int i = 0; i < n; i++) {
         if (coins[i] < max) {
             cnts[coins[i]] = 1;
         }
     }
     for (int i = 1; i <= amount; i++) {
         for (int j = 0; j < n; j++) {
             int prev = i - coins[j];
             if (prev >= 1) {
                 cnts[i] = (cnts[i] < cnts[prev] + 1 ? cnts[i] : cnts[prev] + 1);
             }
         }
     }
     if (cnts[amount] == max) return -1;
     return cnts[amount];
 }
コード例 #8
0
    int shortestDistance(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size(), cnt = 0;
        vector<vector<int>> dists(m, vector<int>(n)), cnts(m, vector<int>(n));
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == 1) {
                    ++cnt;
                    BFS(grid, i, j, &dists, &cnts);
                }
            }
        }

        int shortest = numeric_limits<int>::max();
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (dists[i][j] < shortest && cnts[i][j] == cnt) {
                    shortest = dists[i][j];
                }
            }
        }

        return shortest != numeric_limits<int>::max() ? shortest : -1;
    }
コード例 #9
0
ファイル: IO.cpp プロジェクト: cran/rtk
void smplVec::rarefy(vector<double> depts, string ofile, int rep,
        DivEsts* divs, std::vector<vector<rare_map>> & RareSample,
        vector<string>& retCntsSampleName, string& skippedSample,
        vector<vector<vector<uint>>>* abundInRow, vector<vector<vector<uint>>>* occuencesInRow,
        int writes,bool write, bool fillret){
    bool doShuffle = true;
    long curIdx = 0;
    long dep;
    // resize divvs
    divs->richness.resize(depts.size());
    divs->shannon.resize(depts.size());
    divs->simpson.resize(depts.size());
    divs->invsimpson.resize(depts.size());
    divs->chao1.resize(depts.size());
    divs->eve.resize(depts.size());
    
    for(uint i = 0; i < depts.size(); i++){
        dep = (long)depts[i];

        if (dep > totSum){
            skippedSample = divs->SampleName;
            #ifdef notRpackage
            if (verbose){cout<<"skipped sample, because rowSums < depth \n";}
            #endif
            return;
        }
        //long curIdx=(long)totSum+1;
        for (int curRep=0;curRep<rep;curRep++){
            if(curIdx+dep >= (long) totSum || doShuffle == true){
                shuffle_singl();	
                curIdx = 0;
                doShuffle = false;
            }


            //count up
            vector<uint> cnts(numFeatures, 0);
            for (long i=(0+curIdx);i<(dep+curIdx);i++){
                cnts[arr[i]]++;
            }


            curIdx += dep;
            string t_out = ofile;
            if (rep!=1){
                std::ostringstream oss;
                oss<<curRep;
                t_out += "_" +oss.str();
            }

            if (curRep < writes && fillret) {
                rare_map cntsMap;
                // fill map:
                for(uint i = 0; i < cnts.size(); i++){
                    if(cnts[i] != 0){
                        cntsMap.insert( std::make_pair(i, cnts[i]) );
                    }
                }
                RareSample[i].push_back(cntsMap);
                if(curRep == 0){
                    retCntsSampleName[i] = divs->SampleName; // safe the sample name as well
                }
            }
            richness = 0;
            divs->richness[i].push_back(this->getRichness(cnts));
            vector<double> three = this->calc_div(cnts, 4);
            divs->shannon[i].push_back(three[0]);
            divs->simpson[i].push_back(three[1]);
            divs->invsimpson[i].push_back(three[2]);
            divs->chao1[i].push_back(this->calc_chao1(cnts,1));
            divs->eve[i].push_back(this->calc_eveness(cnts));
            richness = 0;

            // save abundance for chao2 calculations later
            rarefyMutex.lock();
            for(uint im = 0; im < IDs.size(); im++){
                //sparse convertions in swap mode
                int id = std::stoi(IDs[im]);
                if(cnts[im] != 0){
                    abundInRow->at(i)[curRep][id]++;	
                    occuencesInRow->at(i)[curRep][id] += cnts[im];
                }
            }
            rarefyMutex.unlock();
        }
    }
}
コード例 #10
0
ファイル: AsiMS2000.cpp プロジェクト: AllenBurnham/microscope
void AsiMS2000::selectCommand(int commandNum)
{
  switch(commandNum)
  {
      case 0:
          accel();
          break;
      case 1:
          aalign();
          break;
      case 2:
          afcont();
          break;
      case 3:
          aflim();
          break;
      case 4:
          afocus();
          break;
      case 5:
          afset();
          break;
      case 6:
          afmove();
          break;
      case 7:
          ahome();
          break;
      case 8:
          aij();
          break;
      case 9:
          array();
          break;
      case 10:
          azero();
          break;
      case 11:
          backlash();
          break;
      case 12:
          bcustom();
          break;
      case 13:
          benable();
          break;
      case 14:
          build();
          break;
      case 15:
          cdate();
          break;
      case 16:
          cnts();
          break;
      case 17:
          customa();
          break;
      case 18:
          customb();
          break;
      case 19:
          dack();
          break;
      case 20:
          dump();
          break;
      case 21:
          ensync();
          break;
      case 22:
          epolarity();
          break;
      case 23:
          error();
          break;
      case 24:
          halt();
          break;
      case 25:
          here();
          break;
      case 26:
          home();
          break;
      case 27:
          info();
          break;
      case 28:
          joystick();
          break;
      case 29:
          jsspd();
          break;
      case 30:
          kadc();
          break;
      case 31:
          kd();
          break;
      case 32:
          ki();
          break;
      case 33:
          kp();
          break;
      case 34:
          lcd();
          break;
      case 35:
          led();
          break;
      case 36:
          lladdr();
          break;
      case 37:
          load();
          break;
      case 38:
          lock();
          break;
      case 39:
          lockrg();
          break;
      case 40:
          lockset();
          break;
      case 41:
          maintain();
          break;
      case 42:
          motctrl();
          break;
      case 43:
          move();
          break;
      case 44:
          movrel();
          break;
      case 45:
          pcros();
          break;
      case 46:
          pedal();
          break;
      case 47:
          rbmode();
          break;
      case 48:
          rdadc();
          break;
      case 49:
          rdsbyte();
          break;
      case 50:
          rdstat();
          break;
      case 51:
          relock();
          break;
      case 52:
          reset();
          break;
      case 53:
          rt();
          break;
      case 54:
          runaway();
          break;
      case 55:
          saveset();
          break;
      case 56:
          savepos();
          break;
      case 57:
          scan();
          break;
      case 58:
          scanr();
          break;
      case 59:
          scanv();
          break;
      case 60:
          secure();
          break;
      case 61:
          sethome();
          break;
      case 62:
          setlow();
          break;
      case 63:
          setup();
          break;
      case 64:
          si();
          break;
      case 65:
          speed();
          break;
      case 66:
          spin();
          break;
      case 67:
          status();
          break;
      case 68:
          stopbits();
          break;
      case 69:
          ttl();
          break;
      case 70:
          um();
          break;
      case 71:
          units();
          break;
      case 72:
          unlock();
          break;
      case 73:
          vb();
          break;
      case 74:
          vector();
          break;
      case 75:
          version();
          break;
      case 76:
          wait();
          break;
      case 77:
          where();
          break;
      case 78:
          who();
          break;
      case 79:
          wrdac();
          break;
      case 80:
          zero();
          break;
      case 81:
          z2b();
          break;
      case 82:
          zs();
          break;
      case 83:
          overshoot();
          break;
  }
}