当前位置:网站首页>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
边栏推荐
- B - identify floating point constant problems
- uniapp页面标题显示效果
- 【TypeScript】深入学习TypeScript函数
- Alibaba cloud architect details nine trends in the game industry
- Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
- Detailed explanation of GPIO input and output
- Day 3
- 微信小程序更改属性值-setData-双向绑定-model
- ClickHouse学习(五)集群操作
- link与@import的关系
猜你喜欢
![[C language series] - realize the exchange of two numbers without creating the third variable](/img/7c/468000bcbf740c9dd3535f2734728a.png)
[C language series] - realize the exchange of two numbers without creating the third variable

ClickHouse学习(十)监控运行指标

Three handshakes and four waves for the interview summary

·Let's introduce ourselves to the way of programming·

【TypeScript】TypeScript中类型缩小(含类型保护)与类型谓词

ClickHouse学习(五)集群操作

Longest string without duplicate characters

Day 5

Clickhouse learning (VI) grammar optimization

【TypeScript】深入学习TypeScript函数
随机推荐
HCIA-R&S自用笔记(26)PPP
Li Kou 994: rotten orange (BFS)
Introduction to array learning simple question sum of two numbers
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
力扣994:腐烂的橘子(BFS)
Day 3
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
关于局部变量
[C language series] - detailed explanation of file operation (Part 1)
【C语言系列】— 不创造第三个变量,实现两个数的交换
虚拟增强与现实第二篇 (我是一只火鸟)
B - identify floating point constant problems
Cryengine5 shader debugging
个人学习笔记
shell基本操作(上)
第三课threejs全景预览房间案例
MySQL解压版windows安装
uniapp之常用提示弹框
C language file operation