Overview
Let me start with a quote we all know but don't want to read or think of.
Procrastination is the thief of time.
At times, we used to find ways not to do work rather than get started with it. The 2-minute rule comes in handy when we want to achieve things (working towards a goal).
This rule is taken from Atomic Habits by James Clear. Let me simplify the rule given in that book.
"I want to get six packs" becomes "Just Go to the gym and work out for only 2 minutes".
- Eventually, we will work out much longer than expected, instead of being couch potatoes 🥔.
"I want to be a star coder" becomes "Open my laptop and start coding for 2 minutes".
- As a programmer myself we won't stop at 2 minutes and go on endlessly surfing in StackOverflow 🤪.
"I want to read 50 books in a year" becomes "Open the book and read for 2 minutes".
- Keep a book near the bed. This way we won't have to move and get the book 📕.
“A new habit should not feel like a challenge. The actions that follow can be challenging, but the first two minutes should be easy. What you want is a “gateway habit” that naturally leads you down a more productive path. “
- Atomic Habits, James Clear
The above might sound simple, but personally, I've tested it. And it works effectively. The ultimate aim of this rule is to get started and the rest of the things will follow. This is to trick our brains to work only for 2 minutes and that will get us towards our end goal. (where all we need to do is to "Get Started and Stay Consistent throughout the journey").
References
In case you want to check the book 📕.
The below video clearly explains what this rule is all about.
Bonus content
Question 1:
In this post, I will cover number 1 and the famous problem in leetcode. 😁
Given an array of integer nums and an integer target, return indices of the two numbers such that they add up to the target.
Explanation:
We need to get the indexes of 2 numbers where the sum of those should be equal to the target specified.
We will use the map to store the key as the number and index as the value (
key = num, value = index
).We will iterate over the numbers to see if the sum exists.
- If it exists we get the index from the map and the current index and return the result.
Solution:
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int length = nums.length;
for(int i = 0; i < length; i++) {
int temp = target - nums[i];
if(map.containsKey(temp)) {
int index1 = map.get(temp);
return new int[] {index1, i};
}
map.put(nums[i], i);
}
return new int[] {};
}
}
Next problem:
In the next post, I will cover the below problem.