605. Can Place Flowers/611. Valid Triangle Number/575. Distribute Candies
来源:互联网 发布:网络平台开发预算 编辑:程序博客网 时间:2024/06/03 16:40
- Can Place Flowers
- description
- implementation
- Valid Triangle Number
- description
- implementation
- Distribute Candies
- description
- implementation
605. Can Place Flowers
description
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.Example 1:Input: flowerbed = [1,0,0,0,1], n = 1Output: TrueExample 2:Input: flowerbed = [1,0,0,0,1], n = 2Output: FalseNote:The input array won't violate no-adjacent-flowers rule.The input array size is in the range of [1, 20000].n is a non-negative integer which won't exceed the input array size.
implementation
This problem can be solved by simple logic with O(n) solution.
- Set flag = true;
- Go through the vector
- if num[idx] is 0, and next is 0 or last index, n–
- else flag = false
- if n <= 0 return true
- if n > (remain number + 1 >> 1)
- return false
class Solution {public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int num = flowerbed.size(); bool isInterval = true; if(!n) return true; if(n > ((num+1)>>1)) return false; for(int idx = 0; idx < num; idx++) { if(!flowerbed[idx]) { if(isInterval) { if(idx == num - 1 || !flowerbed[idx+1]) { n--; isInterval = false; } } else isInterval = true; } else isInterval = false; if(n <= 0) return true; if(n > ((num - idx + 1)>>1)) return false; } return false; }};
611. Valid Triangle Number
description
implementation
1st solution with time exceeded with backtracking.
class Solution {public: inline bool isTriangle(vector<int> line, int a) { return line[0] + line[1] > a; } void findTrangleNumber(vector<int>& nums, vector<int>& tmp, int rem, int& res, int stt) { /* if(rem == 0) { if(isTriangle(tmp)) res++; return; } */ for(int idx = stt; idx < nums.size(); idx++) { if(!(rem-1)) { if(isTriangle(tmp, nums[idx])) res++; else return;; } else { tmp.push_back(nums[idx]); findTrangleNumber(nums, tmp, rem - 1, res, idx + 1); tmp.pop_back(); } } } int triangleNumber(vector<int>& nums) { int size = nums.size(); int res = 0; if(size < 3) return res; int rem = 3; int stt = 0; vector<int> tmp; sort(nums.begin(), nums.end()); findTrangleNumber(nums, tmp, rem, res, stt); return res; }};
Optimize the 1st solution and get with DFS:
class Solution {public: inline bool isTriangle(vector<int> line) { return line[0] + line[1] > line[2]; } void findTrangleNumber(vector<int> nums, vector<int> tmp, int rem, int& res, int stt) { if(rem == 0) { if(isTriangle(tmp)) res++; return; } for(int idx = stt; idx < nums.size(); idx++) { tmp[3 - rem] = nums[idx]; findTrangleNumber(nums, tmp, rem - 1, res, idx + 1); } } int triangleNumber(vector<int>& nums) { int size = nums.size(); int res = 0; if(size < 3) return res; int rem = 3; int stt = 0; vector<int> tmp(3, 0); sort(nums.begin(), nums.end()); findTrangleNumber(nums, tmp, rem, res, stt); return res; }};
The Backtracking method or DFS are low efficient method, changing it to iterative method and get accept.
class Solution {public: int triangleNumber(vector<int>& nums) { int size = nums.size(); int res = 0; if(size < 3) return res; int rem = 3; int stt = 0; vector<int> tmp(3, 0); sort(nums.begin(), nums.end()); for(int idx1 = 0; idx1 < size - 2; idx1++) for(int idx2 = idx1+1; idx2 < size - 1; idx2++) for(int idx3 = idx2+1; idx3 < size; idx3++) { if(nums[idx1] + nums[idx2] > nums[idx3]) res++; else break; } return res; }};
575. Distribute Candies
description
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.
Example 1:Input: candies = [1,1,2,2,3,3]Output: 3Explanation:There are three different kinds of candies (1, 2 and 3), and two candies for each kind.Optimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. The sister has three different kinds of candies. Example 2:Input: candies = [1,1,2,3]Output: 2Explanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. The sister has two different kinds of candies, the brother has only one kind of candies.
implementation
This is an easy problem.
We just need find the kinds of total candies K.
If K is larger than half of candies number, we just can get half of it. Otherwise we can get the most kinds of candies.
class Solution {public: int distributeCandies(vector<int>& candies) { map<int, int> candi; int size = candies.size(); for(int idx = 0; idx < size; idx++) { if(candi[candies[idx]] > 0) candi[candies[idx]]++; else candi[candies[idx]] = 1; } return candi.size() >= (size >> 1) ? (size >> 1):candi.size(); }};
- 605. Can Place Flowers/611. Valid Triangle Number/575. Distribute Candies
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- 605. Can Place Flowers
- [leetcode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- [LeetCode]605. Can Place Flowers
- LeetCode 605.Can Place Flowers
- [leetcode]: 605. Can Place Flowers
- [LeetCode] 605. Can Place Flowers
- LeetCode: 605. Can Place Flowers
- 全局与局部变量
- Elasticsearch的简单介绍
- 考研党 数据结构(严蔚敏)Stack基本操作(纯C语言,与教材一致)
- vueJs第二讲学习笔记
- Linux中poll实现高性能服务器及优缺点总结
- 605. Can Place Flowers/611. Valid Triangle Number/575. Distribute Candies
- QT自定义Action的信号和槽连接问题
- Mybatis
- 测试用例
- [计蒜之道 复赛2017] 题解 (只有 A,E 两题)
- Referenced file contains errors(project-aware://org/springframework/beans/factory/xml/spring-beans-4
- 动态规划 数字三角形
- hibernate 和java类 往 mysql 传 数据乱码
- 专题二 符号的技巧---- 14.优先级和类型转换分析