leetcode 665. 非遞減數列(貪心算法)

給你一個長度為 n 的整數數組,請你判斷在 最多 改變 1 個元素的情況下,該數組能否變成一個非遞減數列。

我們是這樣定義一個非遞減數列的: 對于數組中所有的 i (0 <= i <= n-2),總滿足 nums[i] <= nums[i + 1]。

示例 1:

輸入: nums = [4,2,3]
輸出: true
解釋: 你可以通過把第一個4變成1來使得它成為一個非遞減數列

貪心的思路在于讓前面子數組的結尾元素盡量少,那么后面元素可以滿足非遞減的情況的可能性越大

代碼

class Solution {public boolean checkPossibility(int[] nums) {int n=nums.length,cnt=0;if(n==1) return  true;for(int i=0;i<n-1;i++){if(nums[i+1]<nums[i])//當出現遞減時{if(++cnt>1) return false;//不滿足的位置加一if(i>0&&nums[i+1]<nums[i-1])
//num[i-1] nums[i] nums[i+1] 
//當出現遞減時,有兩種改變元素的方法,分別是 nums[i+1]=nums[i] 或者 nums[i]=nums[i-1]
//當nums[i+1]<nums[i-1],只能將元素變為  nums[i-1]   nums[i]   nums[i]
//當nums[i+1]>=nums[i-1],就將元素變為  nums[i-1]   nums[i-1]   nums[i+1]
//上面的兩種序列根據 已知條件 nums[i-1]<=nums[i] 可以證明均滿足非遞減nums[i+1]=nums[i];}}return true;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391883.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391883.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391883.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

django基于存儲在前端的token用戶認證

一.前提 首先是這個代碼基于前后端分離的API,我們用了django的framework模塊,幫助我們快速的編寫restful規則的接口 前端token原理: 把(token加密后的字符串,keyname)在登入后發到客戶端,以后客戶端再發請求,會攜帶過來服務端截取(token加密后的字符串,keyname),我們再利用解密…

數據分布策略_有效數據項目的三種策略

數據分布策略Many data science projects do not go into production, why is that? There is no doubt in my mind that data science is an efficient tool with impressive performances. However, a successful data project is also about effectiveness: doing the righ…

cell 各自的高度不同的時候

1, cell 根據文字、圖片等內容&#xff0c;確定自己的高度。每一個cell有自己的高度。 2&#xff0c;tableView 初始化 現實的時候&#xff0c;不是從第一個cell開始顯示&#xff0c;&#xff08;從第二個&#xff1f;&#xff09;&#xff0c;非非正常顯示。 a:cell 的高度問題…

leetcode 978. 最長湍流子數組(滑動窗口)

當 A 的子數組 A[i], A[i1], …, A[j] 滿足下列條件時&#xff0c;我們稱其為湍流子數組&#xff1a; 若 i < k < j&#xff0c;當 k 為奇數時&#xff0c; A[k] > A[k1]&#xff0c;且當 k 為偶數時&#xff0c;A[k] < A[k1]&#xff1b; 或 若 i < k < j&…

spring boot源碼下載地址

github下載&#xff1a; https://github.com/spring-projects/spring-boot/tree/1.5.x git地址&#xff1a; https://github.com/spring-projects/spring-boot.git 因為項目中目前使用的就是spring boot 1.5.19版本&#xff0c;因此這里先研究spring boot 1.5版本源碼.轉載于:h…

java基礎學習——5、HashMap實現原理

一、HashMap的數據結構 數組的特點是&#xff1a;尋址容易&#xff0c;插入和刪除困難&#xff1b;而鏈表的特點是&#xff1a;尋址困難&#xff0c;插入和刪除容易。那么我們能不能綜合兩者的特性&#xff0c;做出一種尋址容易&#xff0c;插入刪除也容易的數據結構&#xff1…

看懂nfl定理需要什么知識_NFL球隊為什么不經常通過?

看懂nfl定理需要什么知識Debunking common NFL myths in an analytical study on the true value of passing the ball在關于傳球真實價值的分析研究中揭穿NFL常見神話 Background背景 Analytics are not used enough in the NFL. In a league with an abundance of money, i…

Docker初學者指南-如何創建您的第一個Docker應用程序

您是一名開發人員&#xff0c;并且想要開始使用Docker&#xff1f; 本文是為您準備的。 (You are a developer and you want to start with Docker? This article is made for you.) After a short introduction on what Docker is and why to use it, you will be able to cr…

mybatis if-else(寫法)

mybaits 中沒有else要用chose when otherwise 代替 范例一 <!--批量插入用戶--> <insert id"insertBusinessUserList" parameterType"java.util.List">insert into business_user (id , user_type , user_login )values<foreach collection…

spring—攔截器和異常

SpringMVC的攔截器 SpringMVC攔截器-攔截器的作用 Spring MVC 的攔截器類似于 Servlet 開發中的過濾器 Filter&#xff0c;用于對處理器進行預處理和后處理。 將攔截器按一定的順序聯結成一條鏈&#xff0c;這條鏈稱為攔截器鏈&#xff08;InterceptorChain&#xff09;。在…

29/07/2010 sunrise

** .. We can only appreciate the miracle of a sunrise if we have waited in the darkness .. 人們在黑暗中等待著&#xff0c;那是期盼著如同日出般的神跡出現 .. 附&#xff1a;27/07/2010 sunrise ** --- 31 July 改動轉載于:https://www.cnblogs.com/orderedchaos/archi…

密度聚類dbscan_DBSCAN —基于密度的聚類方法的演練

密度聚類dbscanThe idea of having newer algorithms come into the picture doesn’t make the older ones ‘completely redundant’. British statistician, George E. P. Box had once quoted that, “All models are wrong, but some are useful”, meaning that no model…

node aws 內存溢出_在AWS Elastic Beanstalk上運行生產Node應用程序的現實

node aws 內存溢出by Jared Nutt賈里德努特(Jared Nutt) 在AWS Elastic Beanstalk上運行生產Node應用程序的現實 (The reality of running a production Node app on AWS Elastic Beanstalk) 從在AWS的ELB平臺上運行生產Node應用程序兩年的經驗教訓 (Lessons learned from 2 y…

Day2-數據類型

數據類型與內置方法 數據類型 數字字符串列表字典元組集合字符串 1.用途 用來描述某個物體的特征&#xff1a;姓名&#xff0c;性別&#xff0c;愛好等 2.定義方式 變量名 字符串 如&#xff1a;name huazai 3.常用操作和內置方法 1.按索引取值&#xff1a;&#xff08;只能取…

嵌套路由

父組件不能用精準匹配&#xff0c;否則只組件路由無法展示 轉載于:https://www.cnblogs.com/dianzan/p/11308146.html

leetcode 992. K 個不同整數的子數組(滑動窗口)

給定一個正整數數組 A&#xff0c;如果 A 的某個子數組中不同整數的個數恰好為 K&#xff0c;則稱 A 的這個連續、不一定獨立的子數組為好子數組。 &#xff08;例如&#xff0c;[1,2,3,1,2] 中有 3 個不同的整數&#xff1a;1&#xff0c;2&#xff0c;以及 3。&#xff09; …

從完整的新手到通過TensorFlow開發人員證書考試

I recently graduated with a bachelor’s degree in Civil Engineering and was all set to start with a Master’s degree in Transportation Engineering this fall. Unfortunately, my plans got pushed to the winter term because of COVID-19. So as of January this y…

微信開發者平臺如何編寫代碼_編寫超級清晰易讀的代碼的初級開發者指南

微信開發者平臺如何編寫代碼Writing code is one thing, but writing clean, readable code is another thing. But what is “clean code?” I’ve created this short clean code for beginners guide to help you on your way to mastering and understanding the art of c…

【轉】PHP面試題總結

PHP面試總結 PHP基礎1&#xff1a;變量的傳值與引用。 2&#xff1a;變量的類型轉換和判斷類型方法。 3&#xff1a;php運算符優先級&#xff0c;一般是寫出運算符的運算結果。 4&#xff1a;PHP中函數傳參&#xff0c;閉包&#xff0c;判斷輸出的echo&#xff0c;print是不是函…

Winform控件WebBrowser與JS腳本交互

1&#xff09;在c#中調用js函數 如果要傳值&#xff0c;則可以定義object[]數組。 具體方法如下例子&#xff1a; 首先在js中定義被c#調用的方法: function Messageaa(message) { alert(message); } 在c#調用js方法Messageaa private void button1_Click(object …