leetcode weekly The 283rd game of Zhou Sai an Xian's quantization ~ simulation + mathematical calculation is really numb

Posted by michaellunsford on Sun, 06 Mar 2022 09:38:42 +0100

πŸ“– Content of this article: leetcode weekly Game 283 of Zhou Sai an Xian quantization ~ simulation + mathematical calculation

πŸ“‘ Article column: leetcode weekly game punch in

πŸ“† Last updated: February 27, 2022 leetcode weekly Game 282 of the week race grape City special session of the week race ~ simple simulation + application of hash table + sorting two points, narrowing the range and seeking the best value

πŸ™Š Personal profile: a Junior Program ape who is studying in a two-year college. In line with paying attention to foundation, clock in algorithm and sharing technology, as a blogger who summarizes personal experience, although he may be lazy sometimes, he will stick to it. If you like blog very much, it is suggested to look at the following line ~ (crazy hint QwQ)

πŸŒ‡ give the thumbs-up πŸ‘ Collection ⭐ Leaving a message. πŸ“ One key three connection care program ape, start with you and me

πŸ™Š Write in front

πŸ™Š Here comes Xiaofu. Today we will update the weekly competition column. Today's Xiaofu is the first weekly competition in school. It's true that the state is not good, and it's too delicious.

πŸ“† Game 283 - March 6, 2022

πŸ“ T1. 6016. Cells within a range in Excel table

subject

A cell (r, c) in the Excel table will be represented in the form of string '', where:

That is, the column number C of the cell. Identify with letters in the English alphabet. For example, column 1 is represented by 'A', column 2 by 'B', column 3 by 'C', and so on. That is, the row number r of the cell. Line R is identified by the integer R. Give you A string s in the format of ":", which represents c1 column, r1 row, c2 column and r2 row, and satisfies r1 < = r2 and c1 < = c2.

Find all cells that meet R1 < = x < = R2 and C1 < = y < = C2 and return them in list form. Cells should be represented as strings in the format described above and arranged in non decreasing order (first by column, then by row).

Examples

Example 1:

Input: s = "K1:L2"
Output:["K1","K2","L1","L2"]
Explanation:
The above figure shows the cells that should appear in the list.
Red arrows indicate the order in which cells appear.

Example 2:

Input: s = "A1:F1"
Output:["A1","B1","C1","D1","E1","F1"]
Explanation:
The above figure shows the cells that should appear in the list. 
Red arrows indicate the order in which cells appear.

⭐ Train of thought ⭐

Ideas and inspection points of this topic:

  • Xiao Fu believes that today's first question is a little more complicated than the usual first question, but it is still a simple simulation question. The idea is that we need to split the given string to obtain the starting Excel table position and the ending Excel table position. Then simulate the Excel table of the range column corresponding to each row and column.

code implementation

class Solution {
     public List<String> cellsInRange(String s) {
        List<String> res = new ArrayList<>();
        // String splitting is used to obtain the starting table position and ending table position respectively to obtain the range
        String[] params = s.split(":");
        // Gets the character of the corresponding starting column
        char col1 = params[0].charAt(0);
        // Gets the number of rows corresponding to the starting row
        int row1 = Integer.parseInt(params[0].substring(1));
        // Gets the character of the corresponding column
        char col2 = params[1].charAt(0);
        // Get the number of rows corresponding to the ending row
        int row2 = Integer.parseInt(params[1].substring(1));
        // simulation
        for (char c = col1 ;c <= col2;c++){
        	// It is worth noting that each time we reach the next column, the corresponding row needs to be reset to the starting row
            int j = row1;
            for (int i = j ; i<= row2;i++){
            	// Add results to the list
                StringBuilder tmp = new StringBuilder();
                tmp.append(c).append(j++);
                res.add(tmp.toString());
            }
        }
        return res;
    }
}

results of enforcement

πŸ“ T2. Add 6017 integers to the array

