Showing posts with label Java_coding. Show all posts
Showing posts with label Java_coding. Show all posts

Dec 1, 2022

Easy_Question27 : 3Sum - Given an integer array nums, return all the triplets

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Nov 30, 2022

Easy_Question26 : find the length of the longest substring without repeating characters.

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"

Output: 3

Explanation: The answer is "abc", with the length of 3.

Nov 1, 2022

Question 20 : Flattening Nested Collections using java 8

Example of a Nested Collection

List<List<String>> nestedList = asList(
  asList("one:one"), 
  asList("two:one", "two:two", "two:three"), 
  asList("three:one", "three:two", "three:three", "three:four"));

May 19, 2022

Easy_Question25 : Majority Element

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 

Easy_Question24: Convert BST to Greater Tree

Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

What is a greater sum tree: 

  • A greater sum tree is a tree in which every node contains the sum of all the nodes which are greater than the node.  see the example below

Easy_Question23 : Divisor game

Given an integer N and two players, A and B are playing a game. On each player’s turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn.

May 18, 2022

Easy_Question22 : Leaf-Similar Trees

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.

Easy_Question21 :Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array nums is monotone increasing if for all i <= jnums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= jnums[i] >= nums[j].

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

Easy_Question20 : Painting Fence

There is a fence with n posts, each post can be painted with one of the k colors.


find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color.

Note: n and k are non-negative integers.

Easy_Question19 : Merge Two Binary Trees

  • You are given two binary trees root1 and root2.
  • Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
  • Return the merged tree.
  • Note: The merging process must start from the root nodes of both trees.

Example:  
Input: 
     Tree 1            Tree 2                  
       2                 3                             
      / \               / \                            
     1   4             6   1                        
    /                   \   \                      
   5                     2   7                  

Output: Merged tree:
         5
        / \
       7   5
      / \     \
     5   2   7 

May 17, 2022

Easy_Question18 : Is Subsequence

  • A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. 

  • (ie, "ace" is a subsequence of "abcde" while "aec" is not).

Example 1:
  • input: s = "abc", t = "ahbgdc"
  • Output: true
Example 2:
  • Input: s = "axc", t = "ahbgdc"
  • Output: false

Easy_Question17 :Minimize cost of painting N houses such that adjacent houses have different colors

  • There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.
  • The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses.

  • Given an integer N and a 2D array cost[][3], where cost[i][0]cost[i][1], and cost[i][2] is the cost of painting ith house with colors redblue, and green respectively, the task is to find the minimum cost to paint all the houses such that no two adjacent houses have the same color.
Examples:
Input: N = 3, cost[][3] = {{14, 2, 11}, {11, 14, 5}, {14, 3, 10}}
Output: 10

Explanation: 
  • Paint house 0 as blue. Cost = 2.
  • Paint house 1 as green. Cost = 5. 
  • Paint house 2 as blue. Cost = 3.
  • Therefore, the total cost = 2 + 5 + 3 = 10.
Input: N = 2, cost[][3] = {{1, 2, 3}, {1, 4, 6}}
Output: 3

Easy_Question16 : Shortest Unsorted Continuous Subarray

Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.

Return the shortest such subarray and output its length.

Easy_Question15 : Lowest Common Ancestor in a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Easy_Question14 :Check if a binary tree is subtree of another binary tree

Given the roots of two binary trees root and subRoot, return true .

If there is a subtree of root with the same structure and node values of subRoot and false otherwise.

A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself.

Easy_Question13: Find All The Lonely Nodes

In a binary tree, a lonely node is a node that is the only child of its parent node. The root of the tree is not lonely because it does not have a parent node.

Easy_Question12 : Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Easy_Question11 : 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.

May 16, 2022

Easy_Question10 : 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?

Easy_Question9 : House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS