leetcode

Leetcode 1189:“气球”的最大数量

这一生的挚爱 提交于 2019-11-29 19:30:12
题目描述 给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球)。 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon"。 示例 1: 输入:text = "nlaebolko" 输出:1 示例 2: 输入:text = "loonbalxballpoon" 输出:2 示例 3: 输入:text = "leetcode" 输出:0 提示: 1 <= text.length <= 10^4 text 全部由小写英文字母组成 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-number-of-balloons 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解题思路 class Solution { public: int maxNumberOfBalloons(string text) { unordered_map<char,int> mp; for(auto it : text){ if(it=='a' || it=='b'||it=='l'||it=='o'||it=='n') mp[it]++; } int s = min(mp['a'],min(mp['b'],mp['n'])),d

leetcode 5189. “气球” 的最大数量(C++、python)

人盡茶涼 提交于 2019-11-29 18:40:47
给你一个字符串 text ,你需要使用 text 中的字母来拼凑尽可能多的单词 "balloon"(气球) 。 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 "balloon" 。 示例 1: 输入: text = "nlaebolko" 输出: 1 示例 2: 输入: text = "loonbalxballpoon" 输出: 2 示例 3: 输入: text = "leetcode" 输出: 0 提示: 1 <= text.length <= 10^4 text 全部由小写英文字母组成 C++ class Solution { public: int maxNumberOfBalloons(string text) { map<char,int> tmp; for(auto c:text) { tmp[c]++; } int res=min(tmp['a'],tmp['b']); res=min(res,tmp['n']); int res2=min(tmp['l']/2,tmp['o']/2); return min(res,res2); } }; python class Solution: def maxNumberOfBalloons(self, text: str) -> int: tmp=['a','b','l','o','n'

LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)

谁说我不能喝 提交于 2019-11-29 18:03:20
题目标签:Dynamic Programming   题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发。   因为每次可以选择走一步,还是走两步,这里用 dynamic, 从index 2 (第三格楼梯开始) 计算每一个楼梯,到达需要用的最小cost。   在每一个楼梯,只需要计算 从前面2格楼梯过来的cost, 和 从前面1格楼梯过来的 cost,哪个小。就选哪个叠加自己的cost。最后 index = len 的地方就是走到top 所用的最小cost。 Java Solution: Runtime: 1 ms, faster than 99.86% Memory Usage: 37.9 MB, less than 92.86 % 完成日期:08/15/2019 关键点:dynamic programming class Solution { public int minCostClimbingStairs(int[] cost) { int len = cost.length; int [] mc = new int[len + 1]; // base cases mc[0] = cost[0]; mc[1] = cost[1]; for(int i = 2; i <= len; i++) { int c = i =

LeetCode 1103. Distribute Candies to People (分糖果 II)

岁酱吖の 提交于 2019-11-29 18:01:35
题目标签:Math   题目让我们分发糖果,分的糖果从1 开始依次增加,直到分完。   for loop可以计数糖果的数量,直到糖果发完。但是还是要遍历array 给people 发糖,这里要用到 index = (本轮分糖果的量 % people 的人数)糖果的数量从0 开始计数,这样的话,index 就会一直重复遍历 array,具体看code。 Java Solution: Runtime: 1ms, faster than 90.53% Memory Usage: 33.8 MB, less than 100.00 % 完成日期:07/15/2019 关键点:利用%重复遍历array class Solution { public int[] distributeCandies(int candies, int num_people) { int[] people = new int[num_people]; for(int give = 0; candies > 0; candies -= give) { people[give % num_people] += Math.min(candies, ++give); } return people; } } 参考资料:LeetCode discuss LeetCode 题目列表 - LeetCode Questions

零基础一年拿下BAT三家offer

孤街浪徒 提交于 2019-11-29 17:17:09
背景 1、本人本科一本双非垫底的那种,硕士211。本硕电子通信,完全0基础,转行一年。 2、研一上第一学期上课+外派到老师合作公司写MATLAB。去年4月开始学习Java。 起步 1、实话说,刚决定转行的时候完全零基础一开始真的啥也不会,甚至不知道怎么去学习。的确,计算机资源很多,自学足够的但是完全不知道怎么去用去学习啊! 啥是leetcode? 啥是github? 啥是IDEA? 啥是牛客网? 各种疑问,怎么操作啊?点哪个啊?身边没有人教啊。 有时候真的只是懂的人点两下的事情,自己搞要一下午。妈个鸡,第一个月真是完全浪费时间,差点原地放弃。 过程 1、战略上坚定信心: 既然那么多人都可以做程序员,甚至高中生都可以,别人可以我肯定也可以。 2、战术上制定计划: 培训班看视频我也看视频咯,对,就是各种视频。(现在想想自己甚至就是培训班出来的) 视频的好处是快速入门,犹如身边有老师手把手教,一步一步操作给你看。很重要一点,培训班老师上课非常认真,真的是面向高中生水平讲课。完全不用担心有卡顿,看不懂。记得某门课老师还在讲啥是二进制和排列组合。。 Java入门(4-7月): 我在 阿里云大学 上找了 李兴华老师 的Java就业班,看了部分,走完了Java SE,Java EE,MYSQL,SSM,Git的使用,跟着敲一遍,耗时大概3个月时间。学会了Java基本语法

LeetCode 第 154 场周赛 【“气球” 的最大数量】【一周中的第几天】【K 次串联后最大子数组之和】

旧时模样 提交于 2019-11-29 16:28:50
5189. “气球” 的最大数量 给你一个字符串 text,你需要使用 text 中的字母来拼凑尽可能多的单词 “balloon”(气球)。 字符串 text 中的每个字母最多只能被使用一次。请你返回最多可以拼凑出多少个单词 “balloon”。 示例 1: 输入:text = "nlaebolko" 输出:1 示例 2: 输入:text = "loonbalxballpoon" 输出:2 示例 3: 输入:text = "leetcode" 输出:0 代码: public int maxNumberOfBalloons ( String text ) { HashMap < Character , Integer > map = new HashMap < > ( ) ; // balloon map . put ( 'b' , 0 ) ; map . put ( 'a' , 0 ) ; map . put ( 'l' , 0 ) ; map . put ( 'o' , 0 ) ; map . put ( 'n' , 0 ) ; for ( int i = 0 ; i < text . length ( ) ; i ++ ) { char key = text . charAt ( i ) ; if ( map . containsKey ( key ) ) { map .

[LeetCode] 834. Sum of Distances in Tree 树中距离之和

烂漫一生 提交于 2019-11-29 15:08:13
An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The i th edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans , where ans[i] is the sum of the distances between node i and all other nodes. Example 1: Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]] Output: [8,12,6,10,10,10] Explanation: Here is a diagram of the given tree: 0 /\ 1 2 /|\ 3 4 5 We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5) equals 1 + 1 + 2 + 2 + 2 = 8. Hence, answer[0] = 8, and so on. Github 同步地址: https://github.com/grandyang/leetcode

