Posts

Showing posts from January, 2021

Get Maximum in Generated Array

  You are given an integer   n . An array   nums   of length   n + 1   is generated in the following way: nums[0] = 0 nums[1] = 1 nums[2 * i] = nums[i]  when  2 <= 2 * i <= n nums[2 * i + 1] = nums[i] + nums[i + 1]  when  2 <= 2 * i + 1 <= n Return   the  maximum  integer in the array  nums ​​​. Example 1: Input: n = 7 Output: 3 Explanation: According to the given rules: nums[0] = 0 nums[1] = 1 nums[(1 * 2) = 2] = nums[1] = 1 nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2 nums[(2 * 2) = 4] = nums[2] = 1 nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3 nums[(3 * 2) = 6] = nums[3] = 2 nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3 Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3. Example 2: Input: n = 2 Output: 1 Explanation: According to the given rules, the maximum between nums[0], nums[1], and n...

Merge Sorted Array

Given two sorted integer arrays  nums1  and  nums2 , merge  nums2  into  nums1  as one sorted array. The number of elements initialized in  nums1  and  nums2  are  m  and  n  respectively. You may assume that  nums1  has enough space (size that is equal to  m + n ) to hold additional elements from  nums2 . Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6] Example 2: Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1] Constraints: 0 <= n, m <= 200 1 <= n + m <= 200 nums1.length == m + n nums2.length == n -10 9  <= nums1[i], nums2[i] <= 10 9 Solution : class Solution {     public void merge(int[] nums1, int m, int[] nums2, int n) {         int i;     ...

Check If Two String Arrays are Equivalent

Given two string arrays  word1  and  word2 , return   true  if the two arrays  represent  the same string, and  false  otherwise. A string is  represented  by an array if the array elements concatenated  in order  forms the string. Example 1: Input: word1 = ["ab", "c"], word2 = ["a", "bc"] Output: true Explanation: word1 represents string "ab" + "c" -> "abc" word2 represents string "a" + "bc" -> "abc" The strings are the same, so return true. Example 2: Input: word1 = ["a", "cb"], word2 = ["ab", "c"] Output: false Example 3: Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] Output: true   Constraints: 1 <= word1.length, word2.length <= 10 3 1 <= word1[i].length, word2[i].length <= 10 3 1 <= sum(word1[i].leng...

Merge Two Sorted Lists

Image
Merge two sorted linked lists and return it as a  sorted  list. The list should be made by splicing together the nodes of the first two lists. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] Constraints: The number of nodes in both lists is in the range  [0, 50] . -100 <= Node.val <= 100 Both  l1  and  l2  are sorted in  non-decreasing  order. Solution: /**  * Definition for singly-linked list.  * public class ListNode {  *     int val;  *     ListNode next;  *     ListNode() {}  *     ListNode(int val) { this.val = val; }  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; } ...