Leetcode — Easy Problems & Solutions — Amazon (https://leetcode.com/company/amazon/)

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Input: num = 14
Output: 6
Explanation:
Step 1) 14 is even; divide by 2 and obtain 7.
Step 2) 7 is odd; subtract 1 and obtain 6.
Step 3) 6 is even; divide by 2 and obtain 3.
Step 4) 3 is odd; subtract 1 and obtain 2.
Step 5) 2 is even; divide by 2 and obtain 1.
Step 6) 1 is odd; subtract 1 and obtain 0.
Input: num = 8
Output: 4
Explanation:
Step 1) 8 is even; divide by 2 and obtain 4.
Step 2) 4 is even; divide by 2 and obtain 2.
Step 3) 2 is even; divide by 2 and obtain 1.
Step 4) 1 is odd; subtract 1 and obtain 0.
Input: num = 123
Output: 12

class Solution {
public int numberOfSteps (int num) {
int count=0;
while(num!=0){
if(num%2==0){
num=num/2;
count++;

}
else{
num=num-1;
count++;
}
}
return count;
}

}

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Explanation:
For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
For nums[1]=1 does not exist any smaller number than it.
For nums[2]=2 there exist one smaller number than it (1).
For nums[3]=2 there exist one smaller number than it (1).
For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
Input: nums = [6,5,4,8]
Output: [2,1,0,3]
Input: nums = [7,7,7,7]
Output: [0,0,0,0]

class Solution {
public int[] smallerNumbersThanCurrent(int[] nums) {

int[] arr = new int[nums.length];
for(int i=0;i<nums.length;i++){
int count=0;
for(int j=0;j<nums.length;j++){
if(nums[i]>nums[j])
count=count+1;
}
arr[i]=count;
}
return arr;
}
}

Input: ransomNote = "a", magazine = "b"
Output: false
Input: ransomNote = "aa", magazine = "ab"
Output: false
Input: ransomNote = "aa", magazine = "aab"
Output: true

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {

StringBuffer sb = new StringBuffer(magazine);
for(int i=0;i<ransomNote.length();i++){
if(sb.toString().contains(ransomNote.charAt(i)+””)){
sb.deleteCharAt(sb.indexOf(ransomNote.charAt(i)+””));
}
else{
return false;

}
}
return true;
}
}

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.

class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums.length==1){
if(nums[0]==0)
return 0;
else
return 1;
}


StringBuilder sb = new StringBuilder();
for(int i=0;i<nums.length;i++){
sb.append(String.valueOf(nums[i]));

}
String str = sb.toString();
if(!str.contains(“1”))
return 0;

String[] str1 = str.split(“0”);
Arrays.sort(str1);
return str1[str1.length-1].length();

}
}

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]

class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {

for(int i=0;i<n;i++){
nums1[m+i]=nums2[i];
}
Arrays.sort(nums1);
}
}

Input: s = "()"
Output: true
Input: s = "()[]{}"
Output: true
Input: s = "(]"
Output: false
Input: s = "([)]"
Output: false
Input: s = "{[]}"
Output: true

