leetcode 153 Zhou race

Links: https://leetcode-cn.com/contest/weekly-contest-153/

With n stations on the ring bus routes, in sequence from 0 to n - 1 is numbered. We know the distance between each pair of adjacent bus stop, distance [i] represents the number of stations and the number i is the distance between the (i + 1)% n stations.
Bus on the ring can travel in the direction of clockwise and counterclockwise.
Passengers return to the starting point to start from the shortest distance between destination destination.

Idea: look at the code

class Solution {
public:
    int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
        int n=distance.size();
        int len=0;
        for(int i=0;i<n;++i)len+=distance[i];
        int dist=0;
        for(int i=start;i<destination;++i)dist+=distance[i];
        for(int i=destination;i<start;++i)dist+=distance[i];
        return min(dist,len-dist);
    }
};

Give you a date, you design an algorithm to determine which is the corresponding day of the week.
Enter three integers: day, month and year, respectively, day, month, year.
The results you return must be in one of these values { "Sunday", "Monday" , "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Idea: look at the code (January 1, 1971, Friday

class Solution {
public:
    string dayOfTheWeek(int day, int month, int year) {
        int mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
        string d[]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        int len=4;
        for(int i=1971;i<year;++i){
            len=(len+365)%7;
            if(is_year(i))
                len=(len+1)%7;
        }
        for(int i=1;i<month;++i){
            if(is_year(year)&&i==2)len=(len+1)%7;
            len=(len+mon[i-1])%7;
        }
        len=(len+day)%7;
        return d[len];
    }
    bool is_year(int year){
        if(((year%4==0)&&(year%100!=0))||(year%400==0))
            return true;
        return false;
    }
};

To give you an array of integers, return it to a non-empty array (continuous element) after performing a delete operation optional, you can get the maximum sum of the elements.
In other words, you can choose from the original array in a sub-array, and can decide whether or not to remove an element from (deleted only once oh), (after deletion) sub-array should have at least one element, then the sub-array (the rest of) the sum of all sub-array elements are among the largest.
Note that when you delete an element, the sub-array can not be empty.

Ideas:
1. Here left [i] is represented by the maximum value of a continuum segment itself the right end point, right [i] the same
2. In seeking left [i] Incidentally, when the request can not remove a segment of a section the answer to update the value of the answer.
3. Finally, the case when seeking delete arr [i]

class Solution {
public:
    int maximumSum(vector<int>& arr) {
        if(arr.size()==1)return arr[0];//特殊情况
        int n=arr.size();
        vector<int> left(n+1);
        vector<int> right(n+1);
        left[0]=arr[0];
        int ans=arr[0];
        for(int i=1;i<n;++i){
            left[i]=max(left[i-1]+arr[i],arr[i]);//不删除
            ans=max(ans,left[i]);
        }
        right[n-1]=arr[n-1];
        for(int i=n-2;i>=0;i--){
            right[i]=max(right[i+1]+arr[i],arr[i]);
        }
        
        for(int i=0;i<n;++i){//删除arr[i]
            int l=0,r=0;
            if(i-1>=0){
                l=left[i-1];
                
            }
            if(i+1<n){
                r=right[i+1];
    
            }
            ans=max(ans,l+r);
            
        }
        return ans;
    }
};

Guess you like

Origin www.cnblogs.com/clear-love/p/11485706.html