File tree Expand file tree Collapse file tree 2 files changed +23
-23
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +23
-23
lines changed Original file line number Diff line number Diff line change 11//
2- // 217. Contains Duplicate .swift
3- // https://leetcode.com/problems/contains-duplicate /description/
2+ // 121. Best Time to Buy and Sell Stock .swift
3+ // https://leetcode.com/problems/best-time-to-buy-and-sell-stock /description/
44// Algorithm
55//
66// Created by 홍승현 on 2024/04/26.
77//
88
99import Foundation
1010
11- final class LeetCode217 {
12- func containsDuplicate( _ nums: [ Int ] ) -> Bool {
13- Set ( nums) . count != nums. count
11+ final class LeetCode121 {
12+ func maxProfit( _ prices: [ Int ] ) -> Int {
13+ if prices. count == 1 { return 0 }
14+ var diff = 0
15+ var left = 0
16+ var right = 0
17+
18+ while right < prices. endIndex {
19+ if prices [ left] > prices [ right] {
20+ left = right
21+ } else {
22+ diff = max ( diff, prices [ right] - prices[ left] )
23+ }
24+ right += 1
25+ }
26+ return diff
1427 }
1528}
Original file line number Diff line number Diff line change 11//
2- // 121. Best Time to Buy and Sell Stock .swift
3- // https://leetcode.com/problems/best-time-to-buy-and-sell-stock /description/
2+ // 217. Contains Duplicate .swift
3+ // https://leetcode.com/problems/contains-duplicate /description/
44// Algorithm
55//
66// Created by 홍승현 on 2024/04/26.
77//
88
99import Foundation
1010
11- final class LeetCode121 {
12- func maxProfit( _ prices: [ Int ] ) -> Int {
13- if prices. count == 1 { return 0 }
14- var diff = 0
15- var left = 0
16- var right = 0
17-
18- while right < prices. endIndex {
19- if prices [ left] > prices [ right] {
20- left = right
21- } else {
22- diff = max ( diff, prices [ right] - prices[ left] )
23- }
24- right += 1
25- }
26- return diff
11+ final class LeetCode217 {
12+ func containsDuplicate( _ nums: [ Int ] ) -> Bool {
13+ Set ( nums) . count != nums. count
2714 }
2815}
You can’t perform that action at this time.
0 commit comments