当前位置:网站首页>365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
365 day challenge leetcode 1000 questions - day 035 one question per day + two point search 13
2022-07-29 05:37:00 【ShowM3TheCode】
List of articles
1260. Two dimensional grid migration
First brush self solution
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;
}
};
Algorithm complexity : O ( i ∗ m ∗ n ) O(i * m * n) O(i∗m∗n), among i yes k modulus m*n
A better method is one-dimensional expansion , Calculate the next location of the element
528. Choose randomly by weight
First brush self solution
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(); */
Algorithm complexity : O ( n + k ∗ l o g ( n ) ) O(n + k * log(n)) O(n+k∗log(n)),k Is to call pickIndex() The number of times
边栏推荐
- 【C语言系列】— 字符串+部分转义字符详解+注释小技巧
- Day 3
- Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
- 弹性盒子相关知识
- link与@import导入外部样式的区别
- [C language series] - storage of deep anatomical data in memory (II) - floating point type
- Day 1
- Allocate memory: malloc() and free()
- AR虚拟增强与现实
- 组件传参与生命周期
猜你喜欢
[C language series] - a recursive topic
用threejs 技术做游戏跑酷
Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
Occt learning 002 - environment construction
Alibaba cloud architect details nine trends in the game industry
Realize simple database query (incomplete)
Li Kou 994: rotten orange (BFS)
【TypeScript】深入学习TypeScript函数
浅谈范式
随机推荐
Occt learning 003 - MFC single document project
Day 5
弹性盒子相关知识
Camunda 1. Camunda workflow - Introduction
【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词
TXT 纯文本操作
Day 3
用sql-client.sh生成的job在cancle过后 如何实现断点续传?
Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
paddle.fluild常量计算报错‘NoneType‘ object has no attribute ‘get_fetch_list‘
redis的基本使用
ClickHouse学习(九)clickhouse整合mysql
力扣994:腐烂的橘子(BFS)
全局components组件注册
MySQL解压版windows安装
移动端-flex项目属性
[C language series] - detailed explanation of file operation (Part 1)
Allocate memory: malloc() and free()
HCIA-R&S自用笔记(25)NAT技术背景、NAT类型及配置
Topological ordering of a graph of water