20200907: Leikou 203 Weekly Competition Problem Solution Record

Likou 203 Weekly Competition Questions Solution

Topic 1:

1. The sector with the most passes on the circular track
Insert picture description here

Example

Insert picture description here

Problem-solving ideas and code implementation

The idea of ​​this question is to optimize the entire starting point and the ending point and clarify the relationship. In the process from the starting point to the ending point, the starting point is less than the ending point, the starting point is greater than the ending point, and the starting point is equal to the ending point. There are three cases, and the number of repeated parts in the middle is the same. of. Note that the starting point is greater than the ending point. See the code for details. Simple question types are simpler with python, and complex question types are clearer and easier to understand with Java and cpp.

class Solution:
    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
        start,end = rounds[0],rounds[-1]
        # 三种情况依次来写即可
        if start < end :
            return list(range(start,end+1));
        elif start == end :
            return list(range(start,start+1))
        else :
            return list(range(1,end+1)) + list(range(start,n+1))

Topic 2:

2. The maximum number of coins you can get
Insert picture description here

Example

Insert picture description here

Problem-solving ideas and code implementation

Pure sorting questions, just take the value in order after shooting

Python implementation:

class Solution:
    def maxCoins(self, piles: List[int]) -> int:
        # python提供的更加简洁的表达格式,注意与matlab中区间及给定步长使用的区分
        return sum(sorted(piles)[len(piles) // 3::2])

Students who are not familiar with python look at the cpp code implementation:

class Solution {
public:
    int maxCoins(vector<int>& piles) {
        int ans = 0;
        sort(piles.begin(),piles.end());
        int len = piles.size();
        for(int i = len / 3; i < len; i = i + 2){
            ans += piles[i];
        }
        return ans;
    }
};

Topic Three:

3. Find the latest group of size M
Insert picture description here

Example

Insert picture description here

Problem-solving ideas and code implementation

Pay attention to understand the meaning of the question, and perform segmentation processing. There are three cases. After inserting, add a paragraph with a length of 1, or the number of original paragraph lengths is reduced by one, and the original paragraph length plus one paragraph is increased by one, or The original two segments are reduced by one, and a large segment is added. Just write by category.

class Solution {
    
    
    public int findLatestStep(int[] arr, int m) {
    
    
        int[] link = new int[arr.length + 2];
        int cnt = 0;
        int res = -1;

        for (int i = 0; i < arr.length; i++) {
    
    

            int x = arr[i];
           
            int l = link[x - 1] != 0 ? link[x - 1] : x;
           
            int r = link[x + 1] != 0 ? link[x + 1] : x;
            
           
            if (x - l == m) {
    
    
                cnt--;
            }
            if (r - x == m) {
    
    
                cnt--;
            }
            if (r - l + 1 == m) {
    
    
                cnt++;
            }
            if (cnt > 0) {
    
    
                res = i + 1;
            }

            
            link[l] = r;
            link[r] = l;
        }
        return res;
    }
}

Topic 4:

4. Pebble Game V

Insert picture description here

Example

Insert picture description here

Problem-solving ideas and code implementation

Prefix sum and advanced topics of dynamic programming. If the first few questions of the stone game are well understood, this question should not be a big problem. Pay attention to the given state transition equation.

class Solution {
    
    
    public int stoneGameV(int[] stoneValue) {
    
    
        int N = stoneValue.length;
        int[][] dp = new int[N][N];
        
        // 前缀和优化
        int[] preSum= new int[N];
        preSum[0]=stoneValue[0];
        for (int i = 1; i < stoneValue.length; i++) {
    
    
            preSum[i] =preSum[i-1]+stoneValue[i];
        }
        
        // dp即可
        for (int len = 2; len <=N ; len++) {
    
    
            for (int i = 0; i+len-1 <N ; i++) {
    
    
                int j = i+len-1;
                for (int m = i; m <=j ; m++) {
    
    
                    if (i>m || m+1>j){
    
    
                        continue;
                    }
                    int l = dp[i][m];   
                    int r = dp[m+1][j]; 
                    int ls =  preSum[m] - (i>0? preSum[i-1]:0); // i ~ m 分数
                    int rs = preSum[j]- preSum[m]; //m+1 ~ j 分数
                    
                    //左右区间分数相同,取大的
                    if (ls == rs){
    
    
                        int score = Math.max(l,r)+ls;
                        dp[i][j] = Math.max(dp[i][j],score);
                    }
                    // 左右不等,取小的
                    else{
    
    
                        if (ls>rs){
    
    
                            dp[i][j] = Math.max(dp[i][j],r+rs);
                        }else{
    
    
                            dp[i][j] = Math.max(dp[i][j],l+ls);
                        }
                    }
                }
            }

        }
        return dp[0][N-1];
    }
}

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/108460000