Sonnet 73

by William Shakespeare

That time of year thou mayst in me behold,
When yellow leaves, or none, or few, do hang
Upon those boughs which shake against the cold,
Bare ruined choirs, where late the sweet birds sang;
In me thou seest the twilight of such day
As after sunset fadeth in the west,
Which by and by black night doth take away,
Death’s second self that seals up all in rest;
In me thou seest the glowing of such fire
That on the ashes of his youth doth lie,
As the deathbed whereon it must expire,
Consum’d with that which it was nourish’d by;
This thou perceiv’st, which makes thy love more strong,
To love that well, which thou must leave ere long.


LeetCode - Algorithms - 15. 3Sum

Beats me! Just post solution of other guy.

Problem

15. 3Sum

Java

two pointers

© LeetCode – 3Sum - two pointers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);

ArrayList<List<Integer>> result = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {
int j = i + 1;
int k = nums.length - 1;

if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}

while (j < k) {
if (k < nums.length - 1 && nums[k] == nums[k + 1]) {
k--;
continue;
}

if (nums[i] + nums[j] + nums[k] > 0) {
k--;
} else if (nums[i] + nums[j] + nums[k] < 0) {
j++;
} else {
ArrayList<Integer> l = new ArrayList<>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[k]);
result.add(l);
j++;
k--;
}
}
}

return result;
}
}

Submission Detail

  • 318 / 318 test cases passed.
  • Runtime: 21 ms, faster than 65.13% of Java online submissions for 3Sum.
  • Memory Usage: 43.1 MB, less than 88.54% of Java online submissions for 3Sum.

Quadratic algorithm

© 3SUM - Quadratic algorithm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
final int N = nums.length;
Arrays.sort(nums);
Set<List<Integer>> set = new HashSet<List<Integer>>();
int start, end;
int a, b, c;
for (int i = 0; i < N - 1; i++) {
a = nums[i];
start = i + 1;
end = N - 1;
while (start < end) {
b = nums[start];
c = nums[end];
if (a + b + c == 0) {
set.add(new ArrayList<>(Arrays.asList(a, b, c)));
start = start + 1;
end = end - 1;
} else if (a + b + c > 0) {
end = end - 1;
} else {
start = start + 1;
}
}
}
return new ArrayList<>(set);
}
}

Submission Detail

  • 318 / 318 test cases passed.
  • Runtime: 230 ms, faster than 27.64% of Java online submissions for 3Sum.
  • Memory Usage: 43.5 MB, less than 64.19% of Java online submissions for 3Sum.

concise solution

© Java clean solution (15 lines) https://leetcode.com/problems/3sum/discuss/865345/Java-clean-solution-(15-lines)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
HashSet<List<Integer>> res = new HashSet<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
int target = -1 * nums[i];
HashSet<Integer> set = new HashSet<>();
for (int j = i + 1; j < nums.length; j++) {
if (set.contains(target - nums[j]))
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], target - nums[j])));
set.add(nums[j]);
}
}
return new ArrayList<>(res);
}
}

Submission Detail

  • 318 / 318 test cases passed.
  • Runtime: 768 ms, faster than 6.51% of Java online submissions for 3Sum.
  • Memory Usage: 43.4 MB, less than 73.80% of Java online submissions for 3Sum.

ref

What is a coronavirus? - Elizabeth Cox - TED-Ed - Transcript

For almost a decade, scientists chased the source of a deadly new virus through China’s tallest mountains and most isolated caverns.

They finally found it here: in the bats of Shitou Cave. The virus in question was a coronavirus that caused an epidemic of severe acute respiratory syndrome, or SARS, in 2003.

Coronaviruses are a group of viruses covered in little protein spikes that look like a crown— or “corona” in Latin. There are hundreds of known coronaviruses. Seven of them infect humans, and can cause disease. The coronavirus SARS-CoV causes SARS, MERS-CoV causes MERS, and SARS-CoV-2 causes the disease COVID-19.

