- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have two lists called walks and target. At the beginning we are at position 0 in a one-dimensional line. Now |walks[i]| represents the number of steps have been walked. And when walk[i] is positive then indicates walked right, and negative for left. When we walk, we move one block, that is the next or previous integer position. We have to find the number of blocks that's been walked on at least target number of times.

So, if the input is like walks = [3, -7, 2] target = 2, then the output will be 5, from the following figure, we can see [0, 1], [1, 2], [2, 3], [-4, -3], [-3, -2] are covered k = 2 times.

To solve this, we will follow these steps −

- pos := 0
- jumps := a hash map, where default value is 0 when key is not present
- for each dist in walks, do
- jumps[pos] := jumps[pos] + 1 if dist > 0 otherwise -1
- jumps[pos + dist] := jumps[pos + dist] - 1 if dist > 0 otherwise -1
- pos := pos + dist

- lastpos := 0
- level := 0
- total := 0
- for each position pos, and value val in the sorted key-value pairs of jumps, do
- if level >= target, then
- total := total + pos - lastpos

- level := level + val
- lastpos := pos

- if level >= target, then
- return total

Let us see the following implementation to get better understanding −

from collections import defaultdict def solve(walks, target): pos = 0 jumps = defaultdict(int) for dist in walks: jumps[pos] += 1 if dist > 0 else -1 jumps[pos + dist] -= 1 if dist > 0 else -1 pos += dist lastpos = level = total = 0 for pos, val in sorted(jumps.items()): if level >= target: total += pos - lastpos level += val lastpos = pos return total walks = [3, -7, 2] target = 2 print(solve(walks, target))

[3, -7, 2], 2

5

- Related Questions & Answers
- Program to find out number of blocks that can be covered in Python
- C++ program to count in how many ways we can paint blocks with two conditions
- Program to find island count by adding blocks into grid one by one in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to count how many ways we can cut the matrix into k pieces in python
- Program to find ith element by rotating k times to right
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to find total area covered by two rectangles in Python
- Walking Robot Simulation in Python
- Count how many times the substring appears in the larger String in Java
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- Get count of how many times a string appears in a MySQL column?
- Count how many times the given digital clock shows identical digits in C++
- How many diseases are spread by mosquitoes?
- Python Program to find out how many cubes are cut

Advertisements