You've created a new programming language, and now you've decided to add hashmap support to it. Actually you are quite disappointed that in common programming languages it's impossible to add a number to all hashmap keys, or all its values. So you've decided to take matters into your own hands and implement your own hashmap in your new language that has the following operations:
insert x y - insert an object with key x and value y.
get x - return the value of an object with key x.
addToKey x - add x to all keys in map.
addToValue y - add y to all values in map.
To test out your new hashmap, you have a list of queries in the form of two arrays: queryTypes contains the names of the methods to be called (eg: insert, get, etc), and queries contains the arguments for those methods (the x and y values).
Your task is to implement this hashmap, apply the given queries, and to find the sum of all the results for get operations.
Example
For queryType = ["insert", "insert", "addToValue", "addToKey", "get"] and query = [[1, 2], [2, 3], [2], [1], [3]], the output should be hashMap(queryType, query) = 5.
The hashmap looks like this after each query:
1 query: {1: 2}
2 query: {1: 2, 2: 3}
3 query: {1: 4, 2: 5}
4 query: {2: 4, 3: 5}
5 query: answer is 5
The result of the last get query for 3 is 5 in the resulting hashmap.
For queryType = ["insert", "addToValue", "get", "insert", "addToKey", "addToValue", "get"] and query = [[1, 2], [2], [1], [2, 3], [1], [-1], [3]], the output should be hashMap(queryType, query) = 6.
The hashmap looks like this after each query:
1 query: {1: 2}
2 query: {1: 4}
3 query: answer is 4
4 query: {1: 4, 2: 3}
5 query: {2: 4, 3: 3}
6 query: {2: 3, 3: 2}
7 query: answer is 2
The sum of the results for all the get queries is equal to 4 + 2 = 6.
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.string queryType
Array of query types. It is guaranteed that each queryType[i] is either "addToKey", "addToValue", "get", or "insert".
Guaranteed constraints:
1 ≤ queryType.length ≤ 105.
[input] array.array.integer query
Array of queries, where each query is represented either by two numbers for insert query or by one number for other queries. It is guaranteed that during all queries all keys and values are in the range [-109, 109].
Guaranteed constraints:
query.length = queryType.length,
1 ≤ query[i].length ≤ 2.
[output] integer64
The sum of the results for all get queries.
[Python3] Syntax Tips
# Prints help message to the console
# Returns a string
def helloWorld(name):
print("This prints to the console when you Run Tests")
return "Hello, " + name

Answers

Answer 1

Answer:

Attached please find my solution in JAVA

Explanation:

long hashMap(String[] queryType, int[][] query) {

       long sum = 0;

       Integer currKey = 0;

       Integer currValue = 0;

       Map<Integer, Integer> values = new HashMap<>();

       for (int i = 0; i < queryType.length; i++) {

           String currQuery = queryType[i];

           switch (currQuery) {

           case "insert":

               HashMap<Integer, Integer> copiedValues = new HashMap<>();

               if (currKey != 0 || currValue != 0) {

                   Set<Integer> keys = values.keySet();

                   for (Integer key : keys) {

                       copiedValues.put(key + currKey, values.get(key) + currValue);

                   }

                   values.clear();

                   values.putAll(copiedValues);

                   currValue = 0;

                   currKey = 0;

               }

               values.put(query[i][0], query[i][1]);

               break;

           case "addToValue":

               currValue += values.isEmpty() ? 0 : query[i][0];

               break;

           case "addToKey":

               currKey += values.isEmpty() ? 0 : query[i][0];

               break;

           case "get":

               copiedValues = new HashMap<>();

               if (currKey != 0 || currValue != 0) {

                   Set<Integer> keys = values.keySet();

                   for (Integer key : keys) {

                       copiedValues.put(key + currKey, values.get(key) + currValue);

                   }

                   values.clear();

                   values.putAll(copiedValues);

                   currValue = 0;

                   currKey = 0;

               }

               sum += values.get(query[i][0]);

           }

       }

       return sum;

   }


Related Questions

Other Questions
What is the slope of the line y=8? HELP ME PLEASEsentence combining1. Ostriches are birds. Ostriches are the fastest in the world. Ostriches are the largest in the world. Ostriches are found in Africa. a. Found in Africa, ostriches are birds that are the fastest and the largest in the worldb striches, the fastest and largest birds in the world, are found in Africa. c. The fastest and largest birds are found in Africa and are ostriches.d. Ostriches are the fastest in the world and the largest in the world and are found in Africa. Mutations result in changes to genotype that can result in changes in phenotype. Explain how a mutation causes a change in the genotype of an organism AND may result in a change in the phenotype of an organism. Whose financial plan included:Combining states war debt into a national debt.Establishing a national bank.Stimulating and protecting American industriesJames MonroeThomas JeffersonAlexander HamiltonJohn Adams Akiko shot 204 baskets in 17 minutes. Find her basket rate in baskets per minute 13.5 x 12.1 what does it equal 2x + 5y = 6find the x-intercept of the line Q1; ___________ have almost half a set of electrons in the outer most energy level.1- metalloids2- metals3- nonmetalsQ2;________ are known to be gasses at room temperature.1- nonmetals2- metalloids3- metals Bella measured the heights of her corn stalks in centimeters. The heights are 81, 88, 69, 65, 87.What is the mean height of Bella's corn stalks?A. 69 cmB. 75 cmC. 78 cmD. 80 cmE. 81 cm 2s + (-4s) simplify The diameter of a circle is 16 yards. What is the circle's area?d=16 ydUse 3.14 for n. Records kept for a period of 30 weeks by adietician at a cafeteria show the followingweekly consumption of milk ( in gallons).200205210215220225230235240MilkingallonsProbability3/304/306/305/304/303/302/302/301/30Find the average number of gallons of milkconsumed per week. Out of 40 times at bat, Raul got 15 hits. Find Rauls batting average. Which invaders of the Roman Empire appear to have reached the city of Rome first? Would you describe breathing as a voluntary or involuntary process? Explain why you think so. A tree frog population lives in the canopy of a tropical rainforest.A mutation occurs in the species that causes stripes on their legs. Which of the following factors has the greatest effect on whether leg stripes will become more common in the tree frog population? A.If the reproduction rate of the tree frog population remains constant. B.If the new phenotype is present in more females than males. C.If the new phenotype increases the survival of the tree frogs in their environment. D.If enough food and water are available in the rainforest canopy to support the tree frog population.choose a,b,c,or d please actally answer Fay has a lemonade stand.She spends 42 on ingredients.She sells each glass of lemonade for 1.50.If she sells 30 glasses of lemonade, what is her total profit? Magda works at a charity for homeless people.She needs to make lunch for 15 people.Magda knows this rule to find the amount of rice she needs to cook.Magda thinks she needs to cook 10 cups of rice for 15 people.Is Magda correct? Not drawnaccuratelyAB is parallel to CD.LMN and PMQ are straight lines.MQ = MNBWork out the value of x. *(3 Points)Diagram not drawn accurately.-DThe value must be a number Describe the location of a. Antarctica b. The Arctic c. The Andes