【每日算法Day 63】LeetCode 第 179 场周赛题解

简介: 生成每种字符都是奇数个的字符串

LeetCode 5352. 生成每种字符都是奇数个的字符串


题目链接


https://leetcode-cn.com/problems/generate-a-string-with-characters-that-have-odd-counts/

题解


image.png

代码(python)

classSolution:  
defgenerateTheString(self, n: int) ->str:    
ifn%2==0:    
return"a"+"b"*(n-1)    
return"a"*n

LeetCode 5353. 灯泡开关 III

题目链接


https://leetcode-cn.com/problems/bulb-switcher-iii/

题解


如果某一个时刻灯都是蓝色的,等价于所有的亮灯都连续排列在数组最左边,没有间断。所以只需要判断当前时刻亮灯的最大编号是否等于亮灯的数量就行了。

比赛的时候傻 x 了,第一个想到的竟然是树状数组,于是直接把模板套过来过了。

代码(c++)

classSolution {
public:  
intnumTimesAllBlue(vector<int>&light) { 
intres=0, maxx=0; 
for (inti=0, sz=light.size(); i<sz; ++i)
                    {        
maxx=max(maxx, light[i]);     
if (maxx==i+1) res++;  
                    }      
returnres; 
                }
};

树状数组:

classSolution {
public: 
staticconstintMAXN=50010; 
intbit[MAXN];     
intnumTimesAllBlue(vector<int>&light)
    {  
memset(bit, 0, sizeofbit);  
intmaxx=0, res=0;    
for (inti=0, sz=light.size(); i<sz; ++i)
        {  
add(light[i], 1);   
maxx=max(maxx, light[i]);  
if (sum(maxx) ==maxx) res++;  
        }       
returnres;   
    }    
intlowbit(intx) 
    { 
returnx&(-x); 
    }
voidadd(inti, intx)
    {   
while (i<MAXN)
        {      
bit[i] +=x;     
i+=lowbit(i);    
        }   
    }
voidsub(inti, intx)
    {   
while (i<MAXN) 
        {    
bit[i] -=x;   
i+=lowbit(i);     
        }  
    }
intsum(inti) {   
ints=0;    
while (i>0)
        {  
s+=bit[i];      
i-=lowbit(i);  
        }        
returns;  
    }
};

LeetCode 5354. 通知所有员工所需的时间


题目链接


https://leetcode-cn.com/problems/time-needed-to-inform-all-employees/

题解


image.png

代码(c++)

classSolution {
public:  
staticconstintN=100010; 
vector<int>G[N];   
intnumOfMinutes(intn, intheadID, vector<int>&manager, 
vector<int>&informTime) {   
for (inti=0; i<n; ++i) 
        {    
if (manager[i] !=-1) 
            {   
G[manager[i]].push_back(i);    
            }    
        }      
returnf(headID, informTime); 
    }       
intf(intheadID, vector<int>&informTime)
    {        if (!informTime[headID]) return0;  
intmaxx=0;   
for (inti=0, sz=G[headID].size(); i<sz; ++i)
     {       
maxx=max(maxx, f(G[headID][i], informTime));  
     }   
returnmaxx+informTime[headID]; 
    }
};

LeetCode 5355. T 秒后青蛙的位置


题目链接

https://leetcode-cn.com/problems/frog-position-after-t-seconds/

题解


image.png

代码(c++)


classSolution {
public:   
doublefrogPosition(intn, vector<vector<int>>&edges, intt, 
inttarget) {  
if (n==1) return1.0;    
vector<vector<int>>G(110); 
for (inti=0; i<n-1; ++i) 
        {            intu=edges[i][0], v=edges[i][1];   
G[u].push_back(v);   
G[v].push_back(u);   
        }       
returndfs(1, 0, t, target, G); 
    }       
doubledfs(intu, intfa, intt, inttarget,
vector<vector<int>>&G) {   
intsz=G[u].size();   
if (!t|| (fa&&sz==1)) {   
if (u==target) return1;  
elsereturn0;     
        }         
doublep=1.0/ (fa?sz-1 : sz), maxx=0;
for (inti=0, sz=G[u].size(); i<sz; ++i) {  
intv=G[u][i];     
if (v==fa) continue;   
maxx=max(maxx, dfs(v, u, t-1, target, G)); 
        }      
returnp*maxx;  
    }
};

image.png

作者简介:godweiyang知乎同名华东师范大学计算机系硕士在读,方向自然语言处理与深度学习喜欢与人分享技术与知识,期待与你的进一步交流~

相关文章
|
3天前
|
存储 算法 JavaScript
怎么刷算法,leetcode上有哪些经典题目
怎么刷算法,leetcode上有哪些经典题目
18 0
|
3天前
|
算法 Java
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
27 1
|
3天前
|
算法 Java
[Java·算法·简单] LeetCode 13. 罗马数字转整数 详细解读
[Java·算法·简单] LeetCode 13. 罗马数字转整数 详细解读
25 0
|
3天前
|
算法 Java
[Java·算法·简单] LeetCode 392. 判断子序列 详细解读
[Java·算法·简单] LeetCode 392. 判断子序列 详细解读
41 0
|
3天前
|
存储 canal 算法
[Java·算法·简单] LeetCode 125. 验证回文串 详细解读
[Java·算法·简单] LeetCode 125. 验证回文串 详细解读
29 0
|
3天前
|
算法 Java
[Java·算法·中等] LeetCode15. 三数之和
[Java·算法·中等] LeetCode15. 三数之和
35 0
|
3天前
|
存储
Leetcode第382场周赛
```markdown 给定字符串`s`,计算按键变更次数,即使用不同键的次数,不考虑大小写差异。例如,`&quot;aAbBcC&quot;`变更了2次。函数`countKeyChanges`实现此功能。另外,求满足特定模式子集最大元素数,`maximumLength`函数使用`TreeMap`统计次数,枚举并构建子集,返回最大长度。最后,Alice和Bob玩鲜花游戏,Alice要赢需满足鲜花总数奇数、顺时针在[1,n]、逆时针在[1,m],返回满足条件的(x, y)对数,可通过奇偶性分类讨论求解。 ```
13 1
|
3天前
|
存储
Leetcode第383场周赛
在LeetCode第383场周赛中,选手完成了3道题目。第一题是关于边界上的蚂蚁,蚂蚁根据非零整数数组nums的值移动,返回蚂蚁返回边界上的次数。解题方法是计算数组累加和为0的次数。第二题涉及计算网格的区域平均强度,给定一个灰度图像和阈值,返回每个像素所属区域的平均强度。解题关键在于理解相邻像素和区域定义,并计算平均强度。第三题是恢复单词初始状态的最短时间问题,通过移除前k个字符并添加k个字符,求恢复原词所需的最短时间。解题策略是检查去除前k个字符后的子串是否能作为原词的前缀。
12 1
Leetcode第383场周赛
|
3天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
22 0
|
3天前
|
算法
代码随想录算法训练营第六十天 | LeetCode 84. 柱状图中最大的矩形
代码随想录算法训练营第六十天 | LeetCode 84. 柱状图中最大的矩形
23 3