class Solution {
public boolean isValid(String s) {

if(s.length() % 2!=0)
return false;

Stack<Character> stack = new Stack();
for(char c : s.toCharArray()){
if(c==’(‘ || c==’{‘ || c==’[‘){

stack.push(c);
}else if(c==’)’ && !stack.isEmpty() && stack.peek()==’(‘){
stack.pop();
}
else if(c==’}’ && !stack.isEmpty() && stack.peek()==’{‘){
stack.pop();
}
else if(c==’]’ && !stack.isEmpty() && stack.peek()==’[‘){
stack.pop();
}
else {
return false ;
}

}
return stack.isEmpty();

}
}

Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
Input: s = "paper", t = "title"
Output: true

class Solution {
public boolean isIsomorphic(String s, String t) {
if(s.length()==0)
return true;

int count=0;
for(int i=0;i<s.length();i++){
if(s.indexOf(s.charAt(i))==t.indexOf(t.charAt(i)))
count=count+1;
}
if(count!=s.length())
return false;
else
return true;

}
}

Input: items = [[1,91],[1,92],[2,93],[2,97],[1,60],[2,77],[1,65],[1,87],[1,100],[2,100],[2,76]]
Output: [[1,87],[2,88]]
Explanation:
The student with ID = 1 got scores 91, 92, 60, 65, 87, and 100. Their top five average is (100 + 92 + 91 + 87 + 65) / 5 = 87.
The student with ID = 2 got scores 93, 97, 77, 100, and 76. Their top five average is (100 + 97 + 93 + 77 + 76) / 5 = 88.6, but with integer division their average converts to 88.
Input: items = [[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100],[1,100],[7,100]]
Output: [[1,100],[7,100]]

class Solution {
public int[][] highFive(int[][] items) {

HashMap<Integer, ArrayList<Integer>> hm = new HashMap<Integer, ArrayList<Integer>>();
for(int i=0;i<items.length;i++){

if(hm.containsKey(items[i][0])){
hm.get(items[i][0]).add(items[i][1]);
}
else{
hm.put(items[i][0],new ArrayList<Integer>());
hm.get(items[i][0]).add(items[i][1]);

}

}
int i=0;
int[][] a = new int[hm.size()][2];
for(Map.Entry<Integer, ArrayList<Integer>> entry : hm.entrySet()){
int sum=0;
int avg=0;
ArrayList<Integer> al = new ArrayList<Integer>();
al = entry.getValue();
Collections.sort(al);
for(int j=al.size()-1;j>=(al.size()-5);j — ){
sum=sum+al.get(j);
}
avg=sum/5;
a[i][0]=entry.getKey();
a[i][1]=avg;
i++;
}
return a;

}
}

Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Input: grid = [[1]]
Output: 4
Input: grid = [[1,0]]
Output: 4

class Solution {
public int islandPerimeter(int[][] grid) {
int per=0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]==1){
per=per+4;
if(i-1>=0 && grid[i-1][j]==1)
per=per-2;
if(j-1>=0 && grid[i][j-1]==1)
per=per-2;

}
}

}
return per;

}
}

Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]

class Solution {
public int[] replaceElements(int[] arr) {
int[] arr1 = new int[arr.length];
int lastbutone = arr[arr.length-1];
if(arr.length==1){
arr1[0]=-1;
return arr1;
}

for(int i=0;i<arr.length-2;i++){
for(int j=0;j<=i;j++){
arr[j]=0;

}
int ele = Arrays.stream(arr).max().getAsInt();
arr1[i]=ele;
}
arr1[arr.length-2]=lastbutone;
arr1[arr.length-1]=-1;
return arr1;
}
}

Input: [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

class Solution {
public int[] sortArrayByParityII(int[] A) {
int[] B = new int[A.length];
Stack<Integer> even = new Stack<Integer>();
Stack<Integer> odd = new Stack<Integer>();

for(int i=0;i<A.length;i++){
if(A[i]%2==0 || A[i]%2==2)
even.add(A[i]);
else
odd.add(A[i]);
}

for(int i=0;i<A.length;i++){
if(i%2==0 || i%2==2)
B[i]=even.pop();
else
B[i]=odd.pop();
}
return B;
}
}

Input: ["bella","label","roller"]
Output: ["e","l","l"]
Input: ["cool","lock","cook"]
Output: ["c","o"]

class Solution {
public List<String> commonChars(String[] A) {

String first = A[0];

List<String> li = new ArrayList<String>();
StringBuilder[] sb = new StringBuilder[A.length-1];
for(int i=0;i<sb.length;i++){
sb[i]=new StringBuilder(A[i+1]);
}
for(int i=0;i<first.length();i++){
int count=0;
for(int j=0;j<sb.length;j++)
{
for(int k=0;k<sb[j].length();k++){
if(sb[j].charAt(k)==first.charAt(i)){
sb[j].deleteCharAt(k);
count=count+1;
break;
}
}

}
if(count==sb.length)
li.add(first.charAt(i)+””);

}
return li;
}
}

Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]

class Solution {
public int[] sortedSquares(int[] nums) {

int[] squares = new int[nums.length];
for(int i=0;i<nums.length;i++){
squares[i]=nums[i]*nums[i];
}
Arrays.sort(squares);
return squares;
}
}

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

class Solution {
public String reverseWords(String s) {
String[] str = s.split(“ “);
StringBuilder[] sb = new StringBuilder[s.length()];
StringBuilder sb1 = new StringBuilder(“”);
for(int i=0;i<str.length;i++){
for(int j=str[i].length()-1;j>=0;j — ){
sb1.append(str[i].charAt(j));
}
sb1.append(“ “);

}
return sb1.toString().trim();
}
}

Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]