Of the seven human coronaviruses, four cause colds, mild, highly contagious infections of the nose and throat. Two infect the lungs, and cause much more severe illnesses. The seventh, which causes COVID-19, has features of each: it spreads easily, but can severely impact the lungs.

When an infected person coughs, droplets containing the virus spray out. The virus can infect a new person when the droplets enter their nose or mouth. Coronaviruses transmit best in enclosed spaces, where people are close together. Cold weather keeps their delicate casing from drying out, enabling the virus to survive for longer between hosts, while UV exposure from sunlight may damage it. These seasonal variations matter more for established viruses. But because no one is yet immune to a new virus, it has so many potential hosts that it doesn’t need ideal conditions to spread.

In the body, the protein spikes embed in the host’s cells and fuse with them — enabling the virus to hijack the host cell’s machinery to replicate its own genes.

Coronaviruses store their genes on RNA. All viruses are either RNA viruses or DNA viruses. RNA viruses tend to be smaller, with fewer genes, meaning they infect many hosts and replicate quickly in those hosts. In general, RNA viruses don’t have a proofreading mechanism, whereas DNA viruses do. So when an RNA virus replicates, it’s much more likely to have mistakes called mutations.

Many of these mutations are useless or even harmful. But some make the virus better suited for certain environments — like a new host species. Epidemics often occur when a virus jumps from animals to humans. This is true of the RNA viruses that caused the Ebola, Zika, and SARS epidemics, and the COVID-19 pandemic. Once in humans, the virus still mutates — usually not enough to create a new virus, but enough to create variations, or strains, of the original one.

Coronaviruses have a few key differences from most RNA viruses. They’re some of the largest, meaning they have the most genes. That creates more opportunity for harmful mutations. To counteract this risk, coronaviruses have a unique feature: an enzyme that checks for replication errors and corrects mistakes. This makes coronaviruses much more stable, with a slower mutation rate, than other RNA viruses.

While this may sound formidable, the slow mutation rate is actually a promising sign when it comes to disarming them. After an infection, our immune systems can recognize germs and destroy them more quickly if they infect us again so they don’t make us sick. But mutations can make a virus less recognizable to our immune systems — and therefore more difficult to fight off. They can also make antiviral drugs and vaccines less effective, because they’re tailored very specifically to a virus. That’s why we need a new flu vaccine every year — the influenza virus mutates so quickly that new strains pop up constantly. The slower mutation rate of coronaviruses means our immune systems, drugs, and vaccines might be able to recognize them for longer after infection, and therefore protect us better.

Still, we don’t know how long our bodies remain immune to different coronaviruses. There’s never been an approved treatment or vaccine for a coronavirus. We haven’t focused on treating the ones that cause colds, and though scientists began developing treatments for SARS and MERS, the epidemics ended before those treatments completed clinical trials.

As we continue to encroach on other animals’ habitats, some scientists say a new coronavirus jumping to humans is inevitable — but if we investigate these unknowns, it doesn’t have to be devastating.

Extended Euclidean algorithm and Modular multiplicative inverse

The computation of the modular multiplicative inverse is an essential step in the derivation of key-pairs in the RSA public-key encryption method. A benefit for the computer implementation of these applications is that there exists a very fast algorithm (the extended Euclidean algorithm) that can be used for the calculation of modular multiplicative inverses.

Extended Euclidean algorithm

Bézout’s identity — Let a and b be integers with greatest common divisor d. Then, there exist integers x and y such that ax + by = d. More generally, the integers of the form ax + by are exactly the multiples of d.

The integers x and y are called Bézout coefficients for (a, b); they are not unique. A pair of Bézout coefficients can be computed by the extended Euclidean algorithm.

\( m’m + n’n = gcd(m, n) \)

\(
\begin{align*}
\begin{cases} \text{if }m = 0, \text{we simply take } m’=0 \text{ and } n’=1 \text{ as } gcd(0, n)=n \\
\text{Otherwise }m \neq 0, \text{we let }r=n \bmod m=n-\lfloor \frac{n}{m} \rfloor m
\end{cases}
\end{align*}
\)

\( \overline{r}r + \overline{m}m = gcd(r,m) \)

