zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 20

    LeetCode Weekly Contest 20

    520. Detect Capital

     
    • User Accepted: 1256
    • User Tried: 1300
    • Total Accepted: 1290
    • Total Submissions: 2464
    • Difficulty: Easy

    Given a word, you need to judge whether the usage of capitals in it is right or not.

    We define the usage of capitals in a word to be right when one of the following cases holds:

    1. All letters in this word are capitals, like "USA".
    2. All letters in this word are not capitals, like "leetcode".
    3. Only the first letter in this word is capital if it has more than one letter, like "Google".
    Otherwise, we define that this word doesn't use capitals in a right way.

    Example 1:

    Input: "USA"
    Output: True
    

    Example 2:

    Input: "FlaG"
    Output: False
    

    Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

    使用模拟法,模拟通过,easy 题目

    class Solution {
    public:
        bool detectCapitalUse(string word) {
            int j, i = 0, len = word.size(); 
            while(i < len){
                if(word[i] >= 'A' && word[i] <= 'Z'){
                    if(i > 0 && word[i-1] !=' '){
                        return false; 
                    }
                    j = i+1; 
                    while(j < len && word[j]>='A' && word[j]<='Z'){
                        j++; 
                    }
                    if(j < len && j>(i+1) && word[j] != ' '){
                        return false; 
                    }
                    i = j; 
                }else{
                    i++; 
                }
            }
            return true; 
        }
    };
    

      

    526. Beautiful Arrangement

     
    • User Accepted: 423
    • User Tried: 678
    • Total Accepted: 428
    • Total Submissions: 1467
    • Difficulty: Medium

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:

    1. The number at the ith position is divisible by i.
    2. i is divisible by the number at the ith position.

    Now given N, how many beautiful arrangements can you construct?

    Example 1:

    Input: 2
    Output: 2
    Explanation: 
    
    The first beautiful arrangement is [1, 2]:
    Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).
    Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).
    The second beautiful arrangement is [2, 1]:
    Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).
    Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

    Note:

    1. N is a positive integer and will not exceed 15.

    因为数据量很小, 直接brute-force dfs解决。

    class Solution {
    public:
        int cnt; 
        
        void dfs(vector<int> &vis, int cur, const int N){
            if(cur == N+1){
                cnt++; 
            }
            for(int i=1; i<=N; ++i){
                if(vis[i] == 0 && (i%cur== 0 || cur%i==0)){
                    vis[i] = 1; 
                    dfs(vis, cur+1, N); 
                    vis[i] = 0; 
                }
            }
        }
        int countArrangement(int N) {
            if(N == 1){
                return 1; 
            }
            vector<int> vis(N+1, 0); 
            cnt = 0; 
            dfs(vis, 1, N); 
            return cnt; 
        }
    };
    

      

    525. Contiguous Array

     
    • User Accepted: 348
    • User Tried: 864
    • Total Accepted: 361
    • Total Submissions: 2464
    • Difficulty: Medium

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

    Example 1:

    Input: [0,1]
    Output: 2
    Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.
    

    Example 2:

    Input: [0,1,0]
    Output: 2
    Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
    

    Note: The length of the given binary array will not exceed 50,000.

    使用map来操作, 复杂度 O(nlogn) 

    class Solution {
    public:
        int findMaxLength(vector<int>& nums) {
            int ans = 0, cnt = 0, len = nums.size(); 
            
            unordered_map<int, int> mp; 
            mp[cnt] = 0; 
            
            for(int i=0; i<len; ++i){
                if(nums[i] == 0){
                    cnt = cnt - 1; 
                }else{
                    cnt = cnt + 1; 
                }
                if(mp.find(cnt) == mp.end()){
                    mp[cnt] = i+1; 
                }else{
                    if(ans < i-mp[cnt]+1){
                        ans = i+1 - mp[cnt]; 
                    }
                }
            }
            return ans; 
        }
    };
    

      

    517. Super Washing Machines

     
    • User Accepted: 42
    • User Tried: 227
    • Total Accepted: 43
    • Total Submissions: 574
    • Difficulty: Hard

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.

    For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .

    Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.

    Example1

    Input: [1,0,5]
    
    Output: 3
    
    Explanation: 
    1st move:    1     0 <-- 5    =>    1     1     4
    2nd move:    1 <-- 1 <-- 4    =>    2     1     3    
    3rd move:    2     1 <-- 3    =>    2     2     2   
    

    Example2

    Input: [0,3,0]
    
    Output: 2
    
    Explanation: 
    1st move:    0 <-- 3     0    =>    1     2     0    
    2nd move:    1     2 --> 0    =>    1     1     1     
    

    Example3

    Input: [0,2,0]
    
    Output: -1
    
    Explanation: 
    It's impossible to make all the three washing machines have the same number of dresses. 
    

    Note:

    1. The range of n is [1, 10000].
    2. The range of dresses number in a super washing machine is [0, 1e5].

    没有get 到题目的点, 看了答案,才有思路

    (1), 只要均值整除总数, 就一定可以达成最后的平均水平。 

    (2), 不妨转换思维,不针对如何操作,针对每一个数值元素。 对于 ith 的元素, 如果左半边比其均值 多n,就一定有操作n次(经过 ith 元素的remove, 不管每次操作的数量)。

    (3), 因为操作的指定性, 所以需要对 每一个数值元素 都符合 ans  > max(l, r) , 所以用 max 操作。 

    class Solution {
    public:
        int findMinMoves(vector<int>& machines) {
            int len = machines.size(); 
            
            vector<int> M(len+1, 0); 
            for(int i=0; i<len; ++i){
                M[i + 1] = M[i] + machines[i]; 
            }
            
            if(M[len] % machines.size()){
                return -1; 
            }
            int av = M[len] / machines.size(); 
            int r, l, ans = 0; 
            for(int i=0; i<machines.size(); ++i){
                l = i*av - M[i];
                r = (len - i - 1)*av - (M[len] - M[i] - machines[i]);
                if(l > 0 && r > 0){
                    ans = max(ans, (l + r)); 
                }else{
                    ans = max(ans, max(abs(l), abs(r))); 
                }
            }
            return ans; 
        }
    };
    

      

  • 相关阅读:
    微信公众平台开发(6) 微信退款接口
    shiro 认证和授权原理
    Shiro架构
    微信公众平台开发(5) 微信客服消息接口
    微信公众平台开发(4) 微信模板消息接口
    微信公众平台开发(3) 企业付款
    微信公众平台开发(1) 通用的工具类CommonUtil
    spring 源码构建
    springMvc配置拦截器无效
    IIdea使用CXF开发WebService
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/6415827.html
Copyright © 2011-2022 走看看