Skip to main content

Posts

Leetcode 79: Word Search

  1. Problem Statement (Simple Explanation) You’re given: A 2D grid of characters board (size m x n). A string word. You must determine if word  exists  in the grid. Rules: The word must be constructed from letters of  sequentially adjacent cells . Adjacent cells are  horizontally or vertically  neighboring (no diagonals). The same cell cannot be used more than once  in a given word path. Return true if word exists in board, otherwise false. 2. Examples Example 1: Input: board = [   ["A","B","C","E"],   ["S","F","C","S"],   ["A","D","E","E"] ] word = "ABCCED" One possible path: (0,0)A → (0,1)B → (0,2)C → (1,2)C → (2,2)E → (2,1)D Output: true Example 2: Input: board = [   ["A","B","C","E"],   ["S","F","C","S"],   ["A","D","E","E"] ] word = ...

Maximum Weight Difference

  Chef and his son bought N items. Item i weighs Wi grams. The son insists on carrying some items, under these rules: The items are split into  two groups . One group contains  exactly K items . Chef carries the  heavier  group. His son carries the  other  group. Chef wants the  difference  between his weight and his son’s weight to be  as large as possible  (while the son is still happy with the rule). Your task: For each test case, compute the  maximum possible difference : Difference = (Chef's total weight) - (Son's total weight) Key Idea: Let: Total sum of weights: S = ∑ (i=1 to N) Wi We must choose  exactly K items  to form the son’s set  or  choose N-K items for the son, because the rule only fixes group size but does  not  say which group belongs to whom; Chef always takes the heavier one. Two scenarios: Son gets  K items : Le...