★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pjcsuhqe-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
There are N children standing in a line. Each child is assigned a rating value.git
You are giving candies to these children subjected to the following requirements:github
What is the minimum candies you must give?微信
Example 1:less
Input: [1,0,2] Output: 5 Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:ide
Input: [1,2,2] Output: 4 Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively. The third child gets 1 candy because it satisfies the above two conditions.
老師想給孩子們分發糖果,有 N 個孩子站成了一條直線,老師會根據每一個孩子的表現,預先給他們評分。ui
你須要按照如下要求,幫助老師給這些孩子分發糖果:spa
那麼這樣下來,老師至少須要準備多少顆糖果呢?code
示例 1:htm
輸入: [1,0,2] 輸出: 5 解釋: 你能夠分別給這三個孩子分發 二、一、2 顆糖果。
示例 2:
輸入: [1,2,2] 輸出: 4 解釋: 你能夠分別給這三個孩子分發 一、二、1 顆糖果。 第三個孩子只獲得 1 顆糖果,這已知足上述兩個條件。
28ms
1 public class Solution { 2 func candy(_ ratings: [Int]) -> Int { 3 let size = ratings.count 4 5 if size == 0 { 6 return -1 7 } 8 if size == 1 { 9 return 1 10 } 11 12 var candys = Array(repeating: 1, count: size) 13 14 for i in 1..<size { 15 if ratings[i] > ratings[i - 1] { 16 candys[i] = candys[i - 1] + 1 17 } 18 } 19 for i in 1..<size { 20 let j = size - i 21 if ratings[j - 1] > ratings[j], candys[j - 1] <= candys[j]{ 22 candys[j - 1] = candys[j] + 1 23 } 24 } 25 26 var sum = 0 27 for i in 0..<size { 28 sum += candys[i] 29 } 30 return sum 31 } 32 }
60ms
1 class Solution { 2 func candy(_ ratings: [Int]) -> Int { 3 var result = 1 4 var more = 0 // 連升次數 5 var less = 0 // 連降次數 6 var mark = 0 7 8 for index in 1..<ratings.count { 9 let dis = ratings[index] - ratings[index - 1] 10 if dis > 0 { // 升 11 more += 1 12 less = 0 13 14 result += more + 1 15 } else if dis == 0 { 16 more = 0 17 less = 0 18 mark = 0 19 result += 1 20 } else { 21 if more > 0 { 22 mark = more 23 more = 0 24 } 25 less += 1 26 result += less + (less > mark ? 1 : 0) 27 } 28 } 29 return result 30 } 31 }
84ms
1 class Solution { 2 func candy(_ ratings: [Int]) -> Int { 3 var candies = Array(repeating:1,count:ratings.count) 4 var numCandies = 0 5 6 //Go forward 7 for i in 1..<ratings.count { 8 if(ratings[i]>ratings[i-1]) { 9 candies[i] = candies[i-1]+1 10 } 11 } 12 13 for i in stride(from:ratings.count-2,through:0,by:-1) { 14 if(ratings[i]>ratings[i+1] && (candies[i] <= candies[i+1])) { 15 candies[i] = candies[i+1]+1 16 } 17 } 18 19 print(candies) 20 numCandies = candies.reduce(0,+) 21 22 return numCandies 23 } 24 }
172ms
1 class Solution { 2 func candy(_ ratings: [Int]) -> Int { 3 guard ratings.count > 0 else { return 0 } 4 5 // o(n), space, nlog(n) time 6 let orderedKids = ratings.enumerated().map { ($1, $0) }.sorted { $0.0 < $1.0 } 7 8 // (n), space, n, time 9 var candies = Array(repeating: 1, count: ratings.count) 10 11 // n time 12 orderedKids.forEach { (rating, index) in 13 var currentCandy = candies[index] 14 if index > 0 && rating < ratings[index - 1] && currentCandy >= candies[index - 1] { 15 candies[index - 1] = currentCandy + 1 16 } 17 if index < candies.count - 1 && rating < ratings[index + 1] && currentCandy >= candies[index + 1] { 18 candies[index + 1] = currentCandy + 1 19 } 20 } 21 // n time, o (1) space 22 return candies.reduce(into: 0) { $0 += $1 } 23 } 24 }