\( \text{ as } gcd(m, n) = gcd(r, m) \)
\( \overline{r}(n-\lfloor \frac{n}{m} \rfloor m) + \overline{m}m = m’m + n’n \implies \)
\( (\overline{m}-\lfloor \frac{n}{m} \rfloor \overline{r})m + \overline{r}n = m’m + n’n \implies \)

\(
\begin{align*}
\begin{cases} m’ = \overline{m}-\lfloor \frac{n}{m} \rfloor \overline{r} \\
n’ = \overline{r}
\end{cases}
\end{align*}
\)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class ExtendedEuclid {
public static void main(String[] args) {
int m = 12, n = 18;
EEResult re = extendedEuclid(m, n);
System.out.printf("%d*%d + %d*%d = %d", re.a, m, re.b, n, re.d);
}

public static EEResult extendedEuclid(int m, int n) {
EEResult re = new EEResult();
if (m==0) {
re.d = n;
re.a = 0;
re.b = 1;
return re;
}
re = extendedEuclid(n%m, m);
int d = re.d;
int a = re.b - (n/m)*re.a;
int b = re.a;
re.d = d;
re.a = a;
re.b = b;
return re;
}

static class EEResult {
int d;
int a;
int b;
}
}

Modular multiplicative inverse

Finding modular multiplicative inverses also has practical applications in the field of cryptography, i.e. public-key cryptography and the RSA Algorithm.

\( \displaystyle ax\equiv b{\pmod {m}} \)

\( \displaystyle x\equiv a^{-1}{\pmod {m}} \)

\(
\begin{align*}
\begin{cases} d_1 \equiv 35^{-1}{\pmod {3}} = 2 \\
d_2 \equiv 21^{-1}{\pmod {5}} = 1 \\
d_3 \equiv 15^{-1}{\pmod {7}} = 1
\end{cases}
\end{align*}
\)

1
2
3
4
5
import java.math.BigInteger;

BigInteger.valueOf(35).modInverse(BigInteger.valueOf(3));
BigInteger.valueOf(21).modInverse(BigInteger.valueOf(5));
BigInteger.valueOf(15).modInverse(BigInteger.valueOf(7));

Build your IELTS Listening test vocabulary

© Build your IELTS Listening test vocabulary

Part 1: familiar vocabulary

1. Days of the week

Sunday

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

Quick Tip: Wednesday is commonly misspelled.

2. Months and seasons of the year

January

February

March

April

May

June

July

August

September

October

November

December

winter

spring

autumn

summer

Quick Tip: The first ‘r’ in February is often forgotten. If you struggle with spelling, you can choose to write the date using numbers as long as these numbers are written in the correct format. E.g. ‘the 10th of February, 2020’ = 10/02/2020

Quick Tip: the final ‘n’ in autumn is often forgotten as we don’t hear the silent ‘n’.

3. Shapes

circle

square

rectangle

triangle

cylinder

oval

Quick Tip: Shapes can be used when we refer to places in a city, King George Square, so it’s important that you can spell common shapes.

4. Transportation

automobile

truck

tractor

tram

subway

airplane

train

bicycle

car

pedestrian

passenger

commuter

Quick Tip: ‘ commuter ‘ is often misspelled, confusing it with ‘computer’!

5. Colours

red

orange

yellow

green

blue

purple

white

black

brown

Quick Tip: Colour spelled with a ‘u’ is the Canadian/British spelling. In the US, it is spelled ‘color’ without a ‘u’. Both are correct and a matter of preference.

6. Verbs

suggest

develop

borrow

persuade

discuss

review

concentrate

believe

crash

Quick Tip: Sometimes verbs are conjugated for gender, tense, etc. The difference could give you an incorrect answer.

7. Adjectives

beautiful

necessary

fantastic

comfortable

convenient

wonderful

terrible

temporary

permanent

knowledgeable

exciting

boring

difficult

easy

Quick Tip: Make sure that you note adjectives that have double consonants (ss/mm). It is easy to miss a letter.