subject

Give you an integer array nums and an integer K. Please add k different positive integers that do not appear in nums to nums, and minimize the sum of the elements of the result array.

Returns the sum of k integers appended to nums.

Examples

Example 1:

Input: nums = [1,4,25,10,25], k = 2
 Output: 5
 Explanation: in this solution, the two different positive integers added to the array are 2 and 3.
nums The final element sum is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70 ,This is the minimum in all cases.
So the sum of the two integers appended to the array is 2 + 3 = 5 ,So return 5.

Example 2:

Input: nums = [5,6], k = 6
 Output: 25
 Explanation: in this solution, the two different positive integers added to the array are 1, 2, 3, 4, 7 and 8.
nums The final element sum is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36 ,This is the minimum in all cases.
So the sum of the two integers appended to the array is 1 + 2 + 3 + 4 + 7 + 8 = 25 ,So return 25.

Tips

1 <= nums.length <= 105
1 <= nums[i], k <= 109

⭐ Train of thought ⭐

Ideas and inspection points of this topic:

  • This question is really a little difficult for Xiaofu in this position. I broke the defense of this question. At first, Xiaofu wanted to solve it through hashing, but it exceeded the memory, then Xiaofu made improvements, then overtime, and finally improved, but the example still couldn't pass. I was fined about seven or eight times, my mind was cracked, and I directly abandoned the game today.
  • But I was thinking about this problem at lunch and found that it was a math problem.
  • The following is a reference to the practice and ideas of a big man in the group:
    1. First of all, we need to sort the array, which is helpful for us to increase the non repeated values from small to large, so as to minimize the sum of integers added to the array.
    2. After sorting, we need to consider the classification. If the first value in the sorting array is greater than k, we can easily know that the array can be added with K numbers from 1 to K, so the added value is 1 + 2 + 3 ++ k ? This is the sum of the first n items of the arithmetic sequence. If the tolerance is 1-n arithmetic sequence, then the sum of the first n items is n*(n+1)/2.
    3. After discussing this situation, we will start the simulation. This simulation needs to be written on the draft paper. For example, it is much easier to understand. We need to set a value in the smallest non repeating array that can be added. We set the initial value to k. If the value we traverse is smaller or equal to the value we need to add, it is easy to understand here. Because the same element in the array cannot be added to the previously added element, we need to make the number that needs to be added increase automatically and then add it, However, the value added here is not necessarily the value that does not exist in the array. We need to traverse to the value that does not exist. It is the real completion of the addition. Here is the greedy idea.

What Xiaofu may express here is not very clear. After all, greedy ideas are not easy to speak out. You need to calculate it on your own draft paper

code implementation

class Solution {
    public long minimalKSum(int[] nums, int k) {
		Arrays.sort(nums);
		// Calculate the sum of the first n items
		long sum = (1L + k) * k / 2;
		// Record the initial value size that needs to be appended at present
		long curAddNum = k;
		// If the number to be added is smaller than the first number, just return it directly, because the sum of 1-k numbers can be added
		if (nums[0] > k)return sum; 
		// simulation
		for (int i = 0 ; i< nums.length;i++){
			if (i > 0 && nums[i-1] == nums[i])continue;
			if (nums[i] <= curAddNum){
				curAddNum++;
				// Greedy is to move back if the value that needs to be added currently exists in the array or has been added
				sum = sum -  nums[i] + curAddNum; 
			}
		}
		return sum;
    }
}

results of enforcement

πŸ™Š Write at the end

Xiaofu clocked in the 8th one week match 2022-03-06

Try to do all the questions you can do well

This week, I got a question right

T2 super memory timeout. Those who should be wrong have been fined once, changed their mentality ten times, collapsed and directly abandoned the game

So it was a good week

last

Make progress every day and harvest every day

I wish you success in your career and success in your studies

If you feel good, don't forget to click three times~

Topics: Algorithm leetcode