当前位置:网站首页>[summary of leetcode weekly competition] the 83rd fortnight competition of leetcode (7.23)
[summary of leetcode weekly competition] the 83rd fortnight competition of leetcode (7.23)
2022-07-28 09:36:00 【Let it beSun】
Topic 1 6128. The best poker hand
Ideas
Use array as hash table , Record decor and deck , Then judge from big to small according to the rules , Then directly return the string .
Code
class Solution {
public String bestHand(int[] ranks, char[] suits) {
int[] countSuits = new int[4];
int[] countRanks = new int[14];
for(char c:suits){
countSuits[c-'a']++;
}
for(int i=0;i<4;i++){
if(countSuits[i]==5){
return "Flush";
}
}
for(int i=0;i<5;i++){
countRanks[ranks[i]]++;
}
boolean flag = false;
for(int i=1;i<=13;i++){
if(countRanks[i]>=3){
return "Three of a Kind";
}
if(countRanks[i]==2){
flag = true;
}
}
if(flag){
return "Pair";
}else{
return "High Card";
}
}
}Topic two 6129. whole 0 The number of subarrays
Ideas
Count each one 0 The number of subarrays at the end of , Simultaneously count each 0 Continuous on the left 0 The number of left,
Every 0 The number of subarrays at the end of = left + 1
for example : 1 ,0 , 0, 0,1
count 1 1+1 1+2
Code
class Solution {
public long zeroFilledSubarray(int[] nums) {
long res = 0;
int left = 0;
for(int i=0;i<nums.length;i++){
if(nums[i]==0){
res += (1+left);
left++;
}else{
left = 0;
}
}
return res;
}
}Topic three 6130. Design digital container system
Ideas
According to the meaning , It needs to be found in a small time complexity num The corresponding minimum idx, I thought I could maintain two hash mappings , At first, I was wrong to use the linked list ,numToIdx It can be used TreeSet
TreeSet The bottom layer is TreeMap, Use red black tree to achieve , The time complexity of insert delete search is O(logn)
Map<Integer,TreeSet<Integer>> numToIdx;
Map<Integer,Integer> idxToNum;
void change(int index, int number) In subscript index Fill in number . If the subscript index There are already numbers at , Then use number Replace this number .
change The operation may modify three data structures ,
1、oldNumber If there is , Delete numToIdx in oldNumber Of idx
2、number Add numToIdx Medium idx( Pay attention to writing , Don't actually add it to the hash table )
3、 Update hash table idxToNum
int find(int number) Returns the given number number The minimum subscript in the system . If there is no number , Then the return -1 .
return -1 The situation of , May be number Not in the hash table , It could be number Corresponding set The size is 0.
Code
class NumberContainers {
Map<Integer,TreeSet<Integer>> numToIdx;
Map<Integer,Integer> idxToNum;
public NumberContainers() {
numToIdx = new HashMap<>();
idxToNum = new HashMap<>();
}
public void change(int index, int number) {
int oldNum = idxToNum.getOrDefault(index,-1); //idx The original num
idxToNum.put(index,number);
if(oldNum!=-1){
numToIdx.get(oldNum).remove(index);
}
TreeSet<Integer> tmp = numToIdx.getOrDefault(number,new TreeSet<Integer>());
tmp.add(index);
numToIdx.put(number,tmp);
}
public int find(int number) {
if(numToIdx.containsKey(number) && !numToIdx.get(number).isEmpty()){
return numToIdx.get(number).first();
}else{
return -1;
}
}
}边栏推荐
- 树上启发式合并
- Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline
- Oracle-11gr2 default system job
- MQTT. JS introductory tutorial: learning notes
- 2022 supplementary questions for the first session of Niuke multi school
- QT基础练手小程序-简单计算器设计(附带源码,解析)
- [multithreading] non atomic agreement of long and double
- 21 day learning challenge - "AUTOSAR from introduction to mastery - practical part"
- MySQL 8.0.30 GA
- MATLAB的数列与极限运算
猜你喜欢

2022高压电工考试模拟100题及模拟考试
![[Guangxi University] information sharing of postgraduate entrance examination and re examination](/img/25/e35de6b9d803c9a80e0d2816aaad87.jpg)
[Guangxi University] information sharing of postgraduate entrance examination and re examination

2022 safety officer-b certificate examination simulated 100 questions and answers
![376. Swing sequence [greedy, dynamic planning -----]](/img/c3/46cdd8c9320c529171cbf963c768a7.png)
376. Swing sequence [greedy, dynamic planning -----]

MySQL中各类型文件详解
![[swintransformer source code reading II] window attention and shifted window attention](/img/fb/5273d87fed66c75a92aec8e94980a3.png)
[swintransformer source code reading II] window attention and shifted window attention

Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server

译文推荐 | 调试 BookKeeper 协议 - 无界 Ledger

ShardingSphere简介(一)

Personal blog applet
随机推荐
golang升级到1.18.4版本 遇到的问题
How promise instance solves hell callback
What is cross domain? How to solve the cross domain problem?
Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit
[high number] high number plane solid geometry
Detailed introduction of v-bind instruction
JDBC connection database
51 single chip microcomputer storage: EEPROM (I2C)
19c sysaux tablespace sqlobj$plan table is too large. How to clean it up
【多线程】println方法底层原理
Express builds a simple local background (1)
What is the difference between these two sets of code?
Salted fish esp32 instance - mqtt lit LED
[JVM] JVM refers to floating point number
[one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
[package deployment]
DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
Leetcode 452. minimum number of arrows to burst balloons (medium)
Talk to the father of MySQL: code completion at one time is a good programmer
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符