LeetCode 165 Race Course Week

LeetCode 165 Race Course Week

The winner of 5275. find Tic-tac-toe

5276. do not waste raw hamburger production program

5277. Statistics are all square sub-matrix 1

5278. Segmentation palindromic sequence III

C do violence can only say insufficient data

Tic-tac-toe to find out the winner of 4

Title Description Description

A and B play a Tic-tac-toe on a 3 x 3 grid.

Tic-tac-toe game rules are as follows:

玩家轮流将棋子放在空方格 (" ") 上。
第一个玩家 A 总是用 "X" 作为棋子,而第二个玩家 B 总是用 "O" 作为棋子。
"X" 和 "O" 只能放在空方格中,而不能放在已经被占用的方格上。
只要有 3 个相同的(非空)棋子排成一条直线(行、列、对角线)时,游戏结束。
如果所有方块都放满棋子(不为空),游戏也会结束。
游戏结束后,棋子无法再进行任何移动。

Moves to give you an array, where each element is another array of size 2 (elements corresponding grid rows and columns), it is in the order of actions A and B (after the first A B) of each of the two recorded pawn position.

If there is a game winner (A or B), then return to the game winner; if the game ends in a draw, "Draw" is returned; if there is still action (not the end of the game), "Pending" is returned.

You can assume that moves are effective (follow the rules of Tic-tac-toe), the mesh is initially empty, A will act first.

Sample input and output sample Sample Input and Sample Output

Example 1:

  输入:moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
  输出:"A"
  解释:"A" 获胜,他总是先走。
  "X  "    "X  "    "X  "    "X  "    "X  "
  "   " -> "   " -> " X " -> " X " -> " X "
  "   "    "O  "    "O  "    "OO "    "OOX"

Example 2:

  输入:moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
  输出:"B"
  解释:"B" 获胜。
  "X  "    "X  "    "XX "    "XXO"    "XXO"    "XXO"
  "   " -> " O " -> " O " -> " O " -> "XO " -> "XO "
  "   "    "   "    "   "    "   "    "   "    "O  "

Example 3:

Input: moves = [[0,0], [1,1], [2,0], [1,0], [1,2], [2,1], [0,1], [0, 2], [2,2]]
output: "draw"
output: Since there is no way to re-action, the game ended in a draw.
"XXO"
"OOX"
"XOX"

Example 4:

Input: moves = [[0,0], [1,1]]
Output: "Pending"
Explanation: the game is not over.
"The X-"
"O"
""

Tip Hint

prompt:

1 <= moves.length <= 9
moves[i].length == 2
0 <= moves[i][j] <= 2
moves 里没有重复的元素。
moves 遵循井字棋的规则。

answer

Simulation can, taking into account the 3x3-bit computing has seen the practice

Code

class Solution {
 public:

  int run(int g[3][3], vector<int>&move, int val) {
    g[move[0]][move[1]] = val;
    int t = 0, ans = 0;
    for(int i = move[0]; i < 3; ++i) {
      if(g[i][move[1]] == val)
        t++;
      else
        break;
    }
    for(int i = move[0] - 1; i >= 0; --i) {
      if(g[i][move[1]] == val)
        t++;
      else
        break;
    }
    ans = max(ans, t);
    t = 0;

    for(int i = move[1]; i < 3; ++i) {
      if(g[move[0]][i] == val)
        t++;
      else
        break;
    }
    for(int i = move[1] - 1; i >= 0; --i) {
      if(g[move[0]][i] == val)
        t++;
      else
        break;
    }
    ans = max(ans, t);
    t = 0;

    for(int i = 0; i + move[0] < 3 && i + move[1] < 3; ++i) {
      if(g[i + move[0]][i + move[1]] == val)
        t++;
      else
        break;
    }
    for(int i = -1; i + move[0] >= 0 && i + move[1] >= 0; --i) {
      if(g[i + move[0]][i + move[1]] == val)
        t++;
      else
        break;
    }
    ans = max(ans, t);
    t = 0;

    for(int i = 0; i + move[0] < 3 && move[1] - i >= 0; ++i) {
      if(g[i + move[0]][-i + move[1]] == val)
        t++;
      else
        break;
    }
    for(int i = -1; i + move[0] >= 0 && move[1] - i < 3 ; --i) {
      if(g[i + move[0]][-i + move[1]] == val)
        t++;
      else
        break;
    }
    ans = max(ans, t);
    t = 0;

    return ans;
  }
  bool finished(int g[3][3]) {
    for(int i = 0; i < 3; ++i)
      for(int j = 0; j < 3; ++j)
        if(g[i][j] == 0)
          return false;
    return true;
  }
  string tictactoe(vector<vector<int>>& moves) {
    int g[3][3];
    memset(g, 0, sizeof(g));
    for(int i = 0, n = moves.size(); i < n; ++i) {
      if(run(g, moves[i], i % 2 + 1) > 2)
        return (i & 1) ?  string("B") : string("A");
    }
    return finished(g) ? string("Draw") : string("Pending");
  }
};

Do not waste raw hamburger production program 4

5276. do not waste raw hamburger production program

Title Description Description

Christmas warm-up activities began Rights, we introduced a new burger burger set. To avoid wasting materials, you help them develop an appropriate production plan.

And give you two integers tomatoSlices cheeseSlices, represent the number of tomatoes and pieces of cheese. Hamburg with different materials as follows:

巨无霸汉堡:4 片番茄和 1 片奶酪
小皇堡:2 片番茄和 1 片奶酪

You to [total_jumbo, total_small] ([Big Mac hamburger total, the total number of small WHOPPER]) returns the appropriate number of formats production program, so that the remaining tomato slices and cheese slices cheeseSlices tomatoSlices is 0.

If you can not make the rest of the cheese slices and tomato slices tomatoSlices cheeseSlices number is 0, then go back to [].

Sample input and output sample Sample Input and Sample Output

Example 1:

Input: tomatoSlices = 16, cheeseSlices = 7
Output: [1,6]
Explanation: Big Mac and making a small WHOPPER 6 requires 4 1 + 2 6 = 16 + 6 = 1 tomato and cheese 7. The remaining material will not.

Example 2:

Input: tomatoSlices = 17, cheeseSlices = 4
output: []
Explanation: just make Fort Little Queen and Big Macs can not run out of all the raw materials.

Example 3:

Input: tomatoSlices = 4, cheeseSlices = 17
Output: []
Explanation: Big Mac making a cheese 16 will be left, will produce two small remaining 15 WHOPPER cheese.

Example 4:

Input: tomatoSlices = 0, cheeseSlices = 0
Output: [0,0]

Example 5:

Input: tomatoSlices = 2, cheeseSlices = 1
Output: [0,1]

Tip Hint

prompt:

0 <= tomatoSlices <= 10^7
0 <= cheeseSlices <= 10^7

answer

Consider first of all as a small imperial fort, each of the remaining 2 tomatoes into a small fort Wong Big Mac hamburger. Special attention to border and sentenced.

Code

class Solution {
 public:
  vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
    if(tomatoSlices > cheeseSlices * 4)
      return vector<int> {};
    vector<int> ans(2);
    ans[1] = cheeseSlices;
    tomatoSlices -= 2 * cheeseSlices;
    if(tomatoSlices < 0 || (tomatoSlices & 1))
      return vector<int> {};
    ans[1] -= tomatoSlices / 2;
    ans[0] += tomatoSlices / 2;
    tomatoSlices -= tomatoSlices - (tomatoSlices & 1);
    if(tomatoSlices != 0)
      return vector<int> {};
    return ans;
  }
};

Statistics are all square sub-matrix 1 5

5277. Statistics are all square sub-matrix 1

Title Description Description

You give a m * n matrix, the matrix element of either 0 or 1, and return to where you count the number of square sub-matrix 1 is entirely composed.

Sample input and output sample Sample Input and Sample Output

Example 1:

Input: Matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
a square of 1 with 10.
A square of 2 has four.
A square of 3 have a.
Number of squares = 10 + 4 + 1 = 15.

Example 2:

Input: Matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
a square of one of the six.
A square of a 2 there.
Number of squares = 6 + 1 = 7.

Tip Hint

1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1

answer

Violence can be had, apparently strong enough data.

Code

class Solution {
 public:
  int n, m;
  int countSquares(vector<vector<int>>& matrix) {
    n = matrix.size(), m = matrix[0].size();
    int ans(0);
    for(int i = 1, c; i <= min(n, m); ++i) {
      c = count(matrix, i);
      ans += c;
      if(!c)
        break;
    }
    return ans;
  }
  int count(vector<vector<int> >&matrix, int edgeLen) {
    int ans(0);
    for(int i = 0; i < n - edgeLen + 1; ++i) {
      for(int j = 0; j < m - edgeLen + 1; ++j)
        if(isSquareAllOne(matrix, i, j, edgeLen))
          ans++;
    }
    return ans;
  }

  bool isSquareAllOne(vector<vector<int>>&matrix, int x, int y, int edgeLen) {
    for(int i = 0; i < edgeLen; ++i) {
      for(int j = 0; j < edgeLen; ++j)
        if(!matrix[x + i][y + j])
          return false;
    }
    return true;
  }
};

Dividing palindromic sequence III

5278. Segmentation palindromic sequence III

Title Description Description

You give a lowercase letter string s, and an integer k.

Please split the string according to the following requirements:

首先,你可以将 s 中的部分字符修改为其他的小写英文字母。
接着,你需要把 s 分割成 k 个非空且不相交的子串,并且每个子串都是回文串。

Please return this way the minimum number of characters in the string division needed to be modified.

Sample input and output sample Sample Input and Sample Output

Example 1:

Input: s = "abc", k = 2
Output: 1
Explanation: You can split into strings "ab" and "c", and modify the "ab" in a character string it becomes a palindrome.

Example 2:

Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", which are palindromic sequence.

Example 3:

Input: s = "leetcode", k = 8
Output: 0

Tip Hint

1 <= k <= s.length <= 100
s 中只含有小写英文字母。

answer

dp [i] [j] represents the ending i, j into the least number of modified segments. Enumeration period ending on the location of pre,dp[i][j] = min(dp[i][j],dp[pre][j-1])

Code

class Solution {
 public:
  int palindromePartition(string s, int k) {
    int dp[s.length()][k + 1];
    memset(dp, 0x3f, sizeof(dp));
    for(int i = 0; i < s.length(); ++i) { // end with s[i]
      for(int j = 1; j <= k && j <= i + 1; ++j) { // has j segments
        if(j == 1)
          dp[i][j] = transferCost(s, 0, i);
        else
          for(int pre = max(j - 2, 0); pre < i; ++pre) {
            dp[i][j] = min(dp[i][j], dp[pre][j - 1] + transferCost(s, pre + 1, i));
          }
      }
    }
    return dp[s.length() - 1][k];
  }
  int transferCost(string s, int l, int r) {
    int ans(0);
    for(int i = 0; i < (r - l + 1) / 2; ++i)
      if(s[i + l] != s[r - i])
        ans++;
    return ans;
  }
};

Guess you like

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