leetcode

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

0440.cpp (605B)


      1 class Solution {
      2     static int count(int n, long p1, long p2) {
      3         int steps = 0;
      4         while (p1 <= n) {
      5 
      6             steps += min((long)(n + 1), p2) - p1;
      7             p1 *= 10, p2 *= 10;
      8         }
      9 
     10         return steps;
     11     }
     12 
     13   public:
     14     int findKthNumber(int n, int k) const {
     15         int crnt = 1;
     16         k--;
     17 
     18         while (k > 0) {
     19             int step = count(n, crnt, crnt + 1);
     20             if (step <= k) {
     21                 crnt++;
     22                 k -= step;
     23             } else {
     24                 crnt *= 10;
     25                 k--;
     26             }
     27         }
     28 
     29         return crnt;
     30     }
     31 };