0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode Easy 1099. Two Sum Less Than K

Posted at

問題:
与えられた数列のうち、2つの要素の合計値が
Kを超えない最大の合計値を求める。

解法:
2重ループで一つずつ比較して条件を満たす最大値を返す。

using namespace std;
# include <iostream>
# include <algorithm>
class Solution {
public:
    int twoSumLessThanK(vector<int>& A, int K) {
        int maxV = -1;
        for (int i = 0; i < A.size(); i++) {
            for (int j = i + 1; j < A.size(); j++) {
                if (A[i] + A[j] < K) {
                    maxV = max(maxV, A[i] + A[j]);
                }
            }
        }
        return maxV;
    }
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?