We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Dynamic Programming
  4. Best spot
  5. Discussions

Best spot

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • manvi_muskan
    3 years ago+ 0 comments

    Can someone suggest me ways to optimise my code ?

    r, c = map(int, input().split())
    l, t, smallest_sum, point = [], [], None, None
    
    for i in range(r):
        l.append(list(map(int,input().split())))
    
    h, w = map(int, input().split())
    for i in range(h):
        t.append(list(map(int,input().split())))
    
    for i in range(r-h+1):
        for j in range(c-w+1):
            temp_sum = 0
            for m in range(h):
                for n in range(w):
                    temp_sum += (l[i+m][j+n] - t[m][n])**2
            if smallest_sum == None or smallest_sum > temp_sum:
                    smallest_sum = temp_sum
                    point = i,j,
    print("{}\n{} {}".format(smallest_sum, point[0]+1, point[1]+1))
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy