当前位置:网站首页>365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
2022-07-29 05:08:00 【ShowM3TheCode】
文章目录
1260. 二维网格迁移
首刷自解
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int m = grid.size();
int n = grid[0].size();
k %= m * n;
for (int i = 0; i < k; i++) {
int tmp = grid[m - 1][n - 1];
for (int j = m - 1; j >= 0; j--) {
for (int k = n - 1; k >= 0; k--) {
if (j == 0 && k == 0) break;
if (k == 0) grid[j][k] = grid[j - 1][n - 1];
else grid[j][k] = grid[j][k - 1];
}
}
grid[0][0] = tmp;
}
return grid;
}
};
算法复杂度: O ( i ∗ m ∗ n ) O(i * m * n) O(i∗m∗n), 其中i是k取模m*n
更好的方法是一维展开,算出元素下一个要去的位置
528. 按权重随机选择
首刷自解
class Solution {
public:
int sums;
vector<int> prefix;
Solution(vector<int>& w) {
sums = 0;
for (const auto& weight : w) {
sums += weight;
prefix.push_back(sums);
}
}
int pickIndex() {
int random = rand() % sums + 1;
auto it = lower_bound(prefix.begin(), prefix.end(), random);
return it - prefix.begin();
}
};
/** * Your Solution object will be instantiated and called as such: * Solution* obj = new Solution(w); * int param_1 = obj->pickIndex(); */
算法复杂度: O ( n + k ∗ l o g ( n ) ) O(n + k * log(n)) O(n+k∗log(n)),k是调用pickIndex()的次数
边栏推荐
- Handwritten student management system
- How to add a map to the legendary server
- ARFoundation从零开始3-创建ARFoundation项目
- 小鲁客栈---预告篇
- osg进阶-序
- 【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
- On AspectJ framework
- VirtualBox has expanded the capacity of virtual hard disk (without modifying the original data)
- Xiaobai high salary shortcut Qt development game Snake
- Helm chart for Kubernetes
猜你喜欢
传奇开区网站如何添加流量统计代码
[from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
On AspectJ framework
365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
ARFoundation从零开始5-AR图像跟踪
直播预告:京东云DevOps与JFrog制品库的融合
200 多家 ISV 入驻!阿里云计算巢发布一周年
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
Qml类型:MouseArea
Arfoundation starts from zero 9-ar anchor
随机推荐
Qml类型:State 状态
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
小白高薪捷径-Qt开发游戏—贪吃蛇
The method and detailed code of automatically pop-up and QQ group when players visit the website
CryEngine5 Shader调试
OCCT学习003-----MFC单文档工程
QT series - Installation
QT学习:使用JSON/XML等非ts文件实现多语言国际化
C语言函数实现输出I love you
Apache POI实现Excel导入读取数据和写入数据并导出
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Teardown 解除时间限制的方法
京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
手写学生管理系统
ARFoundation从零开始5-AR图像跟踪
Helm chart for Kubernetes
7.2-function-overloading
Getting started with solidity
ARFoundation从零开始9-AR锚点(AR Anchor)
SM integration is as simple as before, and the steps are clear (detailed)