LeetCode实战:存在重复元素

时光毁灭记忆、已成空白 提交于 2019-11-29 14:24:42
背景 为什么你要加入一个技术团队? 如何加入 LSGO 软件技术团队? 我是如何组织“算法刻意练习活动”的? 为什么要求团队的学生们写技术Blog 题目英文 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1 : Input : [ 1 , 2 , 3 , 1 ] Output : true Example 2 : Input : [ 1 , 2 , 3 , 4 ] Output : false Example 3 : Input : [ 1 , 1 , 1 , 3 , 3 , 4 , 3 , 2 , 4 , 2 ] Output : true 题目中文 给定一个整数数组,判断是否存在重复元素。 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 1 : 输入 : [ 1 , 2 , 3 , 1 ] 输出 : true 示例 2 : 输入 :

LeetCode943.最短超级串

旧巷老猫 提交于 2019-11-29 11:57:34
给定一个字符串数组 A,找到以 A 中每个字符串作为子字符串的最短字符串。 我们可以假设 A 中没有字符串是 A 中另一个字符串的子字符串。 示例 1: 输入:[“alex”,“loves”,“leetcode”] 输出:“alexlovesleetcode” 解释:“alex”,“loves”,“leetcode” 的所有排列都会被接受。 示例 2: 输入:[“catg”,“ctaagt”,“gcta”,“ttca”,“atgcatc”] 输出:“gctaagttcatgcatc” 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/find-the-shortest-superstring 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 class Solution { int get_dp(vector<string> &A, int a, int b){//单词i与单词j重复个数(有序) if(A[a].length()<1 || A[b].length()<1) return 0; int t=0; for(int i=0;i<A[a].length();i++){ if(A[a][i]==A[b][t]){ t++; } else{ i=i-t; t=0; } } return t; } void

leetcode中完全背包问题集合

谁都会走 提交于 2019-11-29 11:18:38
这两天在刷leetcode的动态规划的题目时,发现很多动态规划的题目怎么想都很难出递推公式,而看答案往往都感觉是精巧设计的,但是遇到类似的题目又不知从何下手,看了一天的博客和其他资料,发现这种类型的题目都是一类经典问题的变种:背包问题 背包问题主要有3种基本类型:01背包,完全背包,多重背包问题。 这里列出几个写的比较好的博客:大家可先行补充下基本知识(主要是leetcode中的题目不会让人立刻联想到是背包问题,所以看基本概念的目的是深入熟悉和记住递推公式的模板,方便套用)。 背包问题九讲 动态规划解决01背包问题 本文是总结leetcode中的背包变形问题的集锦,所以会不断更新,首先来看困扰我的leetcode中的二维递推公式的都长什么样: for (int i = 1; i <= m; i++) { for (int j = 0; j < n; j++) { dp[i] = min(dp[i], dp[i - coins[j]] + 1); } } 注意看红字所示,递推公式中会减去外层循环中的某个值,而不是我们常见的如下模板: for (int i = 1; i <= m; i++) { for (int j = 0; j < n; j++) { dp[i] = min( dp[i-1]+A[i] , dp[i]); } } 这种反减的模板套路就和背包问题很像