8. Numbers, times, and currencies: commonly appear in Part 1 of the listening test. Recording the corresponding number or symbol is suggested because this will help eliminate the possibility of spelling the word incorrectly.

For example, thirty dollars is the same as $30 on the listening test. Also, six o’clock and 6:00 are the same as well. Finally, ten thousand and 10,000 are also both correct. Writing the number or symbol allows you to feel more confident that you have not made a spelling mistake.

Quick Tip: When deciding on whether the answer is 30 or 13, listen to the stressed syllable. If it’s 13, the stress is on the second syllable ‘thir teen ‘, if it’s 30, the stress is on the first syllable ‘ thir ty’.

Quick Tip: Copy the correct symbol from the Listening question to make sure you are using the correct currency. If you use ‘$’ instead of ‘£’, the answer will be incorrect.

Part 2: a monologue, speech or talk

1. Rooms in buildings

kitchen

bathroom

bedroom

living room

dining room

lounge

library

gymnasium (gym)

cafeteria (cafe)

classroom

waiting room

reception

ticket desk

storage room

theatre

2. Place markers

street

road

avenue

lane

drive

court

place

terrace

way

There is some vocabulary you should be listening for, but may not have to write down when doing a map question in Part 2, which will allow you to follow the map more easily. Make sure you know where these direction words point to.

3. Directions and prepositions of place

north

south

east

west

up

down

left

straight

across from

between

beside

diagonal

corner

opposite

adjacent to

near

past

before

after

4. Verbs

turn

move

continue on

walk

cross

pass

start

finish

end

stop

go straight ahead

5. Places on a map

tennis court

river

courtyard

laboratory

building

bridge

road

path

traffic lights

bench

seat

table

basketball court

running track

swimming pool

beach

forest

garden

castle

Part 3: a conversation between people

1. School terms

presentation

project

teamwork

pairs

organisation

outline

proofreading

experiment

reference

lecture

tutor

teacher

attendance

specialist

knowledge

faculty

bachelor’s

master’s

schedule

management

leadership

questionnaire

statistic

percentage

laboratory

school

university

college

Quick Tip: North Americans pronounce ‘laboratory’ in three syllables: lab-bra-tory. British English speakers pronounce the same word using four syllables: lab-or-a-tory.

2. Subjects in school

Mathematics (Maths)

Science

English

Physical Education (PE)

Art

Music

Geography

Biology

Chemistry

History

Quick Tip: When students study a subject at school, their school year is divided into terms or semesters. They have breaks or holidays between each term.

3. Subjects in university

Commerce

Science

Psychology

Engineering

Marketing

Sociology

Medicine

History

Geography

Architecture

Law

Philosophy

Economics

Education

Quick Tip: When students study at university, their university year is divided into semesters. They usually have exam blocks at the end of each semester following by a break. They can study part-time or full-time and combine it with employment.

4. Examination

assessment

test

revision

pass

fail

repeat

supervise

supervisor

assess

exam

results

degree

certificate

Part 4: a university lecture

1. Health

vegetarian

vegan

healthy

unhealthy

leisure

disease

vitamin

protein

carbohydrates

exercise

treatment

obese

overweight

fit

doctor

check up

medicine

vitamin

pandemic

virus

cure

vaccination

2. Animals and their habitat

mammals

reptile

primates

predators

prey

mountain

jungle

forest

island

pond

river

stream

zoo

pet

endangered

species

ocean

sea

3. Continents and Countries

North America

South America

Asia

Africa

Europe

Antarctica

Australia

Oceania

England

Canada

China

United Kingdom

Germany

Mexico

Switzerland

4. Environment

global warming

disaster

earthquake

tornado

blizzard

hurricane

pollution

temperature

drought

flood

cyclone

volcanic eruption

deforestation

desertification

bush fires

5. Government

politics

leader

politician

senator

mayor

laws

regulations

senate

president

society

individual

council

rules

Quick Tip: The word ‘society’ is one of the most commonly misspelled words on the IELTS test.

6. Energy

nuclear

oil

coal

hydro electrical power

natural gas

solar power

source

generate

electricity

