当前位置:网站首页>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
边栏推荐
- ClickHouse学习(八)物化视图
- Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"
- 微信小程序更改属性值-setData-双向绑定-model
- Day 2
- C language first level pointer
- 【C语言系列】— 字符串+部分转义字符详解+注释小技巧
- Integer overflow and printing
- 【C语言系列】— 不创造第三个变量,实现两个数的交换
- H5语义化标签
- Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
猜你喜欢

shell基本操作(上)

【JS题解】牛客网JS篇1-10题

Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system

【剑指offer】— 详解库函数atoi以及模拟实现atoi函数

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"

ClickHouse学习(十一)clickhouseAPI操作

【C语言系列】— 打印100~200之间的素数
![[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function](/img/13/a506861da2db8f5a5181e6d82894b3.png)
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function

无重复字符的最长字串

Day 2
随机推荐
虚拟增强与现实第二篇 (我是一只火鸟)
Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system
Storage category
全局components组件注册
【JS题解】牛客网JS篇1-10题
Li Kou 994: rotten orange (BFS)
Day 3
paddle.fluild常量计算报错‘NoneType‘ object has no attribute ‘get_fetch_list‘
Occt learning 003 - MFC single document project
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
浅谈Servlet
TXT 纯文本操作
Do students in the science class really understand the future career planning?
[C language series] - storage of deep anatomical data in memory (II) - floating point type
Question swiping Madness - leetcode's sword finger offer58 - ii Detailed explanation of left rotation string
[C language series] - realize the exchange of two numbers without creating the third variable
link与@import导入外部样式的区别
Application of Huffman tree and Huffman coding in file compression
H5语义化标签
uniapp组件之选择选项(如套餐选择)