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.

  1. Set flag = true;
  2. 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)
  3. 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();    }};