dam

windmill

wind turbine

renewable

non-renewable

Quick Tip: Remember to check your spelling of nuclear, it is often written as ‘unclear’, a simple typo that result in an incorrect answer.

7. General

appointment

cooperation

employment

government

exhibition

occupation

aluminum

century

decade

millennium

individual

creativity

guarantee

satellite

opportunity

licence

frequently

calendar

different

Quick Tip: The words ‘government’ and ‘different’ are two of the most commonly misspelled words in the IELTS test. Also, ‘aluminum’ is pronounced with four syllables in North American English: a-lu-min-num. It is pronounced with five syllables in British English: al-u-min-i-um.

LeetCode - Algorithms - 242. Valid Anagram

Problem

242. Valid Anagram

Java

HashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public boolean isAnagram(String s, String t) {
final int len_s = s.length();
final int len_t = t.length();
if (len_s != len_t)
return false;
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++) {
Character k = s.charAt(i);
if (map.containsKey(k)) {
Integer num = map.get(k);
map.put(k, ++num);
}
else {
map.put(k, 1);
}
}
for (int i = 0; i < t.length(); i++) {
Character k = t.charAt(i);
if (map.containsKey(k)) {
Integer num = map.get(k);
map.put(k, --num);
} else {
return false;
}
}
for (Integer count : map.values()) {
if (count.intValue() != 0)
return false;
}
return true;
}
}

Submission Detail

  • 34 / 34 test cases passed.
  • Runtime: 12 ms, faster than 22.04% of Java online submissions for Valid Anagram.
  • Memory Usage: 40.6 MB, less than 28.82% of Java online submissions for Valid Anagram.

Sorting

© 20+ basic Algorithms Problems from Coding Interviews 16. Write Algorithms to Check if Two String are Anagram

One trick to solve this problem is to sort the character array and check if they are the same or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public boolean isAnagram(String s, String t) {
final int len_s = s.length();
final int len_t = t.length();
if (len_s != len_t)
return false;
char[] arr_s = s.toCharArray();
char[] arr_t = t.toCharArray();
Arrays.sort(arr_s);
Arrays.sort(arr_t);
for (int i = 0; i < len_s; i++) {
if (arr_s[i] != arr_t[i])
return false;
}
return true;
}
}

Submission Detail

  • 34 / 34 test cases passed.
  • Runtime: 4 ms, faster than 51.35% of Java online submissions for Valid Anagram.
  • Memory Usage: 42.2 MB, less than 9.92% of Java online submissions for Valid Anagram.

leetcode solution - Hash Table

It seemed that no 0ms solution by java.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] counter = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
for (int count : counter) {
if (count != 0) {
return false;
}
}
return true;
}
}

Submission Detail

  • 34 / 34 test cases passed.
  • Runtime: 3 ms, faster than 79.53% of Java online submissions for Valid Anagram.
  • Memory Usage: 39.4 MB, less than 84.32% of Java online submissions for Valid Anagram.

Why you will fail to have a great career - Larry Smith - TEDxUW | November 2011 - Transcript

What you see in the TED talk is essentially thirty years of Smith’s frustrations reaching a boiling point. “Wasted talent is a waste I cannot stand,” Smith told me. “My students want to create technology. I want them to create really ‘kick-ass’ technology. I want them to be passionate about what they’re doing.” — Carmine Gallo


I want to discuss with you this afternoon why you’re going to fail to have a great career.

(Laughter)

I’m an economist.

I do dismal. End of the day, it’s ready for dismal remarks. I only want to talk to those of you who want a great career. I know some of you have already decided you want a good career. You’re going to fail, too.

(Laughter)

Because – goodness, you’re all cheery about failing.

(Laughter)

Canadian group, undoubtedly.

(Laughter)

Those trying to have good careers are going to fail, because, really, good jobs are now disappearing. There are great jobs and great careers, and then there are the high-workload, high-stress, bloodsucking, soul-destroying kinds of jobs, and practically nothing in-between.

