Posts

669. Coin Change

注意这里用的一维dp vector,但需要额外的判断条件 dp[i - coins[j]] != INT_ MAX int coinChange ( vector < int > &coins, int amount) { // write your code here const int size = coins.size(); if (size == 0 ){ return 0 ; } vector < int > dp(amount + 1 , INT_MAX); dp[ 0 ] = 0 ; for ( int i = 1 ; i <= amount; i++){ for ( int j = 0 ; j < size; j++){ if (i >= coins[j] &&dp[i - coins[j]] != INT_ MAX){ int tmp = dp[i - coins[j]] + 1 ; dp[i] = min(dp[i], tmp); } } } return dp[amount] == INT_MAX ? -1 : dp[amount]; } 如果是用二维的,会是更general的方法,可用于解决 coin change II的问题。 int coinChange ( vector < int > &coins, int amount) { // write your code here const int size = coins.size(); if (size == 0 ){ return 0 ; } ...

心中杂念

当做一件事,只停留这件事上,不因未来的想象和过去的回忆打乱自己,那就是专注最好形态 。 只要没了杂念,就可心生智慧 改变焦虑,记住,不过度忧思,立刻行动,百种弊病,皆从懒生,戮力前行。 有人说焦虑是一种无能且懦弱的表现,既拿不起,也放不下,我觉得还是很有道理的。 但真正做出改变的时候,我们发现改变远比我想象地很难,于是我们就遭受了挫折,失望就取代了我们远比的快乐,然后引发焦虑和自我怀疑,最后我们就彻底放弃了努力。只有当我们 失控,再次需要希望 时,我们才会做出改变,于是这个 循环 就开始了。 一件事没做好,多多自我原谅,并用成长的心态看待问题,比如告诉自己这件事情让我学到很多,我又进步了,而不是一味批评自我。 众多研究表明:自我批评会降低积极性和自控力,而且也是最容易导致抑郁的力量,相反,自我同情这会相反。

1022. Valid Tic-Tac-Toe State

A Tic-Tac-Toe board is given as a string array  board . Return  true  if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game. The  board  is a 3 x 3 array, and consists of characters  ' ' ,  'X' , and  'O' . The  ' ' character represents an empty square. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares  ' ' . The first player always places  'X'  characters, while the second player always places  'O'  characters. 'X'  and  'O'  characters are always placed into empty squares, never filled ones. The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal. The game also ends if all squares are non-empty. No more moves can be played if the game is over. board  is a length-3 array of strings, where each string  board[i]  has length 3. ...

1497. Minimum Number of Refueling Stops

int minimumNumberofRefuelingStops ( int target, int startFuel, vector < vector < int >> &stations) { // Write your code here. //这题还是很难的,知道用DP,但不知道怎么定义state。想了想,怎么定义state都解释不通。 //还是解法思想,看了答案,定义state为停i次,油箱中一共加了多少油。 const int size = stations.size(); if (size == 0 ){ if (startFuel >= target){ return 0 ; } else { return -1 ; } } vector < long > dp(size + 1 , startFuel); dp[ 0 ] = startFuel; for ( int k = 0 ; k < size; k++){ //这里顺序反了就不对了。k在外还是i在外?看实际逻辑吧,要在实际 //物理意义中找到答案 for ( int i = 0 ; i <= k; i++){ if (dp[i] >= stations[k][ 0 ]){ dp[i + 1 ] = max(dp[i + 1 ], dp[i] + stations[k][ 1 ]); } } } for ( int i = 0 ; i <= size; i++){ if (dp[i] >= target){ ...