当前位置:网站首页>May brush question 01 - array
May brush question 01 - array
2022-07-06 09:38:00 【A Guang chasing dreams】
May brush questions 01—— Array
Today's brush topic content : Array
Preface
- An algorithm opens the way to brush the questions of waste materials , Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- New column , Try to work out at least one question every day =v=
- Language java、python、c\c++
One 、 Today's topic
- 2016. The maximum difference between incremental elements
- 2239. Find the closest to 0 The number of
- 1475. The final price after discount
- 2248. Intersection of multiple arrays
Two 、 Their thinking
1. 2016. The maximum difference between incremental elements
- According to the requirements of the topic is to find the minimum value of the preceding subscript and the maximum value of the difference between the following subscript
- Use a variable to record the minimum value of the preceding item
- Maintain the minimum value variable and update the difference
class Solution {
public int maximumDifference(int[] nums) {
int i = 0;
int j = 1;
int n = nums.length;
int ret = - 1;
int pre = nums[0];
while(j < n){
if(nums[j] > pre){
ret = Math.max(ret, nums[j] - pre);
}else{
pre = nums[j];
}
j++;
}
return ret;
}
}
2. 2239. Find the closest to 0 The number of
- To find the closest 0 Number of numbers , Is to find the number with the smallest absolute value
- When the absolute value is the same, take a larger number
- Use two variables to maintain absolute value and true value
class Solution {
public int findClosestNumber(int[] nums) {
int real = nums[0];
int absval = Math.abs(nums[0]);
for (int n: nums){
int x = Math.abs(n);
// The absolute value of the current number is smaller than the recorded value
if (x < absval){
absval = x;
real = n;
}
// The absolute value of the current number is the same
else if(x == absval){
real = real > n ? real: n; // The truth value takes the greater value
}
}
return real;
}
}
3. 1475. The final price after discount
- The value in front of the array subscript can be deducted by the smaller value in the back
- Just traverse the array , Get the smaller value after the array
- Just update the array
class Solution {
public int[] finalPrices(int[] prices) {
int i, j;
int n = prices.length;
for(i = 0; i < n; i++){
for (j = i+1; j < n; j++){
if (prices[j] <= prices[i]){
prices[i] -= prices[j];
break;
}
}
}
return prices;
}
}
4. 2248. Intersection of multiple arrays
- Because the range of numbers is not large , So we can use
hash
Table to do questions- Count the number of all numbers in each array
- If the number of numbers is the same as the number of arrays, it means that this number is a subset of the intersection
class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
length = len(nums)
ret = [];
hash_tb = [0] * 1001
for num in nums:
for n in num:
hash_tb[n] += 1
for idx, val in enumerate(hash_tb):
if val == length:
ret.append(idx)
return ret
边栏推荐
- 018. Valid palindromes
- Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
- Kratos战神微服务框架(一)
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- The carousel component of ant design calls prev and next methods in TS (typescript) environment
- Mapreduce实例(七):单表join
- [deep learning] semantic segmentation: paper reading: (2021-12) mask2former
- [daily question] Porter (DFS / DP)
- Appears when importing MySQL
- Redis cluster
猜你喜欢
Redis geospatial
Hard core! One configuration center for 8 classes!
Workflow - activiti7 environment setup
Compilation of libwebsocket
Sentinel mode of redis
【深度學習】語義分割-源代碼匯總
[deep learning] semantic segmentation - source code summary
MapReduce instance (IV): natural sorting
leetcode-14. Longest common prefix JS longitudinal scanning method
QML type: locale, date
随机推荐
Redis之五大基础数据结构深入、应用场景
The five basic data structures of redis are in-depth and application scenarios
Heap (priority queue) topic
Redis之发布订阅
[three storage methods of graph] just use adjacency matrix to go out
Master slave replication of redis
Kratos战神微服务框架(一)
CAP理论
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
IDS cache preheating, avalanche, penetration
[deep learning] semantic segmentation - source code summary
Redis之持久化实操(Linux版)
What are the models of data modeling
工作流—activiti7环境搭建
Solve the problem of too many small files
Redis之哨兵模式
Withdrawal of wechat applet (enterprise payment to change)
Redis core configuration
有软件负载均衡,也有硬件负载均衡,选择哪个?
Lua script of redis