So people looking for good jobs are going to fail. I want to talk about those looking for great jobs, great careers, and why you’re going to fail. First reason is that no matter how many times people tell you, “If you want a great career, you have to pursue your passion, you have to pursue your dreams, you have to pursue the greatest fascination in your life,” you hear it again and again, and then you decide not to do it. It doesn’t matter how many times you download Steven J.’s Stanford commencement address, you still look at it and decide not to do it.

I’m not quite sure why you decide not to do it. You’re too lazy to do it. It’s too hard. You’re afraid if you look for your passion and don’t find it, you’ll feel like you’re an idiot, so then you make excuses about why you’re not going to look for your passion. They are excuses, ladies and gentlemen. We’re going to go through a whole long list – your creativity in thinking of excuses not to do what you really need to do if you want to have a great career.

So, for example, one of your great excuses is:

(Sigh)

“Well, great careers are really and truly, for most people, just a matter of luck. So I’m going to stand around, I’m going to try to be lucky, and if I’m lucky, I’ll have a great career. If not, I’ll have a good career.” But a good career is an impossibility, so that’s not going to work.

Then, your other excuse is, “Yes, there are special people who pursue their passions, but they are geniuses. They are Steven J. I’m not a genius. When I was five, I thought I was a genius, but my professors have beaten that idea out of my head long since.”

(Laughter)

“And now I know I am completely competent.” Now, you see, if this was 1950, being completely competent – that would have given you a great career. But guess what? This is almost 2012, and saying to the world, “I am totally, completely competent,” is damning yourself with the faintest of praise.

And then, of course, another excuse: “Well, I would do this, I would do this, but, but – well, after all, I’m not weird. Everybody knows that people who pursue their passions are somewhat obsessive. A little strange. Hmm? Hmm? Okay? You know, a fine line between madness and genius. “I’m not weird. I’ve read Steven J.’s biography. Oh my goodness – I’m not that person. I am nice. I am normal. I’m a nice, normal person, and nice, normal people – don’t have passion.”

(Laughter)

“Ah, but I still want a great career. I’m not prepared to pursue my passion, so I know what I’m going to do, because I have a solution. I have a strategy. It’s the one Mommy and Daddy told me about. Mommy and Daddy told me that if I worked hard, I’d have a good career. So, if you work hard and have a good career, if you work really, really, really hard, you’ll have a great career. Doesn’t that, like, mathematically make sense?” Hmm. Not. But you’ve managed to talk yourself into that.

You know what? Here’s a little secret: You want to work? You want to work really, really, really hard? You know what? You’ll succeed. The world will give you the opportunity to work really, really, really, really hard. But, are you so sure that that’s going to give you a great career, when all the evidence is to the contrary?

So let’s deal with those of you who are trying to find your passion. You actually understand that you really had better do it, never mind the excuses. You’re trying to find your passion –

(Sigh)

and you’re so happy. You found something you’re interested in.

“I have an interest! I have an interest!”

You tell me. You say, “I have an interest!” I say, “That’s wonderful! And what are you trying to tell me?” “Well, I have an interest.” I say, “Do you have passion?” “I have an interest,” you say. “Your interest is compared to what?” “Well, I’m interested in this.” “And what about the rest of humanity’s activities?” “I’m not interested in them.” “You’ve looked at them all, have you?” “No. Not exactly.”

Passion is your greatest love. Passion is the thing that will help you create the highest expression of your talent. Passion, interest – it’s not the same thing. Are you really going to go to your sweetie and say, “Marry me! You’re interesting.”

(Laughter)

Won’t happen. Won’t happen, and you will die alone.

(Laughter)

What you want, what you want, what you want, is passion. It is beyond interest. You need 20 interests, and then one of them, one of them might grab you, one of them might engage you more than anything else, and then you may have found your greatest love, in comparison to all the other things that interest you, and that’s what passion is.

I have a friend, proposed to his sweetie. He was an economically rational person. He said to his sweetie, “Let us marry. Let us merge our interests.”

(Laughter)

Yes, he did.

