Leetcode Problems – Part 2

https://leetcode.com/problems/climbing-stairs/

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

 

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

 

Constraints:

  • 1 <= n <= 45
				
					function climbStairs(nums: number) {
  let one = 1;
  let two = 1;
  for (let i = nums - 1; i > 0; i--) {
    let temp = one;
    [one, two] = [one + two, temp];
  }
  return one;
}
				
			

Submission Detail

45 / 45 test cases passed.
Status: 

Accepted

Runtime: 75 ms
Memory Usage: 43.6 MB
Submitted: 3 months ago

https://leetcode.com/problems/two-sum/

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

 

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

 

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.

 

Follow-up: Can you come up with an algorithm that is less than O(n2time complexity?

				
					const twoSum = (arr: number[], target: number) => {
  const storeKeyValue: Map<number, number> = new Map();
  for (let i = 0; i < arr.length; i++) {
    let diff = target - arr[i];
    if (storeKeyValue.has(arr[i])) {
      return [storeKeyValue.get(arr[i]), i];
    }
    storeKeyValue.set(diff, i);
  }
};
//console.log(twoSum([1, 2, 8, 4, 2, 5, 6], 9));
//storeKeyValue - used Map Function now
//8:0 --check if 1 is present - no - store the difference -8
//7:1 --check if 2 is present - no - store the difference -7
//check here if arr[2] - 8 is present in - yes ==> index of 8 ..and current index
				
			

Submission Detail

57 / 57 test cases passed.
Status: 

Accepted

Runtime: 76 ms
Memory Usage: 45.4 MB
Submitted: 3 months ago

https://leetcode.com/problems/reverse-integer/

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

Constraints:

  • -231 <= x <= 231 - 1
				
					const reverse = (x: number) => {
  const absoluteNumber = Math.abs(x);
  const isNegative = x <= 0;
  let y = absoluteNumber.toString();
  if (y.length > 10 || (y.length === 10 && Number(y[y.length - 1]) >= 3))
    return 0;
  let reversed = Number(y.split("").reverse().join(""));
  return reversed > 2147483647 || reversed < -2147483648
    ? 0
    : isNegative
    ? -reversed
    : reversed;
};
console.log(reverse(1234567893));
				
			

Submission Detail

1032 / 1032 test cases passed.
Status: 

Accepted

Runtime: 70 ms
Memory Usage: 45.2 MB
Submitted: 3 months ago

https://leetcode.com/problems/reverse-words-in-a-string/

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

Example 1:

Input: s = "the sky is blue"
Output: "blue is sky the"

Example 2:

Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Example 3:

Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Constraints:

  • 1 <= s.length <= 104
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • There is at least one word in s.

Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?

				
					let s = "the   sky is blue";




function reverseWords(s: string): string {
  let currentWord = "";
  let result = "";
  for (let i = 0; i < s.length; i++) {
    const char = s[i];
    if (char !== " ") {
      currentWord += char;
    }
    if (currentWord.length > 0 && (char === " " || i === s.length - 1)) {
      result = result === "" ? currentWord + result : currentWord + " " + result;
      currentWord = "";
    }
  }
  return result;
}




console.log(reverseWords(s));
				
			

Submission Detail

58 / 58 test cases passed.
Status: 

Accepted

Runtime: 113 ms
Memory Usage: 44.9 MB
Submitted: 3 months ago

Leave a Comment