LeetCode 167 Race Course Week

1290. binary integer transfer list

1291. Shun times

1292. element and smaller than the threshold value of the maximum side length of a square

The shortest path 1293. grid

1290. binary integer transfer list

1290. binary integer transfer list

Quote you a single linked list of nodes head. Value of each linked list node 1 is not 0. This list is known that a binary representation of integer numbers.

Please return the list to the number representing the decimal value .

Sample input and output sample Sample Input and Sample Output

Example 1:

**输入:** head = [1,0,1]
**输出:** 5
**解释:** 二进制数 (101) 转化为十进制数 (5)

Example 2:

**输入:** head = [0]
**输出:** 0

Example 3:

**输入:** head = [1]
**输出:** 1

Example 4:

**输入:** head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
**输出:** 18880

Example 5:

**输入:** head = [0,0]
**输出:** 0

Tip Hint

prompt:

  • The list is not empty.
  • The total number of nodes does not exceed the list 30.
  • Value of each node is not 0that 1.

Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
 public:
  int getDecimalValue(ListNode* head) {
    int ans(0);
    ListNode *p = head;
    while(p != nullptr) {
      ans = ans * 2 + (p->val);
      p = p->next;
    }
    return ans;
  }
};

1291. Shun times

1291. Shun times

We define "Shun times" as: large numbers on the former than the numbers on every 1integer.

Please return of [low, high]all times along the range consisting of an ordered list (in ascending order).

Sample input and output sample Sample Input and Sample Output

Example 1:

**输出:** low = 100, high = 300
**输出:** [123,234]

Example 2:

**输出:** low = 1000, high = 13000
**输出:** [1234,2345,3456,4567,5678,6789,12345]

Tip Hint

prompt:

  • 10 <= low <= high <= 10^9

Code

class Solution {
 public:
  vector<int> sequentialDigits(int low, int high) {
    vector<int>ans = {12,23,34,45,56,67,78,89,123,234,345,456,567,678,789,1234,2345,3456,4567,5678,6789,12345,23456,34567,45678,56789,123456,234567,345678,456789,1234567,2345678,3456789,12345678,23456789,123456789};
    vector<int>ret;
    int p = lower_bound(ans.begin(),ans.end(),low)-ans.begin();
    int n = ans.size();
    while(p < n && ans[p] <= high) ret.push_back(ans[p++]);
    return ret;
  }
};

1292. element and smaller than the threshold value of the maximum side length of a square

1292. element and smaller than the threshold value of the maximum side length of a square

To give you a size m x nmatrix matand an integer threshold value threshold.

Please return to a maximum sum of elements less than or equal side length of square region threshold; if no such square area, returns 0 .

Sample input and output sample Sample Input and Sample Output

Example 1:

**输入:** mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
**输出:** 2
**解释:** 总和小于 4 的正方形的最大边长为 2,如图所示。

Example 2:

**输入:** mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
**输出:** 0

Example 3:

**输入:** mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6
**输出:** 3

Example 4:

**输入:** mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184
**输出:** 2

Tip Hint

prompt:

  • 1 <= m, n <= 300
  • m == mat.length
  • n == mat[i].length
  • 0 <= mat[i][j] <= 10000
  • 0 <= threshold <= 10^5

Code

class Solution {
 public:
  int s[300][300];
  int n, m;
  int maxSideLength(vector<vector<int>>& mat, int threshold) {
    this->n = mat.size(), this->m = mat[0].size();
    memset(s, 0, sizeof(s));
    for(int i = 0; i < n; ++i)
      for(int j = 0; j < m; ++j) {
        s[i][j] = mat[i][j];
        if(i)
          s[i][j] += s[i - 1][j];
        if(j)
          s[i][j] += s[i][j - 1];
        if(i && j)
          s[i][j] -= s[i - 1][j - 1];
      }

    int first = 0, middle, half, len = min(n, m);
    const int key = threshold;
    while(len > 0) {
      half = len >> 1;
      middle = first + half;
      //cout << middle << "\n";
      if(cal(middle) > key)
        len = half;
      else {
        first = middle + 1;
        len = len - half - 1;
      }
    }
    //cout << "first " << first << "\n";
    return cal(first)>key ? first - 1 : first;
  }

