Day 1 of #100DaysOfCode
What I learned?
I learned the following topics:
Two-pointer Approach to solve array problem
Solved 1 leetcode easy question 283.Move Zeroes
Solve 1 codeforces question 1360B. Honest Coach
Object literals, this keyword, instanceof operator, constructor function
What I develop/solved?
Solved 1 leetcode easy question 283.Move Zeroes
Solved 1 codeforces question 1360B. Honest Coach which uses greedy and sorting
Code snippet/Screenshots/Notes
- Leetcode 283. Move all Zeros to the end of the array
- Brute approach
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int size = nums.size();
vector<int> temp; //additonal array
for(int i = 0; i<size; i++){
if(nums[i] != 0){
temp.push_back(nums[i]); //storing non-zeros to additional array
}
} // O(n)
for(int i = 0; i<temp.size(); i++){
nums[i] = temp[i]; //put non-zeros to orignal array
} //O(x)
for(int i = temp.size(); i<size; i++){
nums[i] = 0; //fill remaining array with zeros
} // O(n-x)
}
};
Time Complexity: O(n) + O(x) + O(n-x) ~ O(2*n) = O(n) Where n = total no. of elements, x = total no. of non-zero numbers, n-x = total no. of zeros
Space complexity: O(n), because we are using temporary array
- Optimal Approach
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int j = -1;
for(int i = 0; i<nums.size(); i++){
if(nums[i] == 0){ //finding the first 0 and j is assigned to it
j = i;
break;
}
}
if(j == -1){ //it means, there are no zero elements in array
return;
}
for(int i = j+1; i<nums.size(); i++){ //start iterating from j+1
if(nums[i] != 0){ //finding non-zero element
swap(nums[j], nums[i]); //and then swap j'th and i'th element
j++; //increasing j to make sure it remains on first 0
}
}
}
};
Time complexity: O(n) + O(n) = O(2n) = O(n)
Space Complexity: O(1), because we are not using any extra space to solve the problem
- Codeforces 1360B. Honest Coach(based on greedy and sorting)
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> arr(n, 0);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr.begin(), arr.end());
int finalAnswer = INT_MAX;
for(int i = 1; i<n; i++){
int diff = abs(arr[i] - arr[i-1]);
if(diff < finalAnswer){
finalAnswer = diff;
}
}
cout << finalAnswer << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
Objects and classes in JavaScript
Javascript is a prototype-based language and its classes are primarily syntactic sugar.
Object is a collection of properties and methods
Parts of OOPs
Constructor function
Prototypes
Classes
Instances (new, this)
4 Pillars of OOPs
Abstraction
Encapsulation
Inheritance
Polymorphism
Object Literal
//object literal
const userDetails = {
//properties
username: "randomUsername",
name: "username",
loginCnt: 5,
signedIn: true,
//methods
getUserDetails: function () {
console.log(`username is: ${this.username}`);
}
}
userDetails.getUserDetails();
This keyword
this
keyword refers to the context within which a function is executed
const obj = {
name: 'John',
greet: function() {
console.log('Hello, ' + this.name);
}
};
obj.greet(); // `this` refers to the `obj` object
instanceof operator
function Animal() {}
const dog = new Animal();
console.log(dog instanceof Animal); // true
Contructor function
function User(username, loginCount, isLoggedIn) {
this.username = username
this.loginCount = loginCount
this.isLoggedIn = isLoggedIn
this.greeting = function(){
console.log(`Hello ${this.username}! How are you?`)
}
return this
}
/* Empty object created when use new keyword */
const userOne = new User("ashCode98", 2, true);
const userTwo = new User("random", 1, false);
console.log(userOne)
console.log(userTwo)
Subscribe to my newsletter
Read articles from Ashish prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashish prajapati
Ashish prajapati
I am a front-end web developer and currently exploring new things about backend development