DEV Community

Ashish Prajapati
Ashish Prajapati

Posted on

Day 8 of #100DaysOfCode

What I learned?

I learned the following topics:

  • getOwnPropertyDescriptor method in JS
  • defineProperty method in JS
  • Iteratng through Object

What I developed/solved?

  • Solved leetcode problem 75. Sort colors using Dutch national flag algorithm

Code snippet/Screenshots/notes

  1. Leetcode 75. Sort Colors
  • Problem Statement: Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent
  • example.
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Enter fullscreen mode Exit fullscreen mode
Input: nums = [2,0,1]
Output: [0,1,2]
Enter fullscreen mode Exit fullscreen mode
  • Better solution:
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int cnt0 = 0; //stores the numbers of 0's
        int cnt1 = 0; //stores the numbers of 1's
        int cnt2 = 0; //stores the numbers of 2's
        for(int i = 0; i<nums.size(); i++){
            if(nums[i] == 0)cnt0++;
            else if(nums[i] == 1)cnt1++;
            else cnt2++;
        }

        //rewriting first cnt0 places in original array
        for(int i = 0; i<cnt0; i++){
            nums[i] = 0;
        }

        // rewriting cnt1 places after 0's in array
        for(int i = cnt0; i<cnt0+cnt1; i++){
            nums[i] = 1;
        }

        //rewriting cnt2 places after 1's
        for(int i = cnt1+cnt2; i<cnt0+cnt1+cnt2; i++){
            nums[i] = 2;
        }
    }
};
/*
Dry Run: nums = [2,0,2,1,1,0]
cnt0 = 2, cnt1 = 2, cnt2 = 2

range[0,2], two iterations 
for(0 -> 2)
    nums[0] = 0
    nums[1] = 0

changed array: nums = [0, 0, 2, 1, 1, 0]

     [cnt1, cnt0+cnt1]
range[2,4], two iterations
for(2 -> 4)
    nums[2] = 1
    nums[3] = 1

changed array: nums = [0, 0, 1, 1, 1, 0]

     [cnt1+cnt2, cnt0+cnt1+cnt2]
range[4,6], two iterations
for(4 -> 6)
     nums[4] = 2
     nums[5] = 2

 final Sorted Array: nums = [0, 0, 1, 1, 2, 2]  
*/
Enter fullscreen mode Exit fullscreen mode
  • Optimal Solution using Dutch national flag Algorithm:
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int low = 0;
        int mid = 0;
        int high = nums.size()-1;
        while(mid <= high){
            if(nums[mid] == 0){
                swap(nums[mid], nums[low]);
                low++;
                mid++;
            }
            else if(nums[mid] == 1){
                mid++;
            }
            else{
                swap(nums[high], nums[mid]);
                high--;
            }
        }
    }
};
Enter fullscreen mode Exit fullscreen mode

  • Object.getOwnPropertyDescriptor(): static method returns an object describing the configuration of a specific property on a given object
// example
const Descriptor = Object.getOwnPropertyDescriptor(Math, "PI");
/* this method takes two parameters (object, "property") */
console.log(Descriptor);
/*
{
  value: 3.141592653589793,
  writable: false,
  enumerable: false,
  configurable: false
} 
*/

/* in this scenario Math.PI value can't be overwrite, because the writable and enumerable perperties set to false
*/ 
Enter fullscreen mode Exit fullscreen mode
  • Object.defineProperty(): static method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.
const user = {
  name: "user",
  email: "user@gmail.com",
  password: "pwd"
}

console.log(Object.getOwnPropertyDescriptor(user, "email"))
/* 
{
    value: 'user@gmail.com',
    writable: true,
    enumerable: true,
    configurable: true
}
*/

//can use defineProperties for changing multiple properties
Object.defineProperty(user, "email", {  
  writable: false,
  enumerable: false,
  configurable: false
})

console.log(Object.getOwnPropertyDescriptor(user, "email"))
/*
{
  value: 'user@gmail.com',
  writable: false,
  enumerable: false,
  configurable: false
}
*/
Enter fullscreen mode Exit fullscreen mode

This is how you can iterate through object

for (let [key, value] of Object.entries(user)) {
    if (typeof value !== "function") {
        console.log(`${key} : ${value}`)
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)