“I love you truly,” he said. “I love you deeply. I love you more than any other woman I’ve ever encountered. I love you more than Mary, Jane, Susie, Penelope, Ingrid, Gertrude, Gretel – I was on a German exchange program then. I love you more than –” All right. She left the room halfway through his enumeration of his love for her. After he got over his surprise at being, you know, turned down, he concluded he’d had a narrow escape from marrying an irrational person. Although, he did make a note to himself that the next time he proposed, it was perhaps not necessary to enumerate all of the women he had auditioned for the part.

(Laughter)

But the point stands. You must look for alternatives so that you find your destiny, or are you afraid of the word “destiny”? Does the word “destiny” scare you? That’s what we’re talking about. And if you don’t find the highest expression of your talent, if you settle for “interesting,” what the hell ever that means, do you know what will happen at the end of your long life? Your friends and family will be gathered in the cemetery, and there beside your gravesite will be a tombstone, and inscribed on that tombstone it will say, “Here lies a distinguished engineer, who invented Velcro.” But what that tombstone should have said, in an alternative lifetime, what it should have said if it was your highest expression of talent, was, “Here lies the last Nobel Laureate in Physics, who formulated the Grand Unified Field Theory and demonstrated the practicality of warp drive.”

(Laughter)

Velcro, indeed!

(Laughter)

One was a great career. One was a missed opportunity. But then, there are some of you who, in spite of all these excuses, you will find, you will find your passion. And you’ll still fail.

You’re going to fail, because – because you’re not going to do it, because you will have invented a new excuse, any excuse to fail to take action, and this excuse, I’ve heard so many times: “Yes, I would pursue a great career, but, I value human relationships –

(Laughter)

more than accomplishment. I want to be a great friend. I want to be a great spouse. I want to be a great parent, and I will not sacrifice them on the altar of great accomplishment.”

(Laughter)

What do you want me to say? Now, do you really want me to say now, tell you, “Really, I swear I don’t kick children.”

(Laughter)

Look at the worldview you’ve given yourself. You’re a hero no matter what. And I, by suggesting ever so delicately that you might want a great career, must hate children. I don’t hate children. I don’t kick them. Yes, there was a little kid wandering through this building when I came here, and no, I didn’t kick him.

(Laughter)

Course, I had to tell him the building was for adults only, and to get out. He mumbled something about his mother, and I told him she’d probably find him outside anyway. Last time I saw him, he was on the stairs crying.

(Laughter)

What a wimp.

(Laughter)

But what do you mean? That’s what you expect me to say. Do you really think it’s appropriate that you should actually take children and use them as a shield? You know what will happen someday, you ideal parent, you? The kid will come to you someday and say, “I know what I want to be. I know what I’m going to do with my life.” You are so happy. It’s the conversation a parent wants to hear, because your kid’s good in math, and you know you’re going to like what comes next. Says your kid, “I have decided I want to be a magician. I want to perform magic tricks on the stage.”

(Laughter)

And what do you say? You say, you say, “That’s risky, kid. Might fail, kid. Don’t make a lot of money at that, kid. I don’t know, kid, you should think about that again, kid. You’re so good at math, why don’t you –”

The kid interrupts you and says, “But it is my dream. It is my dream to do this.” And what are you going to say? You know what you’re going to say? “Look kid. I had a dream once, too, but – But –” So how are you going to finish the sentence with your “but”? “But. I had a dream too, once, kid, but I was afraid to pursue it.” Or are you going to tell him this: “I had a dream once, kid. But then, you were born.”

(Laughter)

(Applause)

Do you really want to use your family, do you really ever want to look at your spouse and your kid, and see your jailers? There was something you could have said to your kid, when he or she said, “I have a dream.” You could have said – looked the kid in the face and said, “Go for it, kid! Just like I did.” But you won’t be able to say that, because you didn’t. So you can’t.

(Laughter)

And so the sins of the parents are visited on the poor children. Why will you seek refuge in human relationships as your excuse not to find and pursue your passion? You know why. In your heart of hearts, you know why, and I’m being deadly serious. You know why you would get all warm and fuzzy and wrap yourself up in human relationships. It is because you are – you know what you are.

