Programming is an art that cannot be learned in a year or so. You need to practice it continuously and regularly to be pro at it. But, before becoming a pro, some basic fundamentals and foundations are important. This guide will tell you about some of the efficient practices to use while programming.
Sounds exciting? Let’s get right into it!
I won’t spend too much time here since if you’re reading this, you likely have a pretty solid idea of what programming and coding is.
It’s just a formalized version of the logic, in the vocabulary and syntax of the…
I’m sure you heard a lot of people complaining that Python is so slow. I see people compare Python to C in the context of performance only, but they don’t compare in the context of fast development.
It is a dynamically-typed language meaning its variable types are not predefined, although, this is a double-edged sword as being dynamically-typed is what makes Python such an elegant language. So Python is a slower language to run, but faster to type.
Let’s look at some minor tips that could have a major impact on your overall code performance in the long run.
The first approach for this problem that comes to mind is to sort the array. Thus approach will work in O(nlogn) if we sorting techniques like merge sort. So that’s the naive solution to this problem.
def sortColors(self, nums: List[int]) -> None:
Time Complexity: O(nlogn)
Space Complexity: No extra space
The efficient approach is to take 3 pointers i.e. low, high, and mid. Start the low and mid pointer from the 0th index and the high pointer to the last index. Now consider 3 cases:
You are given a 0-indexed integer array
nums and an integer
You are initially standing at index
0. In one move, you can jump at most
k steps forward without going outside the boundaries of the array. That is, you can jump from index
i to any index in the range
[i + 1, min(n - 1, i + k)] inclusive.
You want to reach the last index of the array (index
n - 1). Your score is the sum of all
nums[j] for each index
j you visited in the array.
Return the maximum…
Given two integer arrays
preorder is the preorder traversal of a binary tree and
inorder is the inorder traversal of the same tree, construct and return the binary tree.
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Input: preorder = [-1], inorder = [-1]
- 1 <= preorder.length <= 3000
- inorder.length == preorder.length
- -3000 <= preorder[i], inorder[i] <= 3000
- preorder and inorder consist of unique values
- Each value of inorder also appears in preorder
- preorder is guaranteed to be the preorder traversal of the…
You are given an integer array
cost[i] is the cost of
ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index
0, or the step with index
Return the minimum cost to reach the top of the floor.
Input: cost = [10,15,20]
Explanation: Cheapest is: start on cost, pay that cost, and go to the top.
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Explanation: Cheapest is: start on cost, and only step on…
Given an unsorted array of integers
nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in
Input: nums = [100,4,200,1,3,2]
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Input: nums = [0,3,7,2,5,8,4,6,0,1]
- 0 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
def longestConsecutiveSequence(A: List[int], n: int) -> int:
longestStreak = 0
for i in range (n)…
You are given two integers
k and two integer arrays
efficiency both of length
n. There are
n engineers numbered from
efficiency[i] represent the speed and efficiency of the
ith engineer respectively.
Choose at most
k different engineers out of the
n engineers to form a team with maximum performance.
The performance of a team is the sum of their engineers’ speeds multiplied by the minimum efficiency among their engineers.
Return the maximum performance of this team. …
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example, we can turn
'0' to be
'9'. Each move consists of turning one wheel in one slot.
The lock initially starts at
'0000', a string representing the state of the 4 wheels.
You are given a list of
deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock…
Given a rectangular cake with height
h and width
w, and two arrays of integers
horizontalCuts[i] is the distance from the top of the rectangular cake to the
ith horizontal cut and similarly,
verticalCuts[j] is the distance from the left of the rectangular cake to the
jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays
verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.