GfG Weekly - 175 [Rated Contest] All Solutions

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 11

  • @ArnabBhadra02
    @ArnabBhadra02  Місяць тому

    I was solving the qs in cpp and converted into python due to plag

  • @ArnabBhadra02
    @ArnabBhadra02  Місяць тому

    Soln 2:
    from typing import List
    class Solution:
    def boundarySum(self, n : int, matrix : List[List[int]]) -> List[int]:
    # code here
    sums = []
    for level in range((n + 1) // 2):
    level_sum = 0
    for i in range(level, n - level):
    level_sum += matrix[level][i]
    for i in range(level + 1, n - level):
    level_sum += matrix[i][n - level - 1]
    if n - level - 1 > level:
    for i in range(n - level - 2, level - 1, -1):
    level_sum += matrix[n - level - 1][i]
    if n - level - 1 > level:
    for i in range(n - level - 2, level, -1):
    level_sum += matrix[i][level]
    sums.append(level_sum)
    return sums

  • @hhhhhhhhhhhh0
    @hhhhhhhhhhhh0 Місяць тому +1

    Brother 3rd and 4th que ke solutions bhi sed kr do

    • @ArnabBhadra02
      @ArnabBhadra02  Місяць тому

      3rd kuch tc fail ho raha he
      Dusra compiler pe try karna hoga bahut wrong submission ho gya he
      Thodi wait karlo trying to figure it out

    • @saurabhmaurya2805
      @saurabhmaurya2805 Місяць тому

      @@ArnabBhadra02 hint - make each index i as a boundary index its mean that you can pick atmost x drinks of type A from left of index i and atmost y drinks of type B from right of index i (inclusive). and try to find maximum health.

    • @ArnabBhadra02
      @ArnabBhadra02  Місяць тому

      @@hhhhhhhhhhhh0 4th Done
      Check code and subscribe

  • @ArnabBhadra02
    @ArnabBhadra02  Місяць тому

    Soln 1:
    from typing import List
    class Solution:
    def maximumProduct(self, n : int, arr : List[int], l : int, r : int) -> int:
    mx = float('-inf')
    mn = float('inf')
    for i in range(l-1, r):
    mx = max(mx, arr[i])
    mn = min(mn, arr[i])
    mx2 = float('-inf')
    mn2 = float('inf')
    for i in range(0, l-1):
    mx2 = max(mx2, arr[i])
    mn2 = min(mn2, arr[i])
    for i in range(r, n):
    mx2 = max(mx2, arr[i])
    mn2 = min(mn2, arr[i])
    product1 = mx * mx2
    product2 = mn * mn2
    product3 = mx * mn2
    product4 = mn * mx2
    return max(product1, product2, product3, product4)

  • @nishudwivedi3478
    @nishudwivedi3478 Місяць тому

    Sol 3 and sol 4 please

    • @saurabhmaurya2805
      @saurabhmaurya2805 Місяць тому

      from typing import List

      from collections import defaultdict
      class Solution:
      def longestIncreasingPath(self, n: int, edges: List[List[int]], values: List[int]) -> int:
      # Create the adjacency list for the tree
      tree = defaultdict(list)
      for u, v in edges:
      tree[u].append(v)
      tree[v].append(u)
      # Memoization table to store results of longest path starting from each node
      memo = {}
      # Depth-First Search to find the longest increasing path from node u
      def dfs(u, parent):
      if u in memo:
      return memo[u]
      max_path = 1 # The minimum path length is 1 (the node itself)
      # Explore neighbors
      for v in tree[u]:
      if v != parent and values[v-1] > values[u-1]:
      max_path = max(max_path, 1 + dfs(v, u))
      memo[u] = max_path
      return max_path
      # Try starting DFS from each node and track the maximum increasing path length
      longest_path = 0
      for i in range(1, n + 1):
      longest_path = max(longest_path, dfs(i, -1))
      return longest_path

    • @ArnabBhadra02
      @ArnabBhadra02  Місяць тому

      Uploaded the 4th onr

  • @saurabhmaurya2805
    @saurabhmaurya2805 Місяць тому

    3 wala de do