国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares

這篇具有很好參考價值的文章主要介紹了Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

  • Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares
    • 1. 解題思路
    • 2. 代碼實現(xiàn)
  • 題目鏈接:2897. Apply Operations on Array to Maximize Sum of Squares

1. 解題思路

這一題事實上非常的簡單,我們只需要想明白一些關鍵點就行了。

題中最終的目標是獲得 k k k個數(shù),使得其平方和最大。因此,我們就只需要理解一下題中給出的操作會帶來怎樣的變化,以及要如何獲取最大的平方和即可。

首先,對于題中給出的位操作,事實上只有在一種情況下才會產生變化,即 i = 1 , j = 0 i=1, j=0 i=1,j=0的情況下,會變?yōu)?span id="n5n3t3z" class="katex--inline"> i = 0 , j = 1 i=0, j=1 i=0,j=1,其余情況都不會產生變化。因此,我們無法改變各個位上 0 , 1 0,1 0,1的數(shù)目總量,但是總可以通過一定的操作將其均勻化或者聚合到某幾個數(shù)上。

然后,如果要使得兩個數(shù)的平方和最大,我們考察某一位上的數(shù)最好是分布在較大的數(shù)還是在較小的數(shù)上,不妨令這個位上的數(shù)為 x x x,然后其余位上的數(shù)分別為 a , b a, b a,b,那么平方和就是:
( a + x ) 2 + b 2 = a 2 + 2 a x + x 2 + b 2 (a+x)^2 + b^2 = a^2 + 2ax+x^2 + b^2 (a+x)2+b2=a2+2ax+x2+b2

顯然, a a a較大時,可以獲得更大的平方和。

綜上,我們就可以獲得我們最終的結論:

  • 要取 k k k個數(shù),使得平方和最大,我們只需要統(tǒng)計每一位上擁有的 1 1 1的個數(shù),然后盡可能全部分配到這 k k k個數(shù)當中即可。

2. 代碼實現(xiàn)

給出最終的代碼實現(xiàn)如下:

class Solution:
    def maxSum(self, nums: List[int], k: int) -> int:
        MOD = 10**9+7
        digits = [0 for _ in range(32)]
        flag = [pow(2, i, MOD) for i in range(32)]
        for x in nums:
            idx = 0
            while x != 0:
                digits[idx] += x % 2 
                x = x // 2
                idx += 1
        
        ans = 0
        for i in range(k):
            x = 0
            for idx in range(32):
                if digits[idx] > 0:
                    digits[idx] -= 1
                    x += flag[idx]
            ans = (ans + x * x) % MOD
        return ans

提交代碼評測得到:耗時2510ms,占用內存30.6MB。文章來源地址http://www.zghlxwxcb.cn/news/detail-727575.html

到了這里,關于Leetcode 2897. Apply Operations on Array to Maximize Sum of Squares的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解題思路 2. 代碼實現(xiàn) 題目鏈接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 這一題我的思路上就是一個二分的思路,先確定一個上下界,然后不斷通過二分來找到最大的price不超過k的值。 因此,剩下的

    2024年01月20日
    瀏覽(24)
  • LeetCode //167. Two Sum II - Input Array Is Sorted

    Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 = index1 index2 numbers.length. Return the indices of the two numbers, index1 and index2 , added by one as an integer array [index1

    2024年02月15日
    瀏覽(29)
  • LeetCode --- 1929. Concatenation of Array 解題報告

    Given an integer array? nums ?of length? n , you want to create an array? ans ?of length? 2n ?where? ans[i] == nums[i] ?and? ans[i + n] == nums[i] ?for? 0 = i n ?( 0-indexed ). Specifically,? ans ?is the? concatenation ?of two? nums ?arrays. Return? the array? ans . Example 1: Example 2:

    2024年02月09日
    瀏覽(25)
  • LeetCode --- 1863. Sum of All Subset XOR Totals 解題報告

    The? XOR total ?of an array is defined as the bitwise? XOR ?of ?all its elements , or? 0 ?if the array is ?empty . For example, the? XOR total ?of the array? [2,5,6] ?is? 2 XOR 5 XOR 6 = 1 . Given an array? nums , return? the? sum ?of all? XOR totals ?for every? subset ?of? nums .? Note: ?Subsets with the? same ?elements should be c

    2024年02月15日
    瀏覽(26)
  • 小白水平理解面試經(jīng)典題目LeetCode 404 Sum of Left Leaves【Tree】

    小白水平理解面試經(jīng)典題目LeetCode 404 Sum of Left Leaves【Tree】

    給定二叉樹的root,返回所有左葉的總和。 葉子是沒有子節(jié)點的節(jié)點。左葉是另一個節(jié)點的左子節(jié)點的葉。 在大學某個自習的下午,小白坐在教室看到這道題。想想自己曾經(jīng)和白月光做題,現(xiàn)在大過年的,也是只有自己練題了。左邊一顆樹,右邊一棵樹。。。 這時候黑長直女

    2024年02月22日
    瀏覽(25)
  • LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal. ? Example 1: Input: root = [1,7,0,7,-8,null,null] Output: 2 **Explanation: ** Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return

    2024年01月23日
    瀏覽(19)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n ? 1 ? i ) t h (n-1-i)^{th} ( n ? 1 ? i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    瀏覽(86)
  • 模型評估(誤差平方和(SSE The sum of squares due to error))

    模型評估(誤差平方和(SSE The sum of squares due to error))

    舉例:(下圖中數(shù)據(jù)-0.2, 0.4, -0.8, 1.3, -0.7, 均為真實值和預測值的差) 在k-means中的應用: 公式各部分內容: 上圖中: k=2 SSE圖最終的結果,對圖松散度的衡量. (eg:? SSE(左圖)SSE(右圖) ) SSE隨著聚類迭代,其值會越來越小,直到最后趨于穩(wěn)定: 如果質心的初始值選擇不好,SSE只會達到一個不怎

    2024年02月04日
    瀏覽(17)
  • LeetCode 2475. Number of Unequal Triplets in Array【數(shù)組,排序,哈希表】簡單

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠。在這一系列刷題文章中,我不僅會講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(22)
  • LeetCode //2649. Nested Array Generator (Day 30 of LC JavaScript Challenage)

    Given a multi-dimensional array of integers, return a generator object which yields integers in the same order as inorder traversal. A multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays. inorder traversal iterates over each array from left to right, yielding any integers it encounters or apply

    2024年02月08日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包