You’re afraid to pursue your passion. You’re afraid to look ridiculous. You’re afraid to try. You’re afraid you may fail. Great friend, great spouse, great parent, great career. Is that not a package? Is that not who you are? How can you be one without the other? But you’re afraid.

And that’s why you’re not going to have a great career. Unless – “unless,” that most evocative of all English words – “unless.” But the “unless” word is also attached to that other, most terrifying phrase, “If only I had …” “If only I had …” If you ever have that thought ricocheting in your brain, it will hurt a lot.

So, those are the many reasons why you are going to fail to have a great career. Unless –

Unless.

Thank you.

(Applause)


LeetCode - Algorithms - 796. Rotate String

Problem

796. Rotate String

Java

my solution - brute method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean rotateString(String A, String B) {
final int len_A = A.length();
final int len_B = B.length();
if (len_A != len_B)
return false;
if (A.isEmpty() && B.isEmpty())
return true;
for(int i=0; i<len_A; i++) {
A = A.substring(1)+A.charAt(0);
if (B.equals(A))
return true;
}
return false;
}
}

Submission Detail

  • 45 / 45 test cases passed.
  • Runtime: 5 ms, faster than 9.75% of Java online submissions for Rotate String.
  • Memory Usage: 38.4 MB, less than 32.29% of Java online submissions for Rotate String.

trick method

© 20+ basic Algorithms Problems from Coding Interviews 19. How to check if two String is rotations of each other?

There is a simple trick to solve this problem, just concatenate the String with itself and check if the rotation exists there. You can do that by using indexOf or substring method. If the concatenated String contains rotation then given String is a rotation of former.

1
2
3
4
5
6
7
8
9
10
class Solution {
public boolean rotateString(String A, String B) {
final int len_A = A.length();
final int len_B = B.length();
if (len_A != len_B)
return false;
String s = A + A;
return s.indexOf(B) != -1;
}
}

Submission Detail

  • 45 / 45 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Rotate String.
  • Memory Usage: 37.2 MB, less than 75.85% of Java online submissions for Rotate String.

LeetCode - Algorithms - 507. Perfect Number

Problem

507. Perfect Number

Java

my solution

There is one fact that odd perfect numbers are greater than \( 10^{1500} \) if exist. By Euclid–Euler theorem, there is a one-to-one relationship between even perfect numbers and Mersenne primes. Even perfect number have the form as \( 2^{p−1}(2^p−1) \).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public boolean checkPerfectNumber(int num) {
if ((num & 1) == 1)
return false;
else {
if (num == 0)
return false;
int n = num;
int p = 0;
for (; (n & 1) == 0; p++) {
n = n >> 1;
}
int x = (2 << (p - 1)) * ((2 << p) - 1);
return num == x;
}
}
}

Submission Detail

  • 156 / 156 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Perfect Number.
  • Memory Usage: 37.7 MB, less than 23.24% of Java online submissions for Perfect Number.

LeetCode - Algorithms - 392. Is Subsequence

Problem

392. Is Subsequence

Java

Recursion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public boolean isSubsequence(String s, String t) {
boolean b = true;
final int len_s = s.length();
if (len_s > 0) {
int i = t.indexOf(s.charAt(0));
if (i != -1)
return isSubsequence(s.substring(1), t.substring(++i));
else
return false;
}
return b;
}
}

Submission Detail

  • 15 / 15 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Is Subsequence.
  • Memory Usage: 37.5 MB, less than 62.77% of Java online submissions for Is Subsequence.

Iterative

© JAVA clean, quick, concise solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public boolean isSubsequence(String s, String t) {
final int len_s = s.length();
final int len_t = t.length();
if (len_s > len_t)
return false;
int i = 0;
for (int j = 0; j < len_t && i < len_s; ) {
if (s.charAt(i) == t.charAt(j)) i++;
j++;
}
return i >= len_s;
}
}

Submission Detail

  • 15 / 15 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Is Subsequence.
  • Memory Usage: 37.1 MB, less than 39.06% of Java online submissions for Is Subsequence.