Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem Statement
- Alisha is given two arrays from which to construct and test her cache. The first array, commands, contains the list of commands to be executed on the cache, and the second array, inputs, provides the inputs for the given commands.
- She must ensure the cache removes the least recently used element once it exceeds its capacity. It should be noted that the least recently used element does not need to be the first element called nor the least used element. (An element is considered "used" when it is either retrieved or added/updated in the cache.)
- Requirements
- Implement the cache such that:
- It can be initialized with a capacity (a positive integer).
- When instructed with a get command, and given a single integer key, it will return the value of that key if the key exists in the cache; otherwise, it will return -1.
- When instructed with a put command, and given two integers key and value, it will update or add the value for key in the cache. If the number of keys exceeds the capacity due to this operation, then the least-recently used key must be evicted (removed) from the cache.
- The solution form for the commands get and put must have O(1) time complexity, and you can expect that the first command given will always be capacity to initialize the cache.
- Constraints
- 1≤capacity≤3000
- 0≤key≤10^4
- 0≤value≤10^6
- At most 2×10^5 calls will be made to get and put.
- Example
- Input:
- capacity put put get put get put get get get
- 2 1 1 2 2 1 3 3 2 4 4 1 3 4
- output:
- 1 -1 -1 3 4
- Explanation:
- capacity 2: Initializes the cache with capacity of 2.
- put 1, 1: Adds key 1 with value 1 to the cache.
- put 2, 2: Adds key 2 with value 2 to the cache.
- get 1: Retrieves the value of key 1 (1 is returned).
- put 3, 3: Adds key 3 with value 3 to the cache. The capacity is exceeded, so the least-recently used key (2) is removed from the cache.
- get 2: Tries to retrieve the value of key 2, but it no longer exists in the cache (-1 is returned).
- put 4 4: adds key 4 with value 4 to the cache. capacity is exceeded, so least-recently used key(1) is removed from the cache.
- get 1: tries to retrieve the value of key 1, but is no longer exists in the cache. (it returs -1)
- get 3: retrieves the value of key 3(return 3)
- get 4: retrieves the value of key 4 (return 4)
- important notes:
- the cache is always intialized first, and always with a posititve integer.
- package main
- import "fmt"
- func solution(a string) int32 {
- // Implement types and/or functions to manage the cache
- }
- func main() {
- // Implement how to read input (using e.g. fmt.Scanf()) and output results (using e.g. fmt.Println())
- }
- Description
- Winter is here! My friends and I are excitedly planning our trip to Goa. Now, since all the bars and clubs are too expensive there, we have pooled our money together for the expense of the entire trip. However, things are not that easy. Like every group, we have some internal politics going on. Since some people have a huge cold war going on between them, so if either one of them goes, the other person would bail out. But, we need to maximize our pooled money! While my friend Mohit is trying to solve the problem because of how great he is at money matters, he needs someone to double-check his work. Can you help him out?
- Input:
- The first line contains 8 space-separated integers denoting the money contributed by each member in order.
- The next line will contain the total number of pairs having a cold war between them. Let us denote this by P.
- The next P lines will contain 2 numbers separated by a space showing the members having a cold war.
- Output:
- The output will give the maximum amount of money that can be pooled.
- Constraints:
- Numbers used to denote members will be (1-8) for every 8 members.
- The pairs having the cold wars will be numbered between 1 - 8 as per the member.
- Every input is guaranteed to easily fit in 32-bit integer type.
- Examples
- Input:
- Copy code
- 3 1 4 5 2 3 4 1 9
- 4
- 1 2
- 2 3
- 4 5
- 7 8
- Output:
- Copy code
- 30
- Explanation:
- To maximize the pooled money, members 2, 5, 6, and 8 will go to Goa.
- package main
- import (
- "bufio"
- "fmt"
- "io"
- "os"
- "strconv"
- "strings"
- )
- func fun(arr []int32, edges [][]int32) int32 {
- // Write your code here
- return 0 // Placeholder, implement the logic
- }
- func main() {
- reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
- arrCount, err := 8
- checkError(err)
- var arr []int32
- arrTemp := strings.Split(strings.TrimRight(readLine(reader), "\r\n"), " ")
- for _, arrItem := range arrTemp {
- arrItemTemp, err := strconv.ParseInt(arrItem, 10, 64)
- checkError(err)
- arrItem := int32(arrItemTemp)
- arr = append(arr, arrItem)
- }
- if len(arr) != int(arrCount) {
- panic("Bad input")
- }
- edgesRows, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
- checkError(err)
- edgesColumns := 2
- var edges [][]int32
- for i := 0; i < int(edgesRows); i++ {
- edgesRowTemp := strings.Split(strings.TrimRight(readLine(reader), "\r\n"), " ")
- var edgesRow []int32
- for _, edgesRowItem := range edgesRowTemp {
- edgesItemTemp, err := strconv.ParseInt(edgesRowItem, 10, 64)
- checkError(err)
- edgesItem := int32(edgesItemTemp)
- edgesRow = append(edgesRow, edgesItem)
- }
- if len(edgesRow) != int(edgesColumns) {
- panic("Bad input")
- }
- edges = append(edges, edgesRow)
- }
- result := fun(arr, edges)
- fmt.Printf("%d\n", result)
- }
- func readLine(reader *bufio.Reader) string {
- str, _, err := reader.ReadLine()
- if err == io.EOF {
- return ""
- }
- return strings.TrimRight(string(str), "\r\n")
- }
- func checkError(err error) {
- if err != nil {
- panic(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment