Loading Image

Friday, October 13, 2023

LeetCode Daily Challenges: 746. Min Cost Climbing Stairs


746. Min Cost Climbing Stairs

You are given an integer array cost where 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 1.

Return the minimum cost to reach the top of the floor.


Example 1:
Input: cost = [10,15,20]
Output: 15
Explanation: You will start at index 1.
- Pay 15 and climb two steps to reach the top.
The total cost is 15.
Example 2:
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: You will start at index 0.
- Pay 1 and climb two steps to reach index 2.
- Pay 1 and climb two steps to reach index 4.
- Pay 1 and climb two steps to reach index 6.
- Pay 1 and climb one step to reach index 7.
- Pay 1 and climb two steps to reach index 9.
- Pay 1 and climb one step to reach the top.
The total cost is 6.


Intuition:

The problem is about finding the minimum cost to reach the top of the staircase by either climbing one or two steps at a time. To solve this, we can use dynamic programming to keep track of the minimum cost to reach each step.


Approach:

  1. Initialize an array dp of the same length as the cost array to store the minimum cost for each step.
  2. Initialize dp[0] and dp[1] with the costs of the first two steps, as you can start from either step 0 or step 1.
  3. Iterate through the array starting from step 2 and for each step i, update dp[i] as the minimum of the cost to reach step i-1 plus the cost of step i and the cost to reach step i-2 plus the cost of step i. This ensures that you choose the minimum cost path to reach the current step.
  4. After iterating through all the steps, the minimum cost to reach the top of the staircase is the minimum of the last two elements in the dp array.

Complexity:

Time complexity: The algorithm iterates through the cost array once, so the time complexity is O(n), where n is the number of steps in the staircase.

Space complexity: We use an additional array dp of the same length as the cost array, so the space complexity is O(n) to store the dynamic programming table. 


Code Snapshot:




Thursday, October 12, 2023

LeetCode Daily Challenges: 557. Reverse Words in a String III

 557. Reverse Words in a String III



Problem Description:

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Example 2:

Input: s = "God Ding"
Output: "doG gniD"


Intuition

The problem requires us to reverse the order of characters in each word within a sentence while preserving whitespace and the initial word order. To achieve this, we can split the sentence into words, reverse each word individually, and then join them back together with whitespace to form the reversed sentence.

Approach

  1. Split the input sentence into words: We can split the sentence into words using whitespace as the delimiter, which will give us an array of words.
  2. Reverse each word: For each word in the array, we reverse its characters. We can use a StringBuilder to efficiently reverse the characters of a word.
  3. Join the reversed words: Finally, we join the reversed words back together with whitespace to form the reversed sentence.


Complexity

  1. Time complexity: The time complexity is determined by the length of the input sentence and the number of words it contains. Let's denote the length of the input sentence as n and the average word length as k. Splitting the sentence takes O(n) time, and reversing each word takes O(k) time, so the overall time complexity is O(n * k).
  2. Space complexity: The space complexity depends on the storage required for the array of words and the reversed sentence. Since we are not using any additional data structures apart from the input and output, the space complexity is O(n), where n is the length of the input sentence.

Code Snapshots:













Saturday, September 5, 2020

Software Engineer in Taurus IT


Taurus IT
Via: Skype
Post: Software Engineer
Interview Date: 9/05/2020

  1. Details about Polymorphism.
  2. Write down all of the DML  commands in SQL.
  3. How Constructor work?
  4. Why do we use the TRUNCATE  keyword in SQL?
  5. Difference between view and table, why do we use view?
  6. How many kinds of access modifiers are used in JAVA?
  7. Can we use multiple modifiers in the same class?
  8. How to insert 1000 data into MySQL database from an excel file.
  9.  How to set up an MYSQL database connection in PHP.
  10. Is  JAVA string mutable or immutable?
  11.  Difference between Where and Having in SQL.





Thursday, September 3, 2020

GET and POST Method in Http JAVA

HTTP: The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.
HTTP works as a request-response protocol between a client and a server.
                                        
                                                                    Figure: GET & POST in Systems

Some Properties of the GET method:
GET is used to request data from a specified resource. It appends form-data to the URL in name/ value pairs. It is a read-only method. The length of the URL is limited by 2048 characters. This method must not be used if you have a password or some sensitive information to be sent to the server. It is used for submitting the form where the user can bookmark the result. It is better for data that is not secure. It cannot be used for sending binary data like images or word documents. It also provides $_GET associative array to access all the sent information using the GET method. 

Some Properties of the POSTmethod:
POST is used to send data to a server to create/update a resource. It is a write method. Using this user can update or push his data into the server. 
This method does not have any restrictions on data size to be sent. Submissions by form with POST cannot be bookmarked. This method can be used to send ASCII as well as binary data like image and word documents. Data sent by the POST method goes through HTTP header so security depends on the HTTP protocol. You have to know that your information is secure by using secure HTTP. This method is a little safer than GET because the parameters are not stored in browser history or in web server logs. It also provides $_POST associative array to access all the sent information using the POST method.


Monday, August 17, 2020

OOP Basics

OOP is a design philosophy. It stands for Object-Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages.

Object

An object can be considered a "thing" that can perform a set of related activities. OOP terms of an object is an instance of a class. 
My name is Khalid, and I am an example/object of class men. When we say, being human, male or female, we, you, your friend, to me we are forms of this class. A class is simply a logical definition when we have a physical existence. We are objects.

Class

A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is a blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

OOP has 4 key concepts

These are  
  1. Encapsulation 
  2. Abstraction 
  3.  Inheritance  
  4.  Polymorphism.


1. Encapsulation:

The encapsulation is the inclusion-within a program object-of all the resources needed for the object to function, basically, the methods and the data.  It also enables the reusability of an instant of an already implemented class within a new class while hiding & protecting.  Those variables that are declared in private that have to direct access from outer. These variables are seen as hidden from the outer class, This situation is known as data hiding

To Acces in those variables, there are need some public method as Getter() and Setter(), by using these two methods privately declared variables can be accessed. 

Benefits:
  • Data hiding
  • Classes reusability
  •  Code Can be modified without breaking the main code.

2. Inheritance

Inheritance is a way of organizing classes. The term comes from the inheritance of traits like eye color, hair color, and so on. Classes with properties in common can be grouped so that their common properties are only defined once in the parent class. 
Superclass: Inherit its attributes & methods to the subclass. Subclass:  can inherit all its superclass attributes & methods besides having its own unique attributes & methods.





3. Abstraction

Data Abstraction is one of the essential and important features of object-oriented programming in C ++. Abstraction means simply showing the necessary information and hiding the details. Data abstraction refers to providing only the necessary information about data to the outside world by hiding the details or implementation of the background.

For example, Humans can talk, walk, listen, eat, but the details are hidden from the outside world. We can take our skin as the cause of abstraction in our case, hiding the internal process.


4. polymorphism

The word polymorphism means many forms. Simply, we can define polymorphism as the ability to display messages in multiple forms.

At the same time, a person may have different characteristics. A man has a father, a husband, an employee at the same time. So the same person is entitled to different behaviors in different situations. This is called polymorphism.

If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called Overloading.

And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called Overriding.