Range of shelves from 1 to 99 inclusive Given list of integers, reshelve them to the minimum value possible and return the array in the same order though.
Example 1
Input
[1, 12, 4, 12] - List of shelves
Output
[1, 3, 2, 3]
Explanation - Starting shelf is 1, so that remains same, next lowest available value is 2, which can be assigned to 4 and the next available shelf is 3 that can be assigned to 12.
Example 2
Input
[1, 2, 6, 5]
Ouput
[1, 2, 4, 3]
Given List of String represnting each order, from 2 customers, and each character in the order representing item, find if orders were similar. Conditions for being similar, should be same in length and number of distinct characters should not be more than 3.
Example -
Input
x : ["nnbnnb", "ammxdf"]
y : ["bbncbb", "aecxdf"]
Output
["YES", "NO"]
Explanation
x[0] is similar to y[0] as both same in length and
x[0] has 4 "n", y[0] has 1 "n", so delta is 3,
x[0] has 2 "b", y[0] has 2 "b", so delta is 2,
x[0] has 0 "c", y[0] has 1 "c", so delta is 1
Given list of strings which are logs and an integer representing maximum time, determine which user has activity time of less than or equal to the maximum time. Sort and return those list of users.[Sorting was based on user number or their activity time not specified]
Input
logs : ["12 10 sign-in","5 10 sign-in", "5 100 sign-out", "12 20 sign-out", "8 20 sign-in", "8 50 sign-out"]
maximumTime : 30
Output
[8, 12]
Explanation
User 12 - logged in time period - (signout time - signin time) - 20 - 10 = 10 < 30
User 5 - logged in time period - (signout time - signin time) - 100 - 10 = 90 > 30
User 8 - logged in time period - (signout time - signin time) - 50 - 20 = 30 == 30
Question1
Overview
Solution/Explanation
map<int, int>M
and for each element in that dummy array we are going to iterate it and check if the cur element is already present in the map or not if(!M[dummy[i]])
if the current element is not present in the map we are going to assign M[dummy[i]]=to_be_assigned; to_be_assigned++
it to our map and increase our to_be_assigned value.Result[i]=M[orignal[i]]
Question 2
Overview
Solution
Question 3
Since we are getting strings, we can split the string by space and that way for each entry we will get three things : user_id, time, sign-in/out
. For each user we will have one or more sign in times and their corresponding sign out times. we can maintain a hash_map of user ids and for every sign in and corresponding sign out we can subtract and add the time to the value of that user id in the hash map. If there are multiple sign in and sign out times for a given user, then we will have the sum of all the activities as the total activity time of that user in the hash map. Now the next problem is to calculate the sign in and sign out time. Like we were maintaining a hash map for the total activity for every user, similarly we can maintain one for the sign in time of every user, and every time we come across a sign out time, we can subtract the corresponding sign in time from the has map. For every sign in time for user [u,t1,sign_in] :
hash_map_time[u] = t
For every sign out time for user [u,t2,sign_out]:
hash_map_activity[u] += t2 - hash_map_time[u]
Note that we are maintaining 2 hash maps as mentioned earlier, hash_map_activity
stores the total activity time and hash_map_time
stores the latest sign in time.
after we have all the activity time, we can put all the user_id which have an activity time less than the maximum time in an array, sort the array and print the user id's in a sorted format.