 private :

  int getSum(int x1, int y1, int x2, int y2) {
    int ans = s[x2][y2];
    if(x1 && y1)
      ans += s[x1 - 1][y1 - 1];
    if(x1)
      ans -= s[x1 - 1][y2];
    if(y1)
      ans -= s[x2][y1 - 1];
    //cout << x1 << " " << y1 << " " << x2 << " " << y2 << ":" << ans << "\n";
    return ans;
  }
  int cal(int x) {
    if(x == 0) return 0;
    int ans = 0x3f3f3f3f;
    for(int i = x - 1; i < n; ++i)
      for(int j = x - 1; j < m; ++j) {
        int t = getSum(i - x + 1, j - x + 1, i, j);
        ans = min(ans, t);
      }
    //cout << "len " << x << ":" << ans << endl;
    return ans;
  }
};

The shortest path 1293. grid

The shortest path 1293. grid

Give you a m * ngrid, where each cell is not 0(blank) is 1(obstacle). Every step, you can be on the blank cell, down, left, right.

If you are up to can eliminate kone obstacle, find out from the top left (0, 0)to the bottom right corner of (m-1, n-1)
the shortest path, and returns the number of steps required by the path. If no such path, it returns -1.

Sample input and output sample Sample Input and Sample Output

Example 1:

**输入:**
grid =
[[0,0,0],
 [1,1,0],
 [0,0,0],
 [0,1,1],
 [0,0,0]],
k = 1
**输出:** 6
**解释:** 不消除任何障碍的最短路径是 10。
消除位置 (3,2) 处的障碍后,最短路径是 6 。该路径是 (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> **(3,2)** -> (4,2).

Example 2:

**输入:**
grid =
[[0,1,1],
 [1,1,1],
 [1,0,0]],
k = 1
**输出:** -1
**解释:** 我们至少需要消除两个障碍才能找到这样的路径。

Tip Hint

prompt:

  • grid.length == m
  • grid[0].length == n
  • 1 <= m, n <= 40
  • 1 <= k <= m*n
  • grid[i][j] == 0 **or** 1
  • grid[0][0] == grid[m-1][n-1] == 0

Code

class Solution {
 public:
  int shortestPath(vector<vector<int>>& grid, int k) {
    queue<node>que;
    const int inf = 0x3f3f3f3f;
    int ans = inf;
    n = grid.size(), m = grid[0].size();
    que.push(node(0, 0, k, 0));

    while(!que.empty()) {
      node cur = que.front();
      que.pop();
      if(grid[cur.r][cur.c])
        cur.k--;
      vis[cur.r][cur.c][k-cur.k] = true;

      if(cur.r == n - 1 && cur.c == m - 1 && cur.k >= 0) {
        ans = min(ans, cur.s);
      }
      for(int  i = 0, rr, cc; i < 4; ++i) {
        rr = cur.r + dr[i], cc = cur.c + dc[i];
        if(inBound(rr, cc) && !vis[rr][cc][k-cur.k] && k-cur.k <= n*m) {
          que.push(node(rr, cc, cur.k, cur.s + 1));
          vis[rr][cc][k-cur.k] = true;
        }
      }
    }
    return ans == inf ? -1: ans;
  }

 private :
  struct node {
    int r, c, k, s;

    node(int _r,int _c,int _k,int  _s):r(_r),c(_c),k(_k),s(_s){};
  };

  inline bool inBound(int r, int c) {
    return r >= 0 && r < n && c >= 0 && c < m;
  }
  const int dr[4] = {1, -1, 0, 0};
  const int dc[4] = {0, 0, 1, -1};
  int n,m;
  bool vis[41][41][41*41];
};

Guess you like

Origin www.cnblogs.com/Forgenvueory/p/12057653.html