No algorithmic knowledge is required to eat this article
Tips: The title of the “front end cut diagram son” can also be replaced by “CURD engineer”, “find fault engineer”, “adjust parameter man”, etc., welcome to the appropriate seat ~
Let’s start with two interesting questions
1. Mathematical optimization, dimension reduction blow:
Implement a method that accepts a positive integer and adds the digits in each digit until the sum is the units digit
f(12345) - >1 + 2 + 3 + 4 + 5 = 15
f(15) - >1 + 5 = 6
f(6) // return 6
Copy the code
General idea:
Treat the numbers passed as a string and iterate over them until the length of the string is equal to 1.
Pseudo code:
f(num) {
if(num.length === 1) return num
let both = 0;
for(let i = 0; i < num.length; i++) {
both += num[i]
}
return f(both)
}
Copy the code
(Click the text below to expand)
Reduced dimension strike solution ←
(123 = 100 x 1 + 10 x 2 + 1 x 3)
Then each time the given number is processed by f(), the change is as follows:
100A + 10b + C -> A + b + C
100a + 10b + c – (a + b + c) -> a + b + c – (a + b + c)
99a + 9b -> 0
9(11a + b) -> 0
That is, each time the reduction is a multiple of 9, when a recursive parameter number is less than 9, the final result.
A simple verification:
f(123) -> 1 + 2 + 3 -> 6 -> 117 -> 13 x 9
f(2345) -> 2 + 3 + 4 + 5 -> 14 -> 2331 -> 259 x 9
What if a number is divisible by 9?
You can subtract and add by subtracting the remainder by one
81 is divisible by 9, so we’re going to use 80 for the remainder
80% 9 = 8 -> 8 + 1 = 9
F (81) -> 8 + 1 = 9
[Click to expand the final result]
f(n) { return (n - 1) % 9 + 1; }
What code has anyone seen that makes you jump? King has an answer – zhihu www.zhihu.com/question/28…).
2. Be the first
Alex and Li are playing with piles of stones. An even number of pebbles in a row, each with a positive integer number of pebbles.
The game is decided by who has the most stones in his hand. We have an odd number of pebbles, so there’s no draw.
Alex and Li took turns. Alex started first. Each turn, the player takes the entire stack of stones from the beginning or end of the row. This continues until there are no more pebbles, at which point the player with the most pebbles wins.
Assuming alex and Lee both play their best, return true when Alex wins the match and false when Lee wins the match.
Description in Person:
Suppose you have a wave of pebbles, divided into a number of (even) stacks, each of which has a “different number”, laid out in front of you, one across, as follows:
Now you and your good friend Han Meimei have a competition: two people take turns from the row of stone alley left end or right end of a “pile” of stone alley, to the end of the end, who has the most “all” stone alley total number of the winner. You start to take, you can ensure that: you and Han Meimei are normal IQ can play the best level; There won’t be a tie; Return true if you win, false if Han meimei wins.
For people who have not been exposed to algorithms, this problem is likely to be confused. Who am I?? Why am I taking rocks?? What am I doing with rocks??
So if you have some experience with algorithms and you look at this problem, you might think of brute force recursion plus greedy algorithms, dynamic programming plus map storing all the subsets to avoid exponential complexity, and then just do it.
Don’t worry, here we don’t discuss how to achieve the specific, directly show you the results:
(Click the text below to expand)
Witness the miracle of time ←
The answer is one line
return true
That’s right! The truth is:
Julio cruz win
When I first saw this solution, my understanding was that the first hand, each time, can choose the largest of the two most left and most right piles, so over and over again, the first hand must have more pebbles than the second hand.
But I soon came up with an extreme counterexample to prove it: [3,10000000,2,1]
If you win first, you will lose.
The correct interpretation is:
Divide all stones into two combinations according to the odd-th and even-th positions (1st, 3rd, 5th… Is the odd-th bit, the 2nd, 4th, 6th… Is the even digit), the first player can force the second player to choose a combination with a smaller sum.
Maybe you still don’t think much, so LET me introduce the background of this problem:
- This is from Leetcode’s 95th weekly game
- The original title of this problem is in pure English
- Uwi of Japan, who solved the question, gave the answer in one minute and 34 seconds
Feel the original title:
I guess I haven’t finished reading the problem in more than a minute
Reference:
The original link
Returning to life
In case you haven’t noticed, life is also full of algorithms. From machine learning, image recognition, autonomous driving to navigation software, route planning, online shopping coupons for the biggest discounts. These things, though, don’t require any algorithmic knowledge to solve on your own
But have you ever been in a situation where you implement a module/logic, an inner thought:
What it actually looks like:
Have you ever lamented: for the same thing, why other people look at the Angle of view is so ingenious, why the point of view is so new?
So, to return to the question of the article’s title, why do “I” look at algorithms?
- Because in the process of solving algorithm problems, EXERCISE my ability to realize the code
- Because when I looked at other people’s good solutions, I realized the magic of approaching problems from different angles
Finally, I’d like to end with one of my favorite quotes from the last answer:
For example, does a simple bubbling algorithm simply “scan an array multiple times, swapping each pair of adjacent, out-of-order numbers it encounters; Is the array sorted “or even” a piece of hard-won memorized code “when swapping no longer takes place?
If that’s all you’ve learned, well, you’ve really wasted all your time.
As a sort algorithm with general performance, bubble sort itself has a low appearance rate. And there are various libraries that provide a generalized sort algorithm: if you just memorize this, you’ll never have to rewrite the bubbling algorithm in your life.
But if you think of the bubbling algorithm as a local physical process, like a bubble in water, where neighboring elements compare density (or other characteristics) each time, the less dense ones float up and the more dense ones sink. After many iterations, local order becomes global order (in related features).
Even: processing data by mimicking the various local processes that lead to global ordering can make the data as a whole satisfy similar arrangement.
Or even: to examine any law of nature to see what interesting consequences it may have; So when the need to achieve a similar effect, might as well try to use the program to simulate the rule, it is likely to have been the desired effect.
That, in your life, will be a lot of good.
Author: Invalid S
Source: www.zhihu.com/question/20…