Leetcode周赛165

目录

  • 找出井字棋的获胜者

    • 思路
    • 代码
  • 不浪费原料的汉堡制作方案
    • 思路
    • 代码
  • 统计全为 1 的正方形子矩阵
    • 思路
    • 代码
  • 分割回文串 III
    • 思路
    • 代码

找出井字棋的获胜者

思路

模拟。

代码

class Solution {
public:

    char mp[4][4];

    bool check(int x, int y, char ch) {
        int flag = 1;
        for(int i = 0; i < 3; ++i) {
            if(mp[x][i] != ch) flag = 0;
        }
        if(flag) return true;
        flag = 1;
        for(int i = 0; i < 3; ++i) {
            if(mp[i][y] != ch) flag = 0;
        }
        if(flag) return true;
        int cnt = 0;
        for(int i = -2; i < 3; ++i) {
            if(x + i >= 0 && x + i < 3 && y + i >= 0 && y + i < 3 && mp[x+i][y+i] == ch) ++cnt;
        }
        if(cnt == 3) return true;
        cnt = 0;
        for(int i = -2; i < 3; ++i) {
            if(x - i >= 0 && x - i < 3 && y + i >= 0 && y + i < 3 && mp[x-i][y+i] == ch) ++cnt;
        }
        return cnt == 3;
    }

    string tictactoe(vector<vector<int>>& moves) {
        int n = moves.size();
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) mp[i][j] = ' ';
        }
        for(int i = 0; i < n; ++i) {
            if(i & 1) mp[moves[i][0]][moves[i][1]] = 'O';
            else mp[moves[i][0]][moves[i][1]] = 'X';
        }
        int flag = 0;
        for(int i = 0; i < 3; ++i) {
            for(int j = 0; j < 3; ++j) {
                if(mp[i][j] != ' ' && check(i, j, mp[i][j])) {
                    if(mp[i][j] == 'X') return "A";
                    else if(mp[i][j] == 'O') return "B";
                }
                if(mp[i][j] == ' ') flag = 1;
            }
        }
        if(flag) return "Pending";
        else return "Draw";
    }
};

不浪费原料的汉堡制作方案

思路

一开始看到数据范围只有\(10^7\)然后直接枚举\(T\)了(可能是写搓了?),然后写了个二分。

二分有多少个小皇堡然后比较\(tomatoSlices\)使用数量。

代码

class Solution {
public:
    vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
        std::vector<int> v;
        int ub = cheeseSlices, lb = 0, mid, ans = -1;
        while(ub >= lb) {
            mid = (ub + lb) >> 1;
            if(mid * 2 + (cheeseSlices - mid) * 4 >= tomatoSlices) {
                lb = mid + 1;
                ans = mid;
            } else ub = mid - 1;
        }
        if(ans != -1 && ans * 2 + (cheeseSlices - ans) * 4 == tomatoSlices) {
            v.resize(2);
            v[0] = cheeseSlices - ans, v[1] = ans;
        }
        return v;
    }
};

统计全为 1 的正方形子矩阵

思路

二维前缀和然后枚举上下边界的左边界,看这个正方形内的\(1\)的个数。

代码

class Solution {
public:
    int countSquares(vector<vector<int>>& matrix) {
        int n = matrix.size(), m = matrix[0].size();
        int sum[305][305];
        for(int i = 0; i <= n; ++i) {
            for(int j = 0; j <= m; ++j) sum[i][j] = 0;
        }
        int ans = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < m; ++j) {
                sum[i+1][j+1] = sum[i][j+1] + sum[i+1][j] - sum[i][j] + matrix[i][j];
            }
        }
        for(int l = 1; l <= n; ++l) {
            for(int r = l; r <= n; ++r) {
                int len = r - l + 1;
                for(int j = 1; j + len - 1 <= m; ++j) {
                    if(sum[r][j+len-1] - sum[l-1][j+len-1] - sum[r][j-1] + sum[l-1][j-1] == len * len) ++ans;
                }
            }
        }
        return ans;
    }
};

分割回文串 III

思路

先预处理出以\(i\)为左端点,\(j\)为右端点的字符串变成回文串需要修改多少个位置。

然后进行\(dp\),\(dp[i][j]\)表示前\(i\)个字母构成\(j\)个回文串所需要修改的最少位置。

转移方程为\(dp[i][j]=min(dp[i][j],dp[lst-1][j-1]+change[lst][i])\)。

代码

class Solution {
public:
    const int inf = 0x3f3f3f3f;
    int palindromePartition(string s, int k) {
        int dp[105][105];
        memset(dp, inf, sizeof(dp));
        int n = s.length();
        for(int i = 0; i <= k; ++i) dp[0][i] = 0;
        int change[105][105];
        memset(change, 0, sizeof(change));
        for(int i = 0; i < n; ++i) {
            for(int j = i; j < n; ++j) {
                for(int st = i, ed = j; st <= ed; ++st, --ed) {
                    change[i][j] += (s[st] != s[ed]);
                }
            }
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 1; j <= min(i+1, k); ++j) {
                for(int lst = 1; lst <= i + 1; ++lst) {
                    dp[i+1][j] = min(dp[i+1][j], dp[lst-1][j-1] + change[lst-1][i]);
                }
            }
        }
        return dp[n][k];
    }
};

原文地址:https://www.cnblogs.com/Dillonh/p/11965567.html

时间: 2024-08-30 09:51:30

Leetcode周赛165的相关文章

LeetCode 第 165 场周赛

LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能说数据不充分 找出井字棋的获胜者4 题目描述 Description A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (" ") 上. 第一个玩家 A 总是用 "X" 作为棋子,而第二个玩家 B 总是用 "

[leetcode 周赛 157] 1217 玩筹码

1217 Play With Chips 玩筹码 题目描述 数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中. 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以): 将第 i 个筹码向左或者右移动 2 个单位,代价为 0. 将第 i 个筹码向左或者右移动 1 个单位,代价为 1. 最开始的时候,同一位置上也可能放着两个或者更多的筹码. 返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价. 示例 1: 输入:chips = [1,2,3] 输出:1 解释:

[leetcode 周赛 157] 1218 最长定差子序列

1218 Longest Arithmetic Subsequence of Given Difference 最长定差子序列 问题描述 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等差子序列的长度. 示例 1: 输入:arr = [1,2,3,4], difference = 1 输出:4 解释:最长的等差子序列是 [1,2,3,4]. 示例 2: 输入:arr = [1,3,

[leetcode 周赛 157] 1219 黄金矿工

1219 Path with Maximum Gold 黄金矿工 问题描述 你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 m * n 的网格 grid 进行了标注.每个单元格中的整数就表示这一单元格中的黄金数量:如果该单元格是空的,那么就是 0. 为了使收益最大化,矿工需要按以下规则来开采黄金: 每当矿工进入一个单元,就会收集该单元格中的所有黄金. 矿工每次可以从当前位置向上下左右四个方向走. 每个单元格只能被开采(进入)一次. 不得开采(进入)黄金数目为 0 的单元

[leetcode 周赛 159] 1233 删除子文件夹

1233 Remove Sub-Folders from the Filesystem 删除子文件夹 问题描述 你是一位系统管理员,手里有一份文件夹列表 folder,你的任务是要删除该列表中的所有 子文件夹,并以 任意顺序 返回剩下的文件夹. 我们这样定义「子文件夹」: 如果文件夹?folder[i]?位于另一个文件夹?folder[j]?下,那么?folder[i]?就是?folder[j]?的子文件夹. 文件夹的「路径」是由一个或多个按以下格式串联形成的字符串: /?后跟一个或者多个小写英

【一天一道LeetCode】#165. Compare Version Numbers.md

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/compare-version-numbers/ Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 <

【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]

【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653-656) 链接:https://leetcode.com/contest/leetcode-weekly-contest-44 比赛情况记录:就做出来两题,第三题不难,然而就是在算坐标的时候卡住了.orz.结果:2/4,ranking:637/2272.第四题没看题,第三题搞得心情不好了orz. [653]Two Sum

【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)

Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest-73 Contest 74 (2019年1月31日模拟) 链接:https://leetcode.com/contest/weekly-contest-74 Contest 75 (2019年1月31日模拟) 链接:https://leetcode.com/contest/weekly-conte