class Solution {
public void reverseString(char[] s) {

int i=0;
int j=s.length-1;
while(i<s.length/2 && j>=s.length/2){
char temp= s[i];
s[i]=s[j];
s[j]=temp;

i++;
j — ;
}
}
}

Input: nums = [2,2,1]
Output: 1
Input: nums = [4,1,2,1,2]
Output: 4
Input: nums = [1]
Output: 1

class Solution {
public int singleNumber(int[] nums) {
int num = 0;
int count=0;
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
for(int i=0;i<nums.length-1;i=i+2){
if(nums[i]<nums[i+1]){
System.out.println(nums[i]+” — “+nums[i+1]);
count=count+1;
num= nums[i];
break;

}

}
if(count==0)
num=nums[nums.length-1];
return num;
}
}

Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Input: grid = [[3,2],[1,0]]
Output: 0
Input: grid = [[1,-1],[-1,-1]]
Output: 3
Input: grid = [[-1]]
Output: 1

class Solution {
public int countNegatives(int[][] grid) {
int count =0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]<0)
count=count+1;
}

}
return count;
}
}

Input
["Logger", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage", "shouldPrintMessage"]
[[], [1, "foo"], [2, "bar"], [3, "foo"], [8, "bar"], [10, "foo"], [11, "foo"]]
Output
[null, true, true, false, false, false, true]
Explanation
Logger logger = new Logger();
logger.shouldPrintMessage(1, "foo"); // return true, next allowed timestamp for "foo" is 1 + 10 = 11
logger.shouldPrintMessage(2, "bar"); // return true, next allowed timestamp for "bar" is 2 + 10 = 12
logger.shouldPrintMessage(3, "foo"); // 3 < 11, return false
logger.shouldPrintMessage(8, "bar"); // 8 < 12, return false
logger.shouldPrintMessage(10, "foo"); // 10 < 11, return false
logger.shouldPrintMessage(11, "foo"); // 11 >= 11, return true, next allowed timestamp for "foo" is
// 11 + 10 = 21

class Logger {
HashMap<String, Integer> hm;

public Logger() {
hm = new HashMap<String, Integer>();

}
public boolean shouldPrintMessage(int timestamp, String message) {
for (Map.Entry<String, Integer> entry: hm.entrySet()){
}

if(hm.containsKey(message)){
if(timestamp-hm.get(message)>=10){
hm.put(message, timestamp);
return true;

}
else{
return false;
}

}
else{

hm.put(message, timestamp);
return true;

}

}

}

Input: s = "leetcodeisacommunityforcoders"
Output: "ltcdscmmntyfrcdrs"
Input: s = "aeiou"
Output: ""

class Solution {
public String removeVowels(String s) {
String str = s.replaceAll(“[aeiou]”,””);
return str;
}
}

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]

class Solution {
public int[] runningSum(int[] nums) {

int[] arr = new int[nums.length];
int ele=0;
for(int i=0;i<nums.length;i++){
ele=ele+nums[i];
arr[i]=ele;
}
return arr;
}
}

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation:
Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]

class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int maxEle = Arrays.stream(candies).max().getAsInt();
List<Boolean> li = new ArrayList<Boolean>();
for(int i=0;i<candies.length;i++){
if((candies[i]+extraCandies)>=maxEle)
li.add(true);
else
li.add(false);
}
return li;
}
}

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"

class Solution {
public String defangIPaddr(String address) {
String str = address.replaceAll(“\\.”,”[.]”);
return str;

}
}

Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.
Input: nums = [1,2,3]
Output: 0

class Solution {
public int numIdenticalPairs(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]==nums[j])
count=count+1;
}
}
return count;
}
}

Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
Input: jewels = "z", stones = "ZZ"
Output: 0

class Solution {
public int numJewelsInStones(String J, String S) {
int count = 0;
for(int i=0;i<J.length();i++){
for(int j=0;j<S.length();j++){
if(J.charAt(i)==S.charAt(j))
count=count+1;
}
}
return count;
}
}

Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]
Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

class ParkingSystem {
int big, medium, small;
public ParkingSystem(int big, int medium, int small) {
this.big=big;
this.medium=medium;
this.small=small;
}

public boolean addCar(int carType) {
if(carType==1 && big==0 || carType==2 && medium==0 || carType==3 && small==0)
return false;
if(carType==1)
big — ;
if(carType==2)
medium — ;
if(carType==3)
small — ;
return true;
}

}

Input: 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.
Input: 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

class Solution {
public boolean divisorGame(int N) {
if(N%2==0)
return